2020-06-14 11:18:16 +00:00
import { last , pipe , range , take } from 'ramda'
2020-05-08 10:04:00 +00:00
import { Rule , Rules } from './types'
2020-03-26 15:03:19 +00:00
2020-06-14 11:18:16 +00:00
const splitName = ( str : string ) = > str . split ( ' . ' )
const joinName = strs = > strs . join ( ' . ' )
2020-11-11 18:32:41 +00:00
export const nameLeaf = pipe < string , string [ ] , string > ( splitName , last )
2020-03-26 15:03:19 +00:00
export const encodeRuleName = name = >
2020-05-08 10:04:00 +00:00
name
2020-09-01 23:47:30 +00:00
? . replace ( /\s\.\s/g , '/' )
2020-05-08 10:04:00 +00:00
. replace ( /-/g , '\u2011' ) // replace with a insecable tiret to differenciate from space
. replace ( /\s/g , '-' )
2020-04-30 15:13:45 +00:00
export const decodeRuleName = name = >
2020-05-08 10:04:00 +00:00
name
. replace ( /\//g , ' . ' )
. replace ( /-/g , ' ' )
. replace ( /\u2011/g , '-' )
2020-03-30 17:14:03 +00:00
export function ruleParents < Names extends string > (
dottedName : Names
) : Array < Names > {
2020-04-30 15:13:45 +00:00
const fragments = splitName ( dottedName ) // dottedName ex. [CDD . événements . rupture]
2020-03-26 15:03:19 +00:00
return range ( 1 , fragments . length )
2020-03-30 17:14:03 +00:00
. map ( nbEl = > take ( nbEl , fragments ) )
2020-03-26 15:03:19 +00:00
. map ( joinName ) // -> [ [CDD . événements . rupture], [CDD . événements], [CDD
. reverse ( )
}
2020-03-30 17:14:03 +00:00
export function disambiguateRuleReference < Names extends string > (
rules : Rules < Names > ,
contextName : Names ,
partialName : string
) {
2020-03-26 15:03:19 +00:00
const possibleDottedName = [
contextName ,
. . . ruleParents ( contextName ) ,
''
] . map ( x = > ( x ? x + ' . ' + partialName : partialName ) )
const dottedName = possibleDottedName . find ( name = > name in rules )
if ( ! dottedName ) {
throw new Error ( ` La référence ' ${ partialName } ' est introuvable.
V é rifiez que l 'orthographe et l' espace de nom sont corrects ` )
}
return dottedName
}
2020-03-30 17:14:03 +00:00
export function findParentDependencies < Names extends string > (
rules : Rules < Names > ,
name : Names
) : Array < Names > {
2020-03-26 15:03:19 +00:00
// A parent dependency means that one of a rule's parents is not just a namespace holder, it is a boolean question. E.g. is it a fixed-term contract, yes / no
// When it is resolved to false, then the whole branch under it is disactivated (non applicable)
// It lets those children omit obvious and repetitive parent applicability tests
2020-03-30 17:14:03 +00:00
return ruleParents ( name )
. map ( parent = > [ parent , rules [ parent ] ] as [ Names , Rule ] )
. filter ( ( [ _ , rule ] ) = > ! ! rule )
. filter (
2020-06-08 15:43:55 +00:00
( [ _ , { question , unit é , formule , type } ] ) = >
2020-03-30 17:14:03 +00:00
//Find the first "calculable" parent
( question && ! unit é && ! formule ) ||
2020-06-08 15:43:55 +00:00
type === 'groupe' ||
2020-03-26 15:03:19 +00:00
( question && formule ? . [ 'une possibilité' ] !== undefined ) ||
( typeof formule === 'string' && formule . includes ( ' = ' ) ) ||
formule === 'oui' ||
formule === 'non' ||
formule ? . [ 'une de ces conditions' ] ||
formule ? . [ 'toutes ces conditions' ]
2020-03-30 17:14:03 +00:00
)
. map ( ( [ name , _ ] ) = > name )
2020-03-26 15:03:19 +00:00
}
2020-09-17 21:23:11 +00:00
export function ruleWithDedicatedDocumentationPage ( rule ) {
return (
rule . virtualRule !== true &&
rule . type !== 'groupe' &&
rule . type !== 'texte' &&
rule . type !== 'paragraphe' &&
rule . type !== 'notification'
)
}