From 3917eb2f2485cc33fe8ea4e73c440e19685a3342 Mon Sep 17 00:00:00 2001 From: Mael Date: Wed, 7 Nov 2018 16:55:42 +0000 Subject: [PATCH] =?UTF-8?q?Impl=C3=A9mentation=20de=20p=C3=A9riode=20:=20f?= =?UTF-8?q?lexible?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/engine/treatVariable.js | 25 +++++++++++++++++++++---- test/library.test.js | 7 ++++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/source/engine/treatVariable.js b/source/engine/treatVariable.js index 1ce17a4e5..9cbd43cd7 100644 --- a/source/engine/treatVariable.js +++ b/source/engine/treatVariable.js @@ -118,16 +118,33 @@ export let treatVariableTransforms = (rules, rule) => parseResult => { let nodeValue = filteredNode.nodeValue // Temporal transformation - if (nodeValue == null || !isNumeric(nodeValue)) return filteredNode - + if (nodeValue == null) return filteredNode let ruleToTransform = findRuleByDottedName( rules, filteredNode.explanation.dottedName ) + if (!rule.période) { + if (ruleToTransform.période == 'flexible') + throw new Error( + `Attention, une variable sans période, ${ + rule.dottedName + }, qui appelle une variable à période flexible, ${ + ruleToTransform.dottedName + }, c'est suspect ! + ` + ) + + return filteredNode + } + if (!ruleToTransform.période) return filteredNode let environmentPeriod = situation('période') || 'mois' - let callingPeriod = rule['période'] || environmentPeriod - let calledPeriod = ruleToTransform['période'] || environmentPeriod + let callingPeriod = + rule.période == 'flexible' ? environmentPeriod : rule.période + let calledPeriod = + ruleToTransform.période == 'flexible' + ? environmentPeriod + : ruleToTransform.période let transformedNodeValue = callingPeriod === 'mois' && calledPeriod === 'année' diff --git a/test/library.test.js b/test/library.test.js index 1346facaf..70a27047e 100644 --- a/test/library.test.js +++ b/test/library.test.js @@ -32,6 +32,7 @@ describe('library', function() { - nom: yo formule: 1 - nom: ya + période: flexible formule: contrat salarié . salaire . net + yo ` @@ -128,7 +129,7 @@ describe('library', function() { expect(value).to.equal(7253.26) }) it('should let let user define a rule base on a completely different subject', function() { - let targets = ['impact'] + let targets = 'impact' let value = Syso.evaluate( targets, @@ -137,9 +138,9 @@ describe('library', function() { 'chauffage . type': 'gaz', 'durée de la douche': 10 }, - { base: co2, debug: true } + { base: co2, debug: false } ) - console.log(JSON.stringify(value.targets[0], null, 4)) + //console.log(JSON.stringify(value.targets[0], null, 4)) expect(value).to.equal(7253.26) }) })