Skip to content

Commit

Permalink
Implements FHIRPath string join (stu) (medplum#4683)
Browse files Browse the repository at this point in the history
* Implements FHIRPath string join (stu)

* Update packages/core/src/fhirpath/functions.ts

Co-authored-by: Matt Long <[email protected]>

---------

Co-authored-by: Matt Long <[email protected]>
  • Loading branch information
codyebberson and mattlong committed Jun 18, 2024
1 parent c42ca52 commit b33716e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/fhirpath/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class FhirPathAtom implements Atom {
return this.child.eval(context, []);
}
} catch (error) {
throw new Error(`FhirPathError on "${this.original}": ${error}`);
throw new Error(`FhirPathError on "${this.original}": ${error}`, { cause: error });
}
}

Expand Down
20 changes: 20 additions & 0 deletions packages/core/src/fhirpath/functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,26 @@ describe('FHIRPath functions', () => {
expect(functions.toChars(context, [toTypedValue('xyz')])).toEqual([TYPED_X, TYPED_Y, TYPED_Z]);
});

// Additional string functions
// STU Note: the contents of this section are Standard for Trial Use (STU)

test('join', () => {
expect(functions.join(context, [toTypedValue('')])).toEqual([toTypedValue('')]);
expect(functions.join(context, [toTypedValue('a'), toTypedValue('b'), toTypedValue('c')])).toEqual([
toTypedValue('abc'),
]);
expect(
functions.join(
context,
[toTypedValue('a'), toTypedValue('b'), toTypedValue('c')],
new LiteralAtom(toTypedValue(','))
)
).toEqual([toTypedValue('a,b,c')]);
expect(() => functions.join(context, [toTypedValue('')], new LiteralAtom(toTypedValue(1)))).toThrow(
'Separator must be a string'
);
});

// 5.7. Math

test('abs', () => {
Expand Down
35 changes: 35 additions & 0 deletions packages/core/src/fhirpath/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,41 @@ export const functions: Record<string, FhirPathFunction> = {
return applyStringFunc((str) => (str ? str.split('') : undefined), context, input);
},

/*
* Additional string functions
* See: https://build.fhir.org/ig/HL7/FHIRPath/#additional-string-functions
* STU Note: the contents of this section are Standard for Trial Use (STU)
*/

encode: stub,
decode: stub,
escape: stub,
unescape: stub,
trim: stub,
split: stub,

/**
* The join function takes a collection of strings and joins them into a single string, optionally using the given separator.
*
* If the input is empty, the result is empty.
*
* If no separator is specified, the strings are directly concatenated.
*
* See: https://build.fhir.org/ig/HL7/FHIRPath/#joinseparator-string--string
*
* @param context - The evaluation context.
* @param input - The input collection.
* @param separatorAtom - Optional separator atom.
* @returns The joined string.
*/
join: (context: AtomContext, input: TypedValue[], separatorAtom: Atom): TypedValue[] => {
const separator = separatorAtom?.eval(context, getRootInput(context))[0]?.value ?? '';
if (typeof separator !== 'string') {
throw new Error('Separator must be a string.');
}
return [{ type: PropertyType.string, value: input.map((i) => i.value?.toString() ?? '').join(separator) }];
},

/*
* 5.7. Math
*/
Expand Down

0 comments on commit b33716e

Please sign in to comment.