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