Skip to content

Commit

Permalink
Merge pull request #19 in LFOR/fhirpath.js from bugfix/LF-1490/compar…
Browse files Browse the repository at this point in the history
…ison-of-dates-with-different-precision to master

* commit '0694c11e1cf26b5c363beb981b6014926808e742':
  npm audit fix
  Refactoring of comparison of dates indicated with different level of precision
  Comparison of dates indicated with different level of precision
  • Loading branch information
yuriy-sedinkin committed Oct 19, 2020
2 parents 91eb8d3 + 0694c11 commit 33f6c5b
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 68 deletions.
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/).

## [2.6.2] - 2020-10-09
### Fixed
- Comparison of dates indicated with different level of precision

## [2.6.1] - 2020-09-22
### Fixed
- Module not found errors with TypeScript when importing json files
Expand Down
14 changes: 7 additions & 7 deletions demo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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": "2.6.1",
"version": "2.6.2",
"description": "A FHIRPath engine",
"main": "src/fhirpath.js",
"dependencies": {
Expand Down
40 changes: 24 additions & 16 deletions src/equality.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,34 +80,42 @@ function typecheck(a, b){

engine.lt = function(a, b){
if (!a.length || !b.length) return [];
var vals = typecheck(a,b);
var a0 = vals[0];
var b0 = vals[1];
return a0 instanceof FP_Type ? a0.compare(b0) == -1 : a0 < b0;
const [a0, b0] = typecheck(a,b);
if (a0 instanceof FP_Type) {
const compare = a0.compare(b0);
return compare === null ? [] : compare < 0;
}
return a0 < b0;
};

engine.gt = function(a, b){
if (!a.length || !b.length) return [];
var vals = typecheck(a,b);
var a0 = vals[0];
var b0 = vals[1];
return a0 instanceof FP_Type ? a0.compare(b0) == 1 : a0 > b0;
const [a0, b0] = typecheck(a,b);
if (a0 instanceof FP_Type) {
const compare = a0.compare(b0);
return compare === null ? [] : compare > 0;
}
return a0 > b0;
};

engine.lte = function(a, b){
if (!a.length || !b.length) return [];
var vals = typecheck(a,b);
var a0 = vals[0];
var b0 = vals[1];
return a0 instanceof FP_Type ? a0.compare(b0) <= 0 : a0 <= b0;
const [a0, b0] = typecheck(a,b);
if (a0 instanceof FP_Type) {
const compare = a0.compare(b0);
return compare === null ? [] : compare <= 0;
}
return a0 <= b0;
};

engine.gte = function(a, b){
if (!a.length || !b.length) return [];
var vals = typecheck(a,b);
var a0 = vals[0];
var b0 = vals[1];
return a0 instanceof FP_Type ? a0.compare(b0) >= 0 : a0 >= b0;
const [a0, b0] = typecheck(a,b);
if (a0 instanceof FP_Type) {
const compare = a0.compare(b0);
return compare === null ? [] : compare >= 0;
}
return a0 >= b0;
};


Expand Down
42 changes: 24 additions & 18 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,11 @@ class FP_TimeBase extends FP_Type {


/**
* Returns -1, 0, or 1 if this (date) time is less then, equal to, or greater
* than otherTime. Comparisons are made at the lesser of the two time
* precisions.
* Returns a number less than 0, equal to 0 or greater than 0
* if this (date) time is less than, equal to, or greater than otherTime.
* Comparisons are made at the lesser of the two time precisions.
* @param {FP_TimeBase} otherTime
* @return {number}
*/
compare(otherTime) {
var thisPrecision = this._getPrecision();
Expand All @@ -472,16 +474,20 @@ class FP_TimeBase extends FP_Type {
this._getDateObj().getTime(): this._dateAtPrecision(otherPrecision).getTime();
var otherTimeInt = otherPrecision <= thisPrecision ?
otherTime._getDateObj().getTime(): otherTime._dateAtPrecision(thisPrecision).getTime();
return thisTimeInt < otherTimeInt ?
-1 : thisTimeInt === otherTimeInt ? 0 : 1;
if (thisPrecision !== otherPrecision && thisTimeInt === otherTimeInt) {
return null;
}
return thisTimeInt - otherTimeInt;
}


/**
* Returns a number representing the precision of the time string given to
* the constructor. (Higher means more precise). The number is the number
* of components of the time string (ignoring the time zone) produced by
* matching against the time regular expression.
* matching against the time regular expression, except that milliseconds
* and seconds are counted together as a single of level of precision.
* @return {number}
*/
_getPrecision() {
if (this.precision === undefined)
Expand Down Expand Up @@ -640,7 +646,7 @@ class FP_DateTime extends FP_TimeBase {
* Also sets this.precision.
*/
_getMatchData() {
return super._getMatchData(dateTimeRE, 6);
return super._getMatchData(dateTimeRE, 5);
}

/**
Expand Down Expand Up @@ -696,19 +702,19 @@ class FP_DateTime extends FP_TimeBase {
var hour = thisPrecision > 2 ? parseInt(timeParts[3]) : 0;
var minutes = thisPrecision > 3 ? parseInt(timeParts[4].slice(1)): 0;
var seconds = thisPrecision > 4 ? parseInt(timeParts[5].slice(1)): 0;
var ms = thisPrecision > 5 ? parseInt(timeParts[6].slice(1)): 0;
var ms = timeParts.length > 6 ? parseInt(timeParts[6].slice(1)): 0;
var d = this._createDate(year, month, day, hour, minutes, seconds, ms,
timezoneOffset);
if (precision < this._getPrecision()) {
if (precision < thisPrecision) {
// Adjust the precision
year = d.getFullYear();
month = precision > 0 ? d.getMonth() : 0;
day = precision > 1 ? d.getDate() : 1;
hour = precision > 2 ? d.getHours() : 0;
minutes = precision > 3 ? d.getMinutes(): 0;
seconds = precision > 4 ? d.getSeconds(): 0;
ms = precision > 5 ? d.getMilliseconds(): 0;
d = new Date(year, month, day, hour, minutes, seconds, ms);
// Here the precision will always be less than the maximum
// due to the condition in the if statement: "precision < thisPrecision"
d = new Date(year, month, day, hour, minutes);
}
return d;
}
Expand Down Expand Up @@ -792,7 +798,7 @@ class FP_Time extends FP_TimeBase {
var hour = parseInt(timeParts[0]);
var minutes = thisPrecision > 0 ? parseInt(timeParts[1].slice(1)): 0;
var seconds = thisPrecision > 1 ? parseInt(timeParts[2].slice(1)): 0;
var ms = thisPrecision > 2 ? parseInt(timeParts[3].slice(1)): 0;
var ms = timeParts.length > 3 ? parseInt(timeParts[3].slice(1)): 0;
var d = this._createDate(year, month, day, hour, minutes, seconds, ms,
timezoneOffset);
if (timezoneOffset) {
Expand All @@ -802,13 +808,13 @@ class FP_Time extends FP_TimeBase {
d.setMonth(month);
d.setDate(day);
}
if (precision < this._getPrecision()) {
if (precision < thisPrecision) {
// Adjust the precision
hour = d.getHours();
minutes = precision > 0 ? d.getMinutes(): 0;
seconds = precision > 1 ? d.getSeconds(): 0;
ms = precision > 2 ? d.getMilliseconds(): 0;
d = new Date(year, month, day, hour, minutes, seconds, ms);
// Here the precision will always be less than the maximum
// due to the condition in the if statement: "precision < thisPrecision"
d = new Date(year, month, day, hour, minutes);
}
return d;
}
Expand All @@ -819,7 +825,7 @@ class FP_Time extends FP_TimeBase {
* Also sets this.precision.
*/
_getMatchData() {
return super._getMatchData(timeRE, 3);
return super._getMatchData(timeRE, 2);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions test/cases/6.2_comparision.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ tests:
error: true
- desc: Comparison based on the lesser precision (1)
expression: '@2018-12-31 > @2018'
result: [false]
result: []
- desc: Comparison based on the lesser precision (2)
expression: '@2018 > @2017-01'
result: [true]
Expand Down Expand Up @@ -170,7 +170,7 @@ tests:
result: [false]
- desc: Comparison with differnet precision (1)
expression: '@2018-12-20T12 > @2018-12-20T12:01'
result: [false]
result: []
- desc: Comparison with differnet precision (2)
expression: '@2018-12-20T12 > @2018-12-20T11:01'
result: [true]
Expand All @@ -190,9 +190,9 @@ tests:
expression: '@2018-12-21T12:02+02:00 > dateTime'
result: [false]
- expression: '@2020-08-04T12:34-04:00 < @2020T-04:00'
result: [false]
result: []
- expression: '@2020-08-04T12:34 < @2020'
result: [false]
result: []
- 'group: Time':
- desc: Time comparison; GT (1)
expression: '@T12:03 > @T12:01'
Expand All @@ -205,7 +205,7 @@ tests:
result: [true]
- desc: Time comparison with based on lesser precision (1)
expression: '@T12:01 > @T12'
result: [false]
result: []
- desc: Time comparison with based on lesser precision (2)
expression: '@T12:02:34.324 > @T12:01'
result: [true]
Expand All @@ -214,7 +214,7 @@ tests:
result: [true]
- desc: Time comparison with based on lesser precision (4)
expression: '@T12:02:34.324+05:00 > @T12+05:00'
result: [false]
result: []
- desc: Comparison with later time in resource (1)
expression: '@T17:00-05:00 < timeWithT'
result: [true]
Expand Down
2 changes: 1 addition & 1 deletion test/cases/6.6_math.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ tests:
- expression: "@T09:45:23 + 2 years"
error: true
- expression: "@T09:45:23Z + 2 minutes = @T04:47:23.000-05:00"
result: []
result: [true]
- expression: "@T23:59:23Z + 2 minutes = @T00:01:23Z"
result: [true]
- expression: "@T23:59:23-05:00 + 2 minutes = @T00:01:23-05:00"
Expand Down
18 changes: 0 additions & 18 deletions test/cases/fhir-r4.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1850,13 +1850,11 @@ tests:
expression: '@2012-04-15T15:30:31 = @2012-04-15T15:30:31.0'
result:
- true
disable: true
- desc: '** testEquality22'
inputfile: patient-example.json
expression: '@2012-04-15T15:30:31 = @2012-04-15T15:30:31.1'
result:
- false
disable: true
- desc: '** testEquality23'
inputfile: patient-example.json
expression: '@2012-04-15T15:00:00Z = @2012-04-15T10:00:00'
Expand Down Expand Up @@ -1962,13 +1960,11 @@ tests:
expression: '@2012-04-15T15:30:31 != @2012-04-15T15:30:31.0'
result:
- false
disable: true
- desc: '** testNEquality16'
inputfile: patient-example.json
expression: '@2012-04-15T15:30:31 != @2012-04-15T15:30:31.1'
result:
- true
disable: true
- desc: '** testNEquality17'
inputfile: patient-example.json
expression: '@2012-04-15T15:00:00Z != @2012-04-15T10:00:00'
Expand Down Expand Up @@ -2097,7 +2093,6 @@ tests:
expression: '@2012-04-15T15:30:31 ~ @2012-04-15T15:30:31.0'
result:
- true
disable: true
- desc: '** testEquivalent18'
inputfile: patient-example.json
expression: '@2012-04-15T15:30:31 ~ @2012-04-15T15:30:31.1'
Expand Down Expand Up @@ -2216,7 +2211,6 @@ tests:
expression: '@2012-04-15T15:30:31 !~ @2012-04-15T15:30:31.0'
result:
- false
disable: true
- desc: '** testNotEquivalent18'
inputfile: patient-example.json
expression: '@2012-04-15T15:30:31 !~ @2012-04-15T15:30:31.1'
Expand Down Expand Up @@ -2365,17 +2359,14 @@ tests:
inputfile: patient-example.json
expression: '@2018-03 < @2018-03-01'
result: []
disable: true
- desc: '** testLessThan24'
inputfile: patient-example.json
expression: '@2018-03-01T10 < @2018-03-01T10:30'
result: []
disable: true
- desc: '** testLessThan25'
inputfile: patient-example.json
expression: '@T10 < @T10:30'
result: []
disable: true
- desc: '** testLessThan26'
inputfile: patient-example.json
expression: '@2018-03-01T10:30:00 < @2018-03-01T10:30:00.0'
Expand Down Expand Up @@ -2502,17 +2493,14 @@ tests:
inputfile: patient-example.json
expression: '@2018-03 <= @2018-03-01'
result: []
disable: true
- desc: '** testLessOrEqual24'
inputfile: patient-example.json
expression: '@2018-03-01T10 <= @2018-03-01T10:30'
result: []
disable: true
- desc: '** testLessOrEqual25'
inputfile: patient-example.json
expression: '@T10 <= @T10:30'
result: []
disable: true
- desc: '** testLessOrEqual26'
inputfile: patient-example.json
expression: '@2018-03-01T10:30:00 <= @2018-03-01T10:30:00.0'
Expand Down Expand Up @@ -2639,17 +2627,14 @@ tests:
inputfile: patient-example.json
expression: '@2018-03 >= @2018-03-01'
result: []
disable: true
- desc: '** testGreatorOrEqual24'
inputfile: patient-example.json
expression: '@2018-03-01T10 >= @2018-03-01T10:30'
result: []
disable: true
- desc: '** testGreatorOrEqual25'
inputfile: patient-example.json
expression: '@T10 >= @T10:30'
result: []
disable: true
- desc: '** testGreatorOrEqual26'
inputfile: patient-example.json
expression: '@2018-03-01T10:30:00 >= @2018-03-01T10:30:00.0'
Expand Down Expand Up @@ -2776,17 +2761,14 @@ tests:
inputfile: patient-example.json
expression: '@2018-03 > @2018-03-01'
result: []
disable: true
- desc: '** testGreaterThan24'
inputfile: patient-example.json
expression: '@2018-03-01T10 > @2018-03-01T10:30'
result: []
disable: true
- desc: '** testGreaterThan25'
inputfile: patient-example.json
expression: '@T10 > @T10:30'
result: []
disable: true
- desc: '** testGreaterThan26'
inputfile: patient-example.json
expression: '@2018-03-01T10:30:00 > @2018-03-01T10:30:00.0'
Expand Down

0 comments on commit 33f6c5b

Please sign in to comment.