This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support number and quantity type parameters
- Loading branch information
Showing
9 changed files
with
619 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import each from 'jest-each'; | ||
import { InvalidSearchParameterError } from 'fhir-works-on-aws-interface'; | ||
import { parseNumber } from './number'; | ||
|
||
describe('parseNumber', () => { | ||
describe('valid inputs', () => { | ||
each([ | ||
['-100', 5e-1], | ||
['100', 5e-1], | ||
['10.0', 5e-2], | ||
['1e2', 5e1], | ||
['1.0e2', 5], | ||
['1.0E2', 5], | ||
['5.4', 5e-2], | ||
['5.40e-3', 5e-6], | ||
['5.4e-3', 5e-5], | ||
// Below cases are not truly scientific notation but we allow them since they are valid numbers. | ||
['10e1', 5], | ||
['100e0', 5e-1], | ||
['54.0e-4', 5e-6], | ||
]).test('%s', (string: string, delta: number) => { | ||
const result = parseNumber(string); | ||
expect(result.number).toBeCloseTo(Number(string), 7); | ||
expect(result.implicitRange.end).toBeCloseTo(Number(string) + delta, 8); | ||
expect(result.implicitRange.start).toBeCloseTo(Number(string) - delta, 8); | ||
}); | ||
}); | ||
|
||
describe('invalid inputs', () => { | ||
each([ | ||
['not a number at all'], | ||
[' 100 '], | ||
['1.2.3'], | ||
['1,2'], | ||
['+-1'], | ||
['1+1'], | ||
['1.3e12xx'], | ||
['1.3a12'], | ||
]).test('%s', (string: string) => { | ||
expect(() => parseNumber(string)).toThrow(InvalidSearchParameterError); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
import { InvalidSearchParameterError } from 'fhir-works-on-aws-interface'; | ||
|
||
interface FhirNumber { | ||
number: number; | ||
implicitRange: { | ||
start: number; | ||
end: number; | ||
}; | ||
} | ||
|
||
const NUMBER_REGEX = /^(?<sign>[+-])?(?<whole>\d+)(\.(?<decimals>\d+))?([eE](?<exp>[+-]?\d+))?$/; | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export const parseNumber = (numberString: string): FhirNumber => { | ||
const match = numberString.match(NUMBER_REGEX); | ||
if (match === null) { | ||
throw new InvalidSearchParameterError(`Invalid number in search parameter: ${numberString}`); | ||
} | ||
|
||
const { decimals = '', exp } = match.groups!; | ||
|
||
let significantFiguresDeltaExp = 0; | ||
// FHIR considers ALL written digits to be significant figures | ||
// e.g. 100 has 3 significant figures (this is contrary to the more common definition where trailing zeroes are NOT significant figures) | ||
// See: https://www.hl7.org/fhir/search.html#number | ||
if (exp !== undefined) { | ||
significantFiguresDeltaExp = Number(exp) - (decimals.length + 1); | ||
} else { | ||
significantFiguresDeltaExp = -(decimals.length + 1); | ||
} | ||
|
||
const numberValue = Number(numberString); | ||
return { | ||
number: numberValue, | ||
implicitRange: { | ||
start: numberValue - 5 * 10 ** significantFiguresDeltaExp, | ||
end: numberValue + 5 * 10 ** significantFiguresDeltaExp, | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { InvalidSearchParameterError } from 'fhir-works-on-aws-interface'; | ||
|
||
interface Range { | ||
start: Date | number; | ||
end: Date | number; | ||
} | ||
|
||
interface NumberRange { | ||
start: number; | ||
end: number; | ||
} | ||
|
||
interface DateRange { | ||
start: Date; | ||
end: Date; | ||
} | ||
|
||
const prefixRange = (prefix: string, range: Range, path: string): any => { | ||
const { start, end } = range; | ||
|
||
// See https://www.hl7.org/fhir/search.html#prefix | ||
if (prefix !== 'ne') { | ||
let elasticSearchRange; | ||
switch (prefix) { | ||
case 'eq': // equal | ||
elasticSearchRange = { | ||
gte: start, | ||
lte: end, | ||
}; | ||
break; | ||
case 'lt': // less than | ||
elasticSearchRange = { | ||
lt: end, | ||
}; | ||
break; | ||
case 'le': // less or equal | ||
elasticSearchRange = { | ||
lte: end, | ||
}; | ||
break; | ||
case 'gt': // greater than | ||
elasticSearchRange = { | ||
gt: start, | ||
}; | ||
break; | ||
case 'ge': // greater or equal | ||
elasticSearchRange = { | ||
gte: start, | ||
}; | ||
break; | ||
case 'sa': // starts after | ||
elasticSearchRange = { | ||
gt: end, | ||
}; | ||
break; | ||
case 'eb': // ends before | ||
elasticSearchRange = { | ||
lt: start, | ||
}; | ||
break; | ||
case 'ap': // approximately | ||
throw new InvalidSearchParameterError('Unsupported prefix: ap'); | ||
default: | ||
// this should never happen | ||
throw new Error(`unknown search prefix: ${prefix}`); | ||
} | ||
|
||
return { | ||
range: { | ||
[path]: elasticSearchRange, | ||
}, | ||
}; | ||
} | ||
|
||
// ne prefix is the only case that requires a bool query; | ||
const neQuery = { | ||
bool: { | ||
should: [ | ||
{ | ||
range: { | ||
[path]: { | ||
gt: end, | ||
}, | ||
}, | ||
}, | ||
{ | ||
range: { | ||
[path]: { | ||
lt: start, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
return neQuery; | ||
}; | ||
|
||
export const prefixRangeNumber = (prefix: string, number: number, implicitRange: NumberRange, path: string): any => { | ||
if (prefix === 'eq' || prefix === 'ne') { | ||
return prefixRange(prefix, implicitRange, path); | ||
} | ||
// When a comparison prefix in the set lgt, lt, ge, le, sa & eb is provided, the implicit precision of the number is ignored, | ||
// and they are treated as if they have arbitrarily high precision | ||
// https://www.hl7.org/fhir/search.html#number | ||
return prefixRange( | ||
prefix, | ||
{ | ||
start: number, | ||
end: number, | ||
}, | ||
path, | ||
); | ||
}; | ||
|
||
export const prefixRangeDate = (prefix: string, range: DateRange, path: string): any => { | ||
return prefixRange(prefix, range, path); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.