From d9890d5732f8a7fb6065dc34902ab009eed988c5 Mon Sep 17 00:00:00 2001 From: Max Chodorowski Date: Thu, 4 Jan 2024 22:34:47 +0000 Subject: [PATCH] Fix filter conditions with decimal values --- src/filter.ts | 12 ++++++------ src/utils.ts | 24 +++++++++++++++++++++--- test/other/filter.test.ts | 7 +++++++ 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/filter.ts b/src/filter.ts index 4c582a5c..ae03c872 100644 --- a/src/filter.ts +++ b/src/filter.ts @@ -1,4 +1,4 @@ -import { getRegexFromString, log } from "./utils"; +import { getRegexFromString, log, toNumber } from "./utils"; /** * Functions to check if filter condition is met @@ -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; diff --git a/src/utils.ts b/src/utils.ts index f1f7741a..5e582e03 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 diff --git a/test/other/filter.test.ts b/test/other/filter.test.ts index 99ec0f2d..c04f17c2 100644 --- a/test/other/filter.test.ts +++ b/test/other/filter.test.ts @@ -98,6 +98,13 @@ describe("Filter", () => { ["45", "<=", "46", true], ["some longer text", "contains", "longer", true], ["some longer text", "contains", "loonger", false], + // decimals + ["45.0", "=", "45", true], + ["45,0", "=", "45", true], + ["44.1", ">", "44", true], + ["44,1", ">", "44", true], + ["44", "<", "44.1", true], + ["44", "<", "44,1", true], ])("matching functions return correct results", (state: string | undefined, operator: FilterOperator | undefined, value: string | number, expectedIsVlid: boolean) => { const hassMock = new HomeAssistantMock();