diff --git a/source/engine/rules.js b/source/engine/rules.js index 9d9491119..82b0215e0 100644 --- a/source/engine/rules.js +++ b/source/engine/rules.js @@ -52,7 +52,12 @@ export let enrichRule = (rule, sharedData = {}) => { subquestion = subquestionMarkdown && marked(subquestionMarkdown), defaultValue = rule['par défaut'], examples = rule['exemples'], - icon = rule['icônes'] + icon = rule['icônes'], + justNamespace = + !rule.formule && + !rule.question && + !rule['non applicable si'] && + !rule['applicable si'] return { ...rule, @@ -66,7 +71,8 @@ export let enrichRule = (rule, sharedData = {}) => { defaultValue, raw: rule, examples, - icon + icon, + justNamespace } } catch (e) { throw new Error('Problem enriching ' + JSON.stringify(rule)) @@ -105,17 +111,24 @@ export let encodeRuleName = name => export let decodeRuleName = name => name.replace(/--/g, ' . ').replace(/-/g, ' ') +export let ruleParents = dottedName => { + let fragments = splitName(dottedName) // dottedName ex. [CDD . événements . rupture] + return range(1, fragments.length) + .map(nbEl => take(nbEl)(fragments)) + .reverse() // -> [ [CDD . événements . rupture], [CDD . événements], [CDD] ] +} /* Les variables peuvent être exprimées dans la formule d'une règle relativement à son propre espace de nom, pour une plus grande lisibilité. Cette fonction résoud cette ambiguité. */ export let disambiguateRuleReference = ( allRules, - { ns, name }, + { dottedName, name }, partialName ) => { - let fragments = ns ? [...ns.split(' . '), name] : [], // ex. [CDD . événements . rupture] - pathPossibilities = range(0, fragments.length + 1) // -> [ [CDD . événements . rupture], [CDD . événements], [CDD] ] - .map(nbEl => take(nbEl)(fragments)) - .reverse(), + let pathPossibilities = [ + [], // the top level namespace + ...ruleParents(dottedName), // the parents namespace + splitName(dottedName) // the rule's own namespace + ], found = reduce( (res, path) => when(is(Object), reduced)( diff --git a/test/rules.test.js b/test/rules.test.js index 8f840e088..d7a49621f 100644 --- a/test/rules.test.js +++ b/test/rules.test.js @@ -2,6 +2,8 @@ import { map } from 'ramda' import { expect } from 'chai' import { rules, + disambiguateRuleReference, + ruleParents, enrichRule, translateAll, nestedSituationToPathMap @@ -132,4 +134,53 @@ describe('misc', function() { '2300' ) }) + it('should procude an array of the parents of a rule', function() { + let rawRules = [ + { nom: 'CDD', question: 'CDD ?' }, + { nom: 'taxe', formule: 'montant annuel / 12', espace: 'CDD' }, + { + nom: 'montant annuel', + formule: '20 - exonération annuelle', + espace: 'CDD . taxe' + }, + { + nom: 'exonération annuelle', + formule: 20, + espace: 'CDD . taxe . montant annuel' + } + ] + + let parents = ruleParents(rawRules.map(enrichRule)[3].dottedName) + expect(parents).to.eql([ + ['CDD', 'taxe', 'montant annuel'], + ['CDD', 'taxe'], + ['CDD'] + ]) + }) + it("should disambiguate a reference to another rule in a rule, given the latter's namespace", function() { + let rawRules = [ + { nom: 'CDD', question: 'CDD ?' }, + { nom: 'taxe', formule: 'montant annuel / 12', espace: 'CDD' }, + { + nom: 'montant annuel', + formule: '20 - exonération annuelle', + espace: 'CDD . taxe' + }, + { + nom: 'exonération annuelle', + formule: 20, + espace: 'CDD . taxe . montant annuel' + } + ] + + let enrichedRules = rawRules.map(enrichRule), + resolved = disambiguateRuleReference( + enrichedRules, + enrichedRules[2], + 'exonération annuelle' + ) + expect(resolved).to.eql( + 'CDD . taxe . montant annuel . exonération annuelle' + ) + }) })