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