mon-entreprise/source/engine/index.js

38 lines
991 B
JavaScript
Raw Normal View History

2018-08-16 08:05:00 +00:00
// This file exports the functions of the public computing library
import { analyseMany, parseAll } from './traverse.js'
import {
rulesFr,
collectDefaults,
nestedSituationToPathMap,
enrichRule
} from './rules'
import yaml from 'js-yaml'
2018-08-15 09:39:10 +00:00
2018-08-16 08:05:00 +00:00
// The public evaluation function takes a nested object of input values
let nestedSituationToStateSelector = rules => nestedSituation => dottedName =>
2018-08-16 08:05:00 +00:00
({
...collectDefaults(rules),
2018-08-16 08:05:00 +00:00
...nestedSituationToPathMap(nestedSituation)
}[dottedName])
2018-09-26 09:35:29 +00:00
let enrichRules = input =>
(typeof input === 'string' ? yaml.safeLoad(input) : input).map(enrichRule)
2018-09-24 15:14:40 +00:00
export default {
evaluate: (targetNames, nestedSituation, rulesConfig) => {
let rules = rulesConfig
? do {
let { base, extra } = rulesConfig
;[
2018-09-26 09:35:29 +00:00
...(base ? enrichRules(base) : rulesFr),
...(extra ? enrichRules(extra) : [])
]
}
: rulesFr
return analyseMany(parseAll(rules), targetNames)(
nestedSituationToStateSelector(rules)(nestedSituation)
)
}
2018-09-24 15:14:40 +00:00
}