diff --git a/source/engine/index.js b/source/engine/index.js index 8a78045d7..b1846f54f 100644 --- a/source/engine/index.js +++ b/source/engine/index.js @@ -1,8 +1,14 @@ // This file exports the functions of the public computing library import { analyseMany, parseAll } from './traverse.js' -import { rulesFr, collectDefaults, nestedSituationToPathMap } from './rules' +import { + rulesFr, + collectDefaults, + nestedSituationToPathMap, + enrichRule +} from './rules' +import yaml from 'js-yaml' -let parsedRules = parseAll(rulesFr) +let defaultRules = parseAll(rulesFr) // The public evaluation function takes a nested object of input values let nestedSituationToStateSelector = nestedSituation => dottedName => @@ -12,9 +18,13 @@ let nestedSituationToStateSelector = nestedSituation => dottedName => }[dottedName]) export default { - eval: (targetNames, nestedSituation) => - analyseMany(parsedRules, targetNames)( + evaluate: (targetNames, nestedSituation, rulesYaml) => { + let rules = rulesYaml + ? (rules = parseAll(yaml.safeLoad(rulesYaml).map(enrichRule))) + : defaultRules + + return analyseMany(rules, targetNames)( nestedSituationToStateSelector(nestedSituation) - ), - a: () => 7 + ) + } } diff --git a/test/library.test.js b/test/library.test.js index 02fee93df..ddf90bc55 100644 --- a/test/library.test.js +++ b/test/library.test.js @@ -1,11 +1,13 @@ import { expect } from 'chai' -import evaluate from '../source/engine/index' +import Syso from '../source/engine/index' import { propEq } from 'ramda' +import dedent from 'dedent-js' +console.log(Syso) describe('library', function() { it('should evaluate one target with no input data', function() { let target = 'contrat salariƩ . salaire . net' - let evaluated = evaluate([target], { + let evaluated = Syso.evaluate([target], { 'contrat salariƩ': { salaire: { 'brut de base': 2300 } } }) let evaluatedTarget = evaluated.targets.find(propEq('dottedName', target)) @@ -13,4 +15,17 @@ describe('library', function() { .to.have.property('nodeValue') .to.be.within(1779, 1780) }) + + it('should let the user replace the default rules', function() { + let newRules = ` +- nom: yo + formule: 90890 +- nom: ya + formule: yo + 1 +` + + let evaluated = Syso.evaluate(['ya'], {}, newRules) + + expect(evaluated.targets[0].nodeValue).to.equal(90891) + }) })