mon-entreprise/source/engine/rules.js

56 lines
1.3 KiB
JavaScript
Raw Normal View History

2016-12-07 18:08:10 +00:00
import rules from './load-rules'
2017-01-23 18:06:46 +00:00
import entityRules from './load-entity-rules'
2016-12-07 18:08:10 +00:00
2017-01-23 18:06:46 +00:00
import possibleVariableTypes from './possibleVariableTypes.yaml'
import {borrify} from './remove-diacritics'
import R from 'ramda'
2016-12-07 18:08:10 +00:00
export let findRuleByName = search =>
2016-12-07 18:08:10 +00:00
rules
.map(enrichRule)
.find( ({name}) =>
name === search
2016-12-07 18:08:10 +00:00
)
export let searchRules = searchInput =>
rules
.filter( rule =>
rule && hasKnownRuleType(rule) &&
JSON.stringify(rule).indexOf(searchInput) > -1)
.map(enrichRule)
2016-12-07 18:08:10 +00:00
export let enrichRule = rule => {
2016-12-07 18:08:10 +00:00
let type = possibleVariableTypes.find(t => rule[t])
return {...rule, type, name: rule[type]}
2016-12-07 18:08:10 +00:00
}
export let hasKnownRuleType = rule => rule && enrichRule(rule).type
2017-01-23 18:06:46 +00:00
export let fullDottedName = rule => rule.attache && borrify(
2017-01-23 18:06:46 +00:00
[ rule.attache,
do { let {alias, name} = enrichRule(rule)
alias || name
}
2017-01-23 18:06:46 +00:00
].join(' . ')
)
export let findRuleByDottedName = dottedName =>
entityRules.map(enrichRule).find(rule => fullDottedName(rule) == borrify(dottedName))
export let findGroup = R.pipe(
findRuleByDottedName,
found => found && found['choix exclusifs'] && found,
// Is there a way to express this more litterally in ramda ?
// R.unless(
// R.isNil,
// R.when(
// R.has('choix exclusifs'),
// R.identity
// )
// )
)
console.log('findG', findGroup('Salariat . CDD . événements'))