Skip to content

Commit

Permalink
Merge pull request #136 from mechanik-daniel/135-false-positive-isnum…
Browse files Browse the repository at this point in the history
…eric

135 false positive isnumeric
  • Loading branch information
mechanik-daniel authored Dec 24, 2024
2 parents 804f3e3 + 575beb8 commit a876725
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/helpers/jsonataFunctions/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ export const transform = async (input, expression: string, extraBindings: Record
bindings.parseCsv = parseCsv;
bindings.v2parse = v2.v2parse;
bindings.v2json = v2.v2json;
bindings.isNumeric = stringFuncs.isNumeric;
bindings.capabilities = fhirFuncs.capabilities;

const { aliases } = getCache();
Expand Down Expand Up @@ -128,6 +127,7 @@ export const transform = async (input, expression: string, extraBindings: Record
expr.registerFunction('base64decode', stringFuncs.base64decode, '<s-:s>');
expr.registerFunction('startsWith', stringFuncs.startsWith, '<s-s:a>');
expr.registerFunction('endsWith', stringFuncs.endsWith, '<s-s:a>');
expr.registerFunction('isNumeric', stringFuncs.isNumeric, '<j-:a>');

const res = await expr.evaluate(input, bindings);
return res;
Expand Down
17 changes: 10 additions & 7 deletions src/helpers/stringFunctions/stringFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,18 @@ export const matches = (str: string, regex: string) => {
return fn.test(str);
};

export const isNumeric = (n: any) => {
if (typeof n === 'number') {
return true;
};
if (typeof n === 'string') {
return !isNaN(parseFloat(n)) && isFinite(parseFloat(n));
};
export const isNumeric = (n: any): boolean | undefined => {
if (typeof n === 'number') return true;
if (typeof n === 'undefined') return undefined;

// RegEx taken form the FHIR spec for the decimal datatype: https://www.hl7.org/fhir/r4/datatypes.html
const isStrNumeric = (str: string) => /^-?(0|[1-9][0-9]*)(.[0-9]+)?([eE][+-]?[0-9]+)?$/.test(str);

if (typeof n === 'string') return isStrNumeric(n);

if (Array.isArray(n) && n.length === 1) {
return isNumeric(n[0]);
};

return false;
};

0 comments on commit a876725

Please sign in to comment.