-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat+refactor: Move extraction functions to a separate utils file. Im…
…prove robustness of extraction logic (convert objects to strings, detect if inputs are numeric or dates). Exclude strings from 'grater/smaller'-comparisons. Include date comparison for date-strings following ISO, RFC2822, or HTTP patterns.
- Loading branch information
Showing
2 changed files
with
178 additions
and
42 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,136 @@ | ||
import { DateTime } from "luxon"; | ||
|
||
function getOriginalValueAsString(v) { | ||
if (typeof v !== 'string') { | ||
return JSON.stringify(v); | ||
} else { | ||
return v; | ||
} | ||
} | ||
|
||
function isNumeric(v){ | ||
if (typeof v === 'number') return true; | ||
if (typeof v != 'string') return false; | ||
return !isNaN(v) && !isNaN(parseFloat(v)); | ||
} | ||
|
||
function bothDates(entryValue, comparisonValue) { | ||
try { | ||
if (DateTime.fromISO(entryValue).isValid && DateTime.fromISO(comparisonValue).isValid ) { | ||
// e.g., "2016-05-25T092415.123", "2016-05-25" | ||
return true; | ||
|
||
} else if (DateTime.fromRFC2822(entryValue).isValid && DateTime.fromRFC2822(comparisonValue).isValid ) { | ||
// e.g., "Tue, 01 Nov 2016 13:23:12 +0630" | ||
return true; | ||
|
||
} else if (DateTime.fromHTTP(entryValue).isValid && DateTime.fromHTTP(comparisonValue).isValid ) { | ||
// e.g., "Sunday, 06-Nov-94 08:49:37 GMT" | ||
return true; | ||
|
||
} else { | ||
return false; | ||
} | ||
} | ||
catch(err) { | ||
return false; | ||
} | ||
} | ||
|
||
function getDates(entryValue, comparisonValue) { | ||
if (DateTime.fromISO(entryValue).isValid && DateTime.fromISO(comparisonValue).isValid) { | ||
return [DateTime.fromISO(entryValue), DateTime.fromISO(comparisonValue)]; | ||
|
||
} else if (DateTime.fromRFC2822(entryValue).isValid && DateTime.fromRFC2822(comparisonValue).isValid) { | ||
return [DateTime.fromRFC2822(entryValue), DateTime.fromRFC2822(comparisonValue)]; | ||
|
||
} else if (DateTime.fromHTTP(entryValue).isValid && DateTime.fromHTTP(comparisonValue).isValid) { | ||
return [DateTime.fromHTTP(entryValue), DateTime.fromHTTP(comparisonValue)]; | ||
|
||
} else { | ||
return [null, null] | ||
} | ||
} | ||
|
||
function prepareValues(entryValue, comparisonValue) { | ||
if (isNumeric(entryValue) && isNumeric(comparisonValue)) { | ||
entryValue = parseFloat(entryValue); | ||
comparisonValue = parseFloat(comparisonValue); | ||
} else if (bothDates(entryValue, comparisonValue)) { | ||
[entryValue, comparisonValue] = getDates(entryValue, comparisonValue) | ||
} else { | ||
entryValue = getOriginalValueAsString(entryValue); | ||
comparisonValue = getOriginalValueAsString(comparisonValue); | ||
} | ||
return [entryValue, comparisonValue]; | ||
} | ||
|
||
export function valueIsEqual(entryValue, comparisonValue) { | ||
entryValue = getOriginalValueAsString(entryValue); | ||
comparisonValue = getOriginalValueAsString(comparisonValue); | ||
return entryValue === comparisonValue; | ||
} | ||
|
||
export function valueIsNotEqual(entryValue, comparisonValue) { | ||
entryValue = getOriginalValueAsString(entryValue); | ||
comparisonValue = getOriginalValueAsString(comparisonValue); | ||
return entryValue !== comparisonValue; | ||
} | ||
|
||
export function valueIsSmallerOrEqual(entryValue, comparisonValue) { | ||
if (bothDates(entryValue, comparisonValue) || | ||
(isNumeric(entryValue) && isNumeric(comparisonValue))) { | ||
[entryValue, comparisonValue] = prepareValues(entryValue, comparisonValue); | ||
return entryValue <= comparisonValue; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
export function valueIsGreaterOrEqual(entryValue, comparisonValue) { | ||
if (bothDates(entryValue, comparisonValue) || | ||
(isNumeric(entryValue) && isNumeric(comparisonValue))) { | ||
[entryValue, comparisonValue] = prepareValues(entryValue, comparisonValue); | ||
return entryValue >= comparisonValue; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
export function valueIsSmaller(entryValue, comparisonValue) { | ||
if (bothDates(entryValue, comparisonValue) || | ||
(isNumeric(entryValue) && isNumeric(comparisonValue))) { | ||
[entryValue, comparisonValue] = prepareValues(entryValue, comparisonValue); | ||
return entryValue < comparisonValue; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
export function valueIsGreater(entryValue, comparisonValue) { | ||
if (bothDates(entryValue, comparisonValue) || | ||
(isNumeric(entryValue) && isNumeric(comparisonValue))) { | ||
[entryValue, comparisonValue] = prepareValues(entryValue, comparisonValue); | ||
return entryValue > comparisonValue; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
export function regexDeleteMatch(entryValue, comparisonValue) { | ||
let originalValue = getOriginalValueAsString(entryValue); | ||
let comparisonRegExp = RegExp(comparisonValue, 'g'); | ||
return originalValue.replaceAll(comparisonRegExp, ''); | ||
} | ||
|
||
export function regexReplaceMatch(entryValue, comparisonValue, replacementValue) { | ||
let originalValue = getOriginalValueAsString(entryValue); | ||
let comparisonRegExp = RegExp(comparisonValue, 'g'); | ||
return originalValue.replaceAll(comparisonRegExp, replacementValue); | ||
} | ||
|
||
export function regexDeleteRow(entryValue, comparisonValue) { | ||
let originalValue = getOriginalValueAsString(entryValue); | ||
let comparisonRegExp = RegExp(comparisonValue, 'g'); | ||
return comparisonRegExp.test(originalValue); | ||
} |