Skip to content

Commit

Permalink
Fix filter conditions with decimal values
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwroc committed Jan 4, 2024
1 parent 8df014a commit d9890d5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/filter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getRegexFromString, log } from "./utils";
import { getRegexFromString, log, toNumber } from "./utils";

/**
* Functions to check if filter condition is met
Expand All @@ -7,11 +7,11 @@ const operatorHandlers: { [key in FilterOperator]: (val: string | number | undef
"exists": val => val !== undefined,
"not_exists": val => val === undefined,
"contains": (val, searchString) => val !== undefined && val.toString().indexOf(searchString!.toString()) != -1,
"=": (val, expectedVal) => val == expectedVal,
">": (val, expectedVal) => Number(val) > Number(expectedVal),
"<": (val, expectedVal) => Number(val) < Number(expectedVal),
">=": (val, expectedVal) => Number(val) >= Number(expectedVal),
"<=": (val, expectedVal) => Number(val) <= Number(expectedVal),
"=": (val, expectedVal) => toNumber(val) == toNumber(expectedVal),
">": (val, expectedVal) => toNumber(val) > toNumber(expectedVal),
"<": (val, expectedVal) => toNumber(val) < toNumber(expectedVal),
">=": (val, expectedVal) => toNumber(val) >= toNumber(expectedVal),
"<=": (val, expectedVal) => toNumber(val) <= toNumber(expectedVal),
"matches": (val, pattern) => {
if (val === undefined) {
return false;
Expand Down
24 changes: 21 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,28 @@ export const log = (message: string, level: "warn" | "error" = "warn") => {
* Checks whether given value is a number
* @param val String value to check
*/
export const isNumber = (value: string | number): boolean =>
{
return (value!== undefined && value !== null && value !== '' && !isNaN(Number(value)))
export const isNumber = (value: string | number | undefined): boolean => {
if (value === undefined) {
return false;
}

if (typeof(value) == "string") {
// trying to solve decimal number formatting in some langs
value = value.replace(",", ".");
}

return (value!== undefined && value !== null && value !== '' && !isNaN(Number(value)))
}

export const toNumber = (value: string | number | undefined): number => {
if (typeof(value) == "string") {
// trying to solve decimal number formatting in some langs
value = value.replace(",", ".");
}

return Number(value);
}

/**
* Returns array of values regardles if given value is string array or null
* @param val Value to process
Expand Down
7 changes: 7 additions & 0 deletions test/other/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ describe("Filter", () => {
["45", <FilterOperator>"<=", "46", true],
["some longer text", <FilterOperator>"contains", "longer", true],
["some longer text", <FilterOperator>"contains", "loonger", false],
// decimals
["45.0", <FilterOperator>"=", "45", true],
["45,0", <FilterOperator>"=", "45", true],
["44.1", <FilterOperator>">", "44", true],
["44,1", <FilterOperator>">", "44", true],
["44", <FilterOperator>"<", "44.1", true],
["44", <FilterOperator>"<", "44,1", true],
])("matching functions return correct results", (state: string | undefined, operator: FilterOperator | undefined, value: string | number, expectedIsVlid: boolean) => {
const hassMock = new HomeAssistantMock();

Expand Down

0 comments on commit d9890d5

Please sign in to comment.