From 12a4f963685ee9d8249e30067a6fe4191ff46540 Mon Sep 17 00:00:00 2001 From: Maxime Quandalle Date: Sun, 14 Jun 2020 13:18:16 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20Supprime=20la=20fonction=20getSi?= =?UTF-8?q?tuationValue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- publicodes/source/getSituationValue.js | 33 --------------- publicodes/source/parseReference.js | 9 +--- publicodes/source/ruleUtils.ts | 15 ++----- publicodes/test/variables.test.js | 57 -------------------------- 4 files changed, 5 insertions(+), 109 deletions(-) delete mode 100644 publicodes/source/getSituationValue.js delete mode 100644 publicodes/test/variables.test.js diff --git a/publicodes/source/getSituationValue.js b/publicodes/source/getSituationValue.js deleted file mode 100644 index 8d3685135..000000000 --- a/publicodes/source/getSituationValue.js +++ /dev/null @@ -1,33 +0,0 @@ -import { dropLast, isEmpty, last } from 'ramda' -import { joinName, splitName } from './ruleUtils' - -let evaluateBottomUp = situation => startingFragments => { - let rec = (parentFragments, childFragments = []) => - parentFragments.length == 0 - ? null - : (function() { - let query = joinName(parentFragments), - expectedResult = isEmpty(childFragments) - ? 'oui' - : joinName(childFragments) - - return situation[query] == null - ? rec(dropLast(1)(parentFragments), [ - last(parentFragments), - ...childFragments - ]) - : situation[query] == expectedResult - })() - - return rec(startingFragments) -} -export let getSituationValue = (situation, variableName, rule) => { - // get the current situation value - // it's the user input or test input, possibly with default values - let value = situation[variableName] - - if (rule.formule?.['une possibilité']) - return evaluateBottomUp(situation)(splitName(variableName)) - - return value -} diff --git a/publicodes/source/parseReference.js b/publicodes/source/parseReference.js index d91c9e21b..edf00c89c 100644 --- a/publicodes/source/parseReference.js +++ b/publicodes/source/parseReference.js @@ -3,7 +3,6 @@ import { Leaf } from './components/mecanisms/common' import { typeWarning } from './error' import { evaluateApplicability } from './evaluateRule' import { evaluateNode, mergeMissing } from './evaluation' -import { getSituationValue } from './getSituationValue' import { convertNodeToUnit } from './nodeUnits' import parseRule from './parseRule' import { disambiguateRuleReference } from './ruleUtils' @@ -48,11 +47,7 @@ const getApplicableReplacements = ( // Remove remplacement defined in a node whose situation value is false .filter(({ referenceNode }) => { const referenceRule = rules[referenceNode.dottedName] - const situationValue = getSituationValue( - situation, - referenceRule.dottedName, - referenceRule - ) + const situationValue = situation[referenceRule.dottedName] if (referenceNode.question && situationValue == null) { missingVariableList.push({ [referenceNode.dottedName]: 1 }) } @@ -177,7 +172,7 @@ Par défaut, seul le premier s'applique. Si vous voulez un autre comportement, v applicabilityEvaluation ) } - const situationValue = getSituationValue(situation, dottedName, rule) + const situationValue = situation[dottedName] if (situationValue != null) { const unit = !situationValue.unit || serializeUnit(situationValue.unit) === '' diff --git a/publicodes/source/ruleUtils.ts b/publicodes/source/ruleUtils.ts index 3047aacbd..5408c3dd4 100644 --- a/publicodes/source/ruleUtils.ts +++ b/publicodes/source/ruleUtils.ts @@ -1,13 +1,8 @@ -import { dropLast, last, pipe, range, take } from 'ramda' +import { last, pipe, range, take } from 'ramda' import { Rule, Rules } from './types' -export const splitName = (str: string) => str.split(' . ') -export const joinName = strs => strs.join(' . ') -export const parentName = pipe( - splitName, - dropLast(1) as (a: string[]) => string[], - joinName -) +const splitName = (str: string) => str.split(' . ') +const joinName = strs => strs.join(' . ') export const nameLeaf = pipe(splitName, last) export const encodeRuleName = name => name @@ -47,10 +42,6 @@ export function disambiguateRuleReference( return dottedName } -/********************************* - Autres - */ - export function findParentDependencies( rules: Rules, name: Names diff --git a/publicodes/test/variables.test.js b/publicodes/test/variables.test.js deleted file mode 100644 index ef0ad5a96..000000000 --- a/publicodes/test/variables.test.js +++ /dev/null @@ -1,57 +0,0 @@ -import { expect } from 'chai' -import { getSituationValue } from '../source/getSituationValue' - -describe('getSituationValue', function() { - it('should directly return the value of any rule that specifies a format (i.e currency, duration)', function() { - let rule = { unité: '€' }, - situation = { salaire: '2300' } - - expect(getSituationValue(situation, 'salaire', rule)).to.equal('2300') - }) - - it("should interpret rules with 'one of these', with 'oui' for true", function() { - let rule = { formule: { 'une possibilité': ['noir', 'blanc'] } }, - situation = { condition: 'oui' } - - expect(getSituationValue(situation, 'condition', rule)).to.be.true - }) - - it('should walk up the namespace chain until it finds the tail as the value', function() { - let rule = { formule: { 'une possibilité': ['noir', 'blanc'] } }, - situation = { - 'contrat salarié . CDD . motif': 'classique . accroissement activité' - } - - expect( - getSituationValue( - situation, - 'contrat salarié . CDD . motif . classique . accroissement activité', - rule - ) - ).to.be.true - }) - - it("should return null if a value isn't found for the name given", function() { - let rule = { formule: { 'une possibilité': ['noir', 'blanc'] } }, - situation = { condition: 'classique . accroissement activité' } - - expect( - getSituationValue( - situation, - 'contrat salarié . CDD . motif . classique . accroissement activité', - rule - ) - ).to.be.null - }) - - it('should set the value of variants to false if one of them is true', function() { - let rule = { - nom: 'univers . ici', - formule: { 'une possibilité': ['noir', 'blanc'] } - }, - situation = { 'univers . ici': 'blanc' } - - expect(getSituationValue(situation, 'univers . ici . noir', rule)).to.be - .false - }) -})