mon-entreprise/source/engine/index.js

36 lines
1.0 KiB
JavaScript
Raw Normal View History

2018-08-16 08:05:00 +00:00
// This file exports the functions of the public computing library
import { safeLoad } from 'js-yaml'
import { collectDefaults, enrichRule, rulesFr } from './rules'
import { analyseMany, parseAll } from './traverse.js'
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 inputToStateSelector = rules => input => dottedName =>
2018-08-16 08:05:00 +00:00
({
...collectDefaults(rules),
...input
2018-08-16 08:05:00 +00:00
}[dottedName])
2018-09-26 09:35:29 +00:00
let enrichRules = input =>
(typeof input === 'string' ? safeLoad(input) : input).map(enrichRule)
2018-09-24 15:14:40 +00:00
export default {
evaluate: (targetInput, input, config) => {
let rules = config
? [
...(config.base ? enrichRules(config.base) : rulesFr),
...(config.extra ? enrichRules(config.extra) : [])
]
: rulesFr
let evaluation = analyseMany(
parseAll(rules),
Array.isArray(targetInput) ? targetInput : [targetInput]
)(inputToStateSelector(rules)(input))
if (config?.debug) return evaluation
let values = evaluation.targets.map(t => t.nodeValue)
return Array.isArray(targetInput) ? values : values[0]
}
2018-09-24 15:14:40 +00:00
}