From 0b046a9654d5afe6e0fe4a023b9c0568a27cea70 Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 10 May 2019 21:18:26 +0300 Subject: [PATCH 1/7] ln math function --- src/fhirpath.js | 2 ++ src/math.js | 7 +++++++ test/cases/5.7_math.yaml | 19 +++++++++++++++++++ ...vigation.yaml => 5.8_tree_navigation.yaml} | 0 ...ctions.yaml => 5.9_utility_functions.yaml} | 0 5 files changed, 28 insertions(+) create mode 100644 test/cases/5.7_math.yaml rename test/cases/{5.7_tree_navigation.yaml => 5.8_tree_navigation.yaml} (100%) rename test/cases/{5.8_utility_functions.yaml => 5.9_utility_functions.yaml} (100%) diff --git a/src/fhirpath.js b/src/fhirpath.js index 7e40115..a1ba4a4 100644 --- a/src/fhirpath.js +++ b/src/fhirpath.js @@ -96,6 +96,8 @@ engine.invocationTable = { replaceMatches: {fn: strings.replaceMatches, arity: {2: ["String", "String"]}}, length: {fn: strings.length }, + ln: {fn: math.natlog}, + now: {fn: datetime.now }, today: {fn: datetime.today }, diff --git a/src/math.js b/src/math.js index 5b34511..c5184a6 100644 --- a/src/math.js +++ b/src/math.js @@ -46,5 +46,12 @@ engine.mod = function(x, y){ return x % y; }; +engine.natlog = function(x){ + if (x.length > 0){ + return Math.log(x); + }else{ + return []; + } +}; module.exports = engine; diff --git a/test/cases/5.7_math.yaml b/test/cases/5.7_math.yaml new file mode 100644 index 0000000..596d924 --- /dev/null +++ b/test/cases/5.7_math.yaml @@ -0,0 +1,19 @@ +tests: + - desc: '5.7 Math' + - desc: '5.7.5 ln() : Decimal' + - desc: '** Can take the natural logarithm of the number' + expression: Math.n1.ln() + result: [0] + - desc: '** Empty result when taking logarithm from empty collection' + expression: Math.n2.ln() + result: [] + - desc: '** Error taking logarithm due to too many input parameters' + expression: Math.n3.ln(n4) + error: true + +subject: + resourceType: Math + n1: 1 + n2: [] + n3: 2 + n4: 8 diff --git a/test/cases/5.7_tree_navigation.yaml b/test/cases/5.8_tree_navigation.yaml similarity index 100% rename from test/cases/5.7_tree_navigation.yaml rename to test/cases/5.8_tree_navigation.yaml diff --git a/test/cases/5.8_utility_functions.yaml b/test/cases/5.9_utility_functions.yaml similarity index 100% rename from test/cases/5.8_utility_functions.yaml rename to test/cases/5.9_utility_functions.yaml From 816a9d5b69ec520e6def9936fbc72e39c584108f Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 10 May 2019 23:27:55 +0300 Subject: [PATCH 2/7] 5.7.6 log math function --- src/fhirpath.js | 1 + src/math.js | 4 ++++ test/cases/5.7_math.yaml | 8 ++++++++ 3 files changed, 13 insertions(+) diff --git a/src/fhirpath.js b/src/fhirpath.js index a1ba4a4..33ed494 100644 --- a/src/fhirpath.js +++ b/src/fhirpath.js @@ -97,6 +97,7 @@ engine.invocationTable = { length: {fn: strings.length }, ln: {fn: math.natlog}, + log: {fn: math.baselog, arity: {1: ["Number"]}, nullable: true}, now: {fn: datetime.now }, today: {fn: datetime.today }, diff --git a/src/math.js b/src/math.js index c5184a6..a779f51 100644 --- a/src/math.js +++ b/src/math.js @@ -54,4 +54,8 @@ engine.natlog = function(x){ } }; +engine.baselog = function(x, y){ + return (Math.log(x) / Math.log(y)); +}; + module.exports = engine; diff --git a/test/cases/5.7_math.yaml b/test/cases/5.7_math.yaml index 596d924..f733fe4 100644 --- a/test/cases/5.7_math.yaml +++ b/test/cases/5.7_math.yaml @@ -11,6 +11,14 @@ tests: expression: Math.n3.ln(n4) error: true + - desc: '5.7.6 log(base : Decimal) : Decimal' + - desc: '** Can take the logarithm of the number with a given base' + expression: Math.n4.log(2) + result: [3] + - desc: '** Empty result when taking logarithm from empty collection' + expression: Math.n2.log(8) + result: [] + subject: resourceType: Math n1: 1 From e56c3a6b157a52b8e40e41e4ab64d0834d0df873 Mon Sep 17 00:00:00 2001 From: Ivan Date: Mon, 13 May 2019 19:40:19 +0300 Subject: [PATCH 3/7] fixes --- src/math.js | 4 ++-- test/cases/5.7_math.yaml | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/math.js b/src/math.js index a779f51..21055fe 100644 --- a/src/math.js +++ b/src/math.js @@ -54,8 +54,8 @@ engine.natlog = function(x){ } }; -engine.baselog = function(x, y){ - return (Math.log(x) / Math.log(y)); +engine.baselog = function(x, base){ + return (Math.log(x) / Math.log(base)); }; module.exports = engine; diff --git a/test/cases/5.7_math.yaml b/test/cases/5.7_math.yaml index f733fe4..773e7ed 100644 --- a/test/cases/5.7_math.yaml +++ b/test/cases/5.7_math.yaml @@ -18,6 +18,9 @@ tests: - desc: '** Empty result when taking logarithm from empty collection' expression: Math.n2.log(8) result: [] + - desc: '** Empty result when taking logarithm with empty base' + expression: Math.n3.log() + result: [] subject: resourceType: Math From ad4cadba0479271bc2b7da284f3e42f40b4c6683 Mon Sep 17 00:00:00 2001 From: Ivan Date: Wed, 15 May 2019 16:47:05 +0300 Subject: [PATCH 4/7] arity fix and input mul collection test --- src/math.js | 13 +++++++++++-- test/cases/5.7_math.yaml | 9 ++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/math.js b/src/math.js index 21055fe..dccff3d 100644 --- a/src/math.js +++ b/src/math.js @@ -6,6 +6,13 @@ var engine = {}; +function ensureNumberSingleton(x){ + if(x.length == 1 && typeof x[0] === "number") { + return x[0]; + } + throw new Error("Expected number, but got " + JSON.stringify(x)); +} + engine.amp = function(x, y){ return (x || "") + (y || ""); }; @@ -48,14 +55,16 @@ engine.mod = function(x, y){ engine.natlog = function(x){ if (x.length > 0){ - return Math.log(x); + let num = ensureNumberSingleton(x); + return Math.log(num); }else{ return []; } }; engine.baselog = function(x, base){ - return (Math.log(x) / Math.log(base)); + let num = ensureNumberSingleton(x); + return (Math.log(num) / Math.log(base)); }; module.exports = engine; diff --git a/test/cases/5.7_math.yaml b/test/cases/5.7_math.yaml index 773e7ed..003cc22 100644 --- a/test/cases/5.7_math.yaml +++ b/test/cases/5.7_math.yaml @@ -10,6 +10,9 @@ tests: - desc: '** Error taking logarithm due to too many input parameters' expression: Math.n3.ln(n4) error: true + - desc: '** Error taking logarithm if the input collection contains multiple items' + expression: Math.arr.ln() + error: true - desc: '5.7.6 log(base : Decimal) : Decimal' - desc: '** Can take the logarithm of the number with a given base' @@ -19,8 +22,11 @@ tests: expression: Math.n2.log(8) result: [] - desc: '** Empty result when taking logarithm with empty base' - expression: Math.n3.log() + expression: Math.n3.log(n2) result: [] + - desc: '** Error taking logarithm if the input collection contains multiple items' + expression: Math.arr.log(16) + error: true subject: resourceType: Math @@ -28,3 +34,4 @@ subject: n2: [] n3: 2 n4: 8 + arr: [3, 5] From 838c1de5b519bb9d857201e0292b69b005975f02 Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 17 May 2019 19:25:23 +0300 Subject: [PATCH 5/7] func names and too many args err test --- src/fhirpath.js | 4 ++-- src/math.js | 18 ++++++++++++------ test/cases/5.7_math.yaml | 7 +++++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/fhirpath.js b/src/fhirpath.js index 33ed494..6f07486 100644 --- a/src/fhirpath.js +++ b/src/fhirpath.js @@ -96,8 +96,8 @@ engine.invocationTable = { replaceMatches: {fn: strings.replaceMatches, arity: {2: ["String", "String"]}}, length: {fn: strings.length }, - ln: {fn: math.natlog}, - log: {fn: math.baselog, arity: {1: ["Number"]}, nullable: true}, + ln: {fn: math.ln}, + log: {fn: math.log, arity: {1: ["Number"]}, nullable: true}, now: {fn: datetime.now }, today: {fn: datetime.today }, diff --git a/src/math.js b/src/math.js index dccff3d..2469281 100644 --- a/src/math.js +++ b/src/math.js @@ -7,10 +7,15 @@ var engine = {}; function ensureNumberSingleton(x){ - if(x.length == 1 && typeof x[0] === "number") { - return x[0]; + if (typeof x !== "number"){ + if (x.length == 1){ + return x[0]; + }else{ + throw new Error("Expected number, but got " + JSON.stringify(x)); + } + }else{ + return x; } - throw new Error("Expected number, but got " + JSON.stringify(x)); } engine.amp = function(x, y){ @@ -53,7 +58,7 @@ engine.mod = function(x, y){ return x % y; }; -engine.natlog = function(x){ +engine.ln = function(x){ if (x.length > 0){ let num = ensureNumberSingleton(x); return Math.log(num); @@ -62,9 +67,10 @@ engine.natlog = function(x){ } }; -engine.baselog = function(x, base){ +engine.log = function(x, base){ let num = ensureNumberSingleton(x); - return (Math.log(num) / Math.log(base)); + let num2 = ensureNumberSingleton(base); + return (Math.log(num) / Math.log(num2)); }; module.exports = engine; diff --git a/test/cases/5.7_math.yaml b/test/cases/5.7_math.yaml index 003cc22..779889b 100644 --- a/test/cases/5.7_math.yaml +++ b/test/cases/5.7_math.yaml @@ -25,9 +25,12 @@ tests: expression: Math.n3.log(n2) result: [] - desc: '** Error taking logarithm if the input collection contains multiple items' - expression: Math.arr.log(16) + expression: Math.arr.log(8) error: true - + - desc: '** Error taking logarithm due to too many input parameters' + expression: Math.n3.log([3, 5]) + error: true + subject: resourceType: Math n1: 1 From d277aa8583c9f156f0f03191b2582fc51c2b844c Mon Sep 17 00:00:00 2001 From: Ivan Date: Mon, 20 May 2019 15:59:42 +0300 Subject: [PATCH 6/7] isEmpty() func --- src/math.js | 25 ++++++++++++++++++------- test/cases/5.7_math.yaml | 1 + 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/math.js b/src/math.js index 2469281..ac58ceb 100644 --- a/src/math.js +++ b/src/math.js @@ -7,7 +7,7 @@ var engine = {}; function ensureNumberSingleton(x){ - if (typeof x !== "number"){ + if (typeof x != 'number'){ if (x.length == 1){ return x[0]; }else{ @@ -18,6 +18,13 @@ function ensureNumberSingleton(x){ } } +function isEmpty(x) { + if(typeof(x) == 'number'){ + return false; + } + return x.length == 0; +} + engine.amp = function(x, y){ return (x || "") + (y || ""); }; @@ -59,18 +66,22 @@ engine.mod = function(x, y){ }; engine.ln = function(x){ - if (x.length > 0){ + if (isEmpty(x)){ + return []; + }else{ let num = ensureNumberSingleton(x); return Math.log(num); - }else{ - return []; } }; engine.log = function(x, base){ - let num = ensureNumberSingleton(x); - let num2 = ensureNumberSingleton(base); - return (Math.log(num) / Math.log(num2)); + if (isEmpty(x) || isEmpty(base)){ + return []; + }else{ + let num = ensureNumberSingleton(x); + let num2 = ensureNumberSingleton(base); + return (Math.log(num) / Math.log(num2)); + } }; module.exports = engine; diff --git a/test/cases/5.7_math.yaml b/test/cases/5.7_math.yaml index 779889b..f77c498 100644 --- a/test/cases/5.7_math.yaml +++ b/test/cases/5.7_math.yaml @@ -38,3 +38,4 @@ subject: n3: 2 n4: 8 arr: [3, 5] + t: true From 367b2310a30dc8ca5a43588a3b32ad2a4ee6f381 Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 21 May 2019 00:58:47 +0300 Subject: [PATCH 7/7] CHANGELOG.md & package.json update --- CHANGELOG.md | 4 ++++ package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6dabbb2..7cfd1a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ This log documents significant changes for each release. This project follows [Semantic Versioning](http://semver.org/). +## [0.13.0] - 2019-05-21 +### Added +- Functions ln() and log() in 5.7 (Math) of the FHIRPath specification. + ## [0.12.2] - 2019-05-15 ### Fixed - Corrected output in the demo website for results containing dates and times. diff --git a/package.json b/package.json index 184bca6..e6de1e3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fhirpath", - "version": "0.12.2", + "version": "0.13.0", "description": "A FHIRPath engine", "main": "src/fhirpath.js", "dependencies": {