Skip to content

Commit

Permalink
Merge pull request #9 from Virnkord/math
Browse files Browse the repository at this point in the history
5.7 Math functions ln() and log()
  • Loading branch information
plynchnlm authored May 20, 2019
2 parents cbba9a8 + 367b231 commit ce8e243
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fhirpath",
"version": "0.12.2",
"version": "0.13.0",
"description": "A FHIRPath engine",
"main": "src/fhirpath.js",
"dependencies": {
Expand Down
3 changes: 3 additions & 0 deletions src/fhirpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ engine.invocationTable = {
replaceMatches: {fn: strings.replaceMatches, arity: {2: ["String", "String"]}},
length: {fn: strings.length },

ln: {fn: math.ln},
log: {fn: math.log, arity: {1: ["Number"]}, nullable: true},

now: {fn: datetime.now },
today: {fn: datetime.today },

Expand Down
37 changes: 37 additions & 0 deletions src/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@

var engine = {};

function ensureNumberSingleton(x){
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;
}
}

function isEmpty(x) {
if(typeof(x) == 'number'){
return false;
}
return x.length == 0;
}

engine.amp = function(x, y){
return (x || "") + (y || "");
};
Expand Down Expand Up @@ -46,5 +65,23 @@ engine.mod = function(x, y){
return x % y;
};

engine.ln = function(x){
if (isEmpty(x)){
return [];
}else{
let num = ensureNumberSingleton(x);
return Math.log(num);
}
};

engine.log = function(x, base){
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;
41 changes: 41 additions & 0 deletions test/cases/5.7_math.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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
- 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'
expression: Math.n4.log(2)
result: [3]
- 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(n2)
result: []
- desc: '** Error taking logarithm if the input collection contains multiple items'
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
n2: []
n3: 2
n4: 8
arr: [3, 5]
t: true
File renamed without changes.
File renamed without changes.

0 comments on commit ce8e243

Please sign in to comment.