Improve type coercion in table conditions

This commit is contained in:
Andrew Kingston 2024-07-22 08:52:42 +01:00
parent 10b0e46e4d
commit 34cd0e1d81
No known key found for this signature in database
1 changed files with 23 additions and 13 deletions

View File

@ -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 // Evaluates an array of cell conditions against a certain row and returns the
// resultant metadata // resultant metadata
const evaluateConditions = (row, conditions) => { const evaluateConditions = (row, conditions) => {
@ -101,19 +120,10 @@ const evaluateConditions = (row, conditions) => {
coercedType = FieldType.NUMBER coercedType = FieldType.NUMBER
} }
} }
if (coercedType === FieldType.NUMBER) { const coerce = TypeCoercionMap[coercedType]
referenceValue = parseFloat(referenceValue) if (coerce) {
value = parseFloat(value) value = coerce(value)
} else if (coercedType === FieldType.DATETIME) { referenceValue = coerce(referenceValue)
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"
} }
// Build lucene compatible condition expression // Build lucene compatible condition expression