mon-entreprise/source/engine/getSituationValue.js

34 lines
1015 B
JavaScript
Raw Normal View History

import { dropLast, isEmpty, last } from 'ramda'
2020-03-26 15:03:19 +00:00
import { joinName, splitName } from './ruleUtils'
let evaluateBottomUp = situationGate => startingFragments => {
let rec = (parentFragments, childFragments = []) =>
parentFragments.length == 0
? null
: (function() {
let query = joinName(parentFragments),
2018-01-08 15:07:26 +00:00
expectedResult = isEmpty(childFragments)
? 'oui'
: joinName(childFragments)
return situationGate(query) == null
2018-01-08 15:07:26 +00:00
? rec(dropLast(1)(parentFragments), [
last(parentFragments),
...childFragments
])
: situationGate(query) == expectedResult
})()
return rec(startingFragments)
}
export let getSituationValue = (situationGate, variableName, rule) => {
// get the current situation value
// it's the user input or test input, possibly with default values
let value = situationGate(variableName)
if (rule.formule && rule.formule['une possibilité'])
return evaluateBottomUp(situationGate)(splitName(variableName))
return value
}