From 9bbe251f56e7e3602b3c860915ff5fe00a3aa004 Mon Sep 17 00:00:00 2001 From: Mael Date: Mon, 25 Jun 2018 16:58:00 +0000 Subject: [PATCH] =?UTF-8?q?Simplification=20du=20m=C3=A9canisme=20s=C3=A9l?= =?UTF-8?q?ection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/engine/mecanisms.js | 24 ++++++------------- .../transform-taux-versement-transport.js | 4 ++-- test/traverse.test.js | 6 ++--- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/source/engine/mecanisms.js b/source/engine/mecanisms.js index db03fea4a..faa0ec438 100644 --- a/source/engine/mecanisms.js +++ b/source/engine/mecanisms.js @@ -975,27 +975,17 @@ export let mecanismSelection = (recurse, k, v) => { dataSource = findRuleByName(parsedRules, dataSourceName), data = dataSource ? dataSource['data'] : null, dataKey = explanation.nodeValue, - dataItems = + found = data && dataKey && dataSearchField - ? filter(item => item[dataSearchField] == dataKey, data) + ? find(item => item[dataSearchField] == dataKey, data) : null, - dataItemValues = - dataItems && !isEmpty(dataItems) ? values(dataItems) : null, - // TODO - over-specific! transform the JSON instead - dataItemSubValues = - dataItemValues && dataItemValues[0][dataTargetName] - ? dataItemValues[0][dataTargetName]['taux'] - : null, - sortedSubValues = dataItemSubValues - ? sortBy(pair => pair[0], toPairs(dataItemSubValues)) - : null, // return 0 if we found a match for the lookup but not for the specific field, // so that component sums don't sum to null - nodeValue = dataItems - ? sortedSubValues - ? Number.parseFloat(last(sortedSubValues)[1]) / 100 - : 0 - : null, + nodeValue = + (found && + found[dataTargetName] && + Number.parseFloat(found[dataTargetName]) / 100) || + 0, missingVariables = explanation.missingVariables return rewriteNode(node, nodeValue, explanation, missingVariables) diff --git a/source/règles/transform-taux-versement-transport.js b/source/règles/transform-taux-versement-transport.js index 7d1930960..115ac8496 100644 --- a/source/règles/transform-taux-versement-transport.js +++ b/source/règles/transform-taux-versement-transport.js @@ -87,9 +87,9 @@ fs.writeFile( JSON.stringify(trimmed), function(err) { if (err) { - return console.log(err) + return console.log(err) //eslint-disable-line no-console } - console.log("C'est tout bon !") + console.log("C'est tout bon !") //eslint-disable-line no-console } ) diff --git a/test/traverse.test.js b/test/traverse.test.js index e5710ccf2..ef9801bc1 100644 --- a/test/traverse.test.js +++ b/test/traverse.test.js @@ -440,8 +440,8 @@ describe('analyse with mecanisms', function() { let stateSelector = name => ({ 'top . code postal': '2' }[name]) let data = { taux_versement_transport: [ - { codePostal: 1, aot: { taux: { '2019': '1.0' } } }, - { codePostal: 2, smt: { taux: { '2019': '2.0' } } } + { codePostal: 1, aot: '2.5' }, + { codePostal: 2, smt: '1.5' } ] } let rawRules = [ @@ -463,7 +463,7 @@ describe('analyse with mecanisms', function() { rules = parseAll(rawRules.map(rule => enrichRule(rule, data))) expect( analyse(rules, 'startHere')(stateSelector).targets[0] - ).to.have.property('nodeValue', 0.02) + ).to.have.property('nodeValue', 0.015) }) it('should handle failed selections', function() {