Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements lowBoundary and highBoundary #229

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions sof-js/src/path.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { default as fhirpath } from 'fhirpath'
// @note: These are not exported by main export, but could be because they are useful
import { FP_Date, FP_DateTime, FP_Time, timeRE, dateTimeRE } from 'fhirpath/src/types';

// @note this is not exported by fhirpath/src/types but should be
let dateRE = new RegExp(
'^[0-9][0-9][0-9][0-9](-[0-9][0-9](-[0-9][0-9])?)?$');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to use FP_Date.checkString for this.

There are also checkString functions for FP_DateTime and FP_Time.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yngwarr I have updated the pull request to use a model with fhirpath.evaluate to make this much easier.

Now we get TypeInfo correctly with dateTime, date, and time.


const identity = (ctx, v) => [v]

Expand All @@ -24,6 +30,81 @@ function getReferenceKey(nodes, opts) {
})
}

function lowBoundary(nodes) {
return nodes.flatMap((node) => {
if (node == null) {
return null;
}
if (node.match(timeRE)) {
const picoSeconds = (node.split(".")[1] || "").padEnd(9, 0);
const time = new FP_Time(node.split(".")[0])._dateAtPrecision(2);
return time.toISOString().split("T")[1].slice(0, -4).concat(picoSeconds);
}
// @note for some examples of Dates and DateTimes, both are matched by this regex
// else if(node.match(dateRE)) {
// let [year, month, day] = node.split('-');
// if (!day) {
// day = '01'
// }
// if (!month) {
// month = '01'
// }
// return `${year}-${month}-${day}`;
// }
// if (node.match(dateTimeRE)) {
// const picoSeconds = (node.split(".")[1] || "").padEnd(9, 0);
// const hasTimeZone = (node.split('-').length == 4 || node.includes('+'));
// const dateTime = new FP_DateTime(node.split(".")[0])._dateAtPrecision(5);
// const date = dateTime.toISOString().split("T")[0];
// const time = dateTime.toISOString().split("T")[1].slice(0, -4).concat(picoSeconds);
// if (hasTimeZone) {
// //let timeZone =

// } else {
// time.concat("+14:00")
// }
// return date.concat("T", time);
// }
return [node];
})
}

function highBoundary(nodes) {
return nodes.flatMap((node) => {
if (node == null) {
return null;
}
if (node.match(timeRE)) {
const hasSeconds = node.split(":").length == 3;
const picoSeconds = (node.split(".")[1] || "").padEnd(9, 9);
const time = new FP_Time(node.split(".")[0])._dateAtPrecision(2);
if (!hasSeconds) time.setSeconds(59);
return time.toISOString().split("T")[1].slice(0, -4).concat(picoSeconds);
}
// @note for some examples of Dates and DateTimes, both are matched by this regex
// else if (node.match(dateRE)) {
// let [year, month, day] = node.split('-');
// if (!month) {
// month = '12';
// day = '31';
// }
// if (!day) {
// if (month == "02") {
// (year % 4) ? day = '28' : day = '29';
// }
// else if (["04", "06", "09", "11"].includes(month)) {
// day = '30';
// }
// else {
// day = '31';
// }
// }
// return `${year}-${month}-${day}`;
// }
return [node];
})
}

function ofType(ctx, nodes, a1, a2, a3) {
console.log('of type nodes', nodes, a1, a2, a3)
return 'ups'
Expand Down Expand Up @@ -52,6 +133,8 @@ let fhirpath_options = {
getResourceKey: { fn: getResourceKey, arity: { 0: [] } },
getReferenceKey: { fn: getReferenceKey, arity: { 0: [], 1: ['TypeSpecifier'] } },
identity: { fn: (nodes) => nodes, arity: { 0: [] } },
lowBoundary: { fn: lowBoundary, arity: { 0: [] }, nullable: true},
highBoundary: { fn: highBoundary, arity: { 0: [] }, nullable: true},
}
}

Expand Down
Loading