La librairie permet d'étendre la base défaut avec de nouvelles règles

co2
Mael 2018-09-25 17:22:32 +00:00
parent 490c793f79
commit e386428cee
3 changed files with 43 additions and 6 deletions

View File

@ -17,10 +17,18 @@ let nestedSituationToStateSelector = nestedSituation => dottedName =>
...nestedSituationToPathMap(nestedSituation)
}[dottedName])
let enrichYaml = string => yaml.safeLoad(string).map(enrichRule)
export default {
evaluate: (targetNames, nestedSituation, rulesYaml) => {
let rules = rulesYaml
? (rules = parseAll(yaml.safeLoad(rulesYaml).map(enrichRule)))
evaluate: (targetNames, nestedSituation, rulesConfig) => {
let rules = rulesConfig
? do {
let { base, extra } = rulesConfig
parseAll([
...(base ? enrichYaml(base) : rulesFr),
...(extra ? enrichYaml(extra) : [])
])
}
: defaultRules
return analyseMany(rules, targetNames)(

View File

@ -2,7 +2,7 @@ import { expect } from 'chai'
import Syso from '../source/engine/index'
import { propEq } from 'ramda'
import dedent from 'dedent-js'
console.log(Syso)
import sasuYaml from 'yaml-loader!../source/règles/sasu.yaml'
describe('library', function() {
it('should evaluate one target with no input data', function() {
@ -17,15 +17,44 @@ describe('library', function() {
})
it('should let the user replace the default rules', function() {
let newRules = `
let rules = `
- nom: yo
formule: 90890
- nom: ya
formule: yo + 1
`
let evaluated = Syso.evaluate(['ya'], {}, newRules)
let evaluated = Syso.evaluate(['ya'], {}, { base: rules })
expect(evaluated.targets[0].nodeValue).to.equal(90891)
})
it('should let the user add rules to the default ones', function() {
let rules = `
- nom: yo
formule: 1
- nom: ya
formule: contrat salarié . salaire . net + yo
`
let evaluated = Syso.evaluate(
['ya'],
{
'contrat salarié . salaire . brut de base': 2300
},
{ extra: rules }
)
expect(evaluated.targets[0].nodeValue).to.be.closeTo(1780.41, 1)
})
it('should let the user extend the rules constellation in a serious manner', function() {
let evaluated = Syso.evaluate(
['ya'],
{
'contrat salarié . salaire . brut de base': 2300
},
{ extra: sasuYaml }
)
expect(evaluated.targets[0].nodeValue).to.be.closeTo(1780.41, 1)
})
})