mon-entreprise/source/engine/rules.js

88 lines
2.2 KiB
JavaScript
Raw Normal View History

// Séparation artificielle, temporaire, entre ces deux types de règles
import rawRules from './load-rules'
import rawEntityRules from './load-entity-rules'
import R from 'ramda'
2017-01-23 18:06:46 +00:00
import possibleVariableTypes from './possibleVariableTypes.yaml'
import marked from 'marked'
let customMarked = new marked.Renderer()
customMarked.link = ( href, title, text ) =>
`<a target="_blank" href="${ href }" title="${ title }">${ text }</a>`
marked.setOptions({
renderer: customMarked
})
/***********************************
Méthodes agissant sur une règle */
// Enrichissement de la règle avec des informations évidentes pour un lecteur humain
export let enrichRule = rule => {
let
type = possibleVariableTypes.find(t => rule[t]),
name = rule[type],
dottedName = rule.attache && [
rule.attache,
rule.alias || name
].join(' . '),
subquestionMarkdown = rule['sous-question'],
subquestion = subquestionMarkdown && marked(subquestionMarkdown)
return {...rule, type, name, dottedName, subquestion}
}
export let hasKnownRuleType = rule => rule && enrichRule(rule).type
let
splitName = R.split(' . '),
joinName = R.join(' . ')
export let parentName = R.pipe(
splitName,
R.dropLast(1),
joinName
)
export let nameLeaf = R.pipe(
splitName,
R.last
)
// On enrichit la base de règles avec des propriétés dérivées de celles du YAML
export let [rules, entityRules] =
[rawRules, rawEntityRules].map(rules => rules.map(enrichRule))
/****************************************
Méthodes de recherche d'une règle */
2016-12-07 18:08:10 +00:00
export let findRuleByName = search =>
[...rules, ...entityRules]
.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).toLowerCase().indexOf(searchInput) > -1)
.map(enrichRule)
2016-12-07 18:08:10 +00:00
2017-01-23 18:06:46 +00:00
export let findRuleByDottedName = dottedName =>
entityRules.find(rule => rule.dottedName == dottedName)
export let findGroup = R.pipe(
findRuleByDottedName,
found => found && found['une possibilité'] && found,
// Is there a way to express this more litterally in ramda ?
// R.unless(
// R.isNil,
// R.when(
// R.has('une possibilité'),
// R.identity
// )
// )
)