From 34cd0e1d8156849467d2c0daab9e826cb46590cb Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Mon, 22 Jul 2024 08:52:42 +0100 Subject: [PATCH] Improve type coercion in table conditions --- .../src/components/grid/stores/conditions.js | 36 ++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/packages/frontend-core/src/components/grid/stores/conditions.js b/packages/frontend-core/src/components/grid/stores/conditions.js index b10342a376..3800f21df1 100644 --- a/packages/frontend-core/src/components/grid/stores/conditions.js +++ b/packages/frontend-core/src/components/grid/stores/conditions.js @@ -69,6 +69,25 @@ export const initialise = context => { }) } +const TypeCoercionMap = { + [FieldType.NUMBER]: parseFloat, + [FieldType.DATETIME]: val => { + if (val) { + return new Date(val).toISOString() + } + return null + }, + [FieldType.BOOLEAN]: val => { + if (`${val}`.toLowerCase().trim() === "true") { + return true + } + if (`${val}`.toLowerCase().trim() === "false") { + return false + } + return null + }, +} + // Evaluates an array of cell conditions against a certain row and returns the // resultant metadata const evaluateConditions = (row, conditions) => { @@ -101,19 +120,10 @@ const evaluateConditions = (row, conditions) => { coercedType = FieldType.NUMBER } } - if (coercedType === FieldType.NUMBER) { - referenceValue = parseFloat(referenceValue) - value = parseFloat(value) - } else if (coercedType === FieldType.DATETIME) { - if (referenceValue) { - referenceValue = new Date(referenceValue).toISOString() - } - if (value) { - value = new Date(value).toISOString() - } - } else if (coercedType === FieldType.BOOLEAN) { - referenceValue = `${referenceValue}`.toLowerCase() === "true" - value = `${value}`.toLowerCase() === "true" + const coerce = TypeCoercionMap[coercedType] + if (coerce) { + value = coerce(value) + referenceValue = coerce(referenceValue) } // Build lucene compatible condition expression