2
0

🐛 (condition) Improve comparison on dates

This commit is contained in:
Baptiste Arnaud
2023-06-27 09:18:36 +02:00
parent 3575fef34f
commit 92f7f3cbe2

View File

@ -34,41 +34,39 @@ const executeComparison =
if (b === '' || !b || !a) return false if (b === '' || !b || !a) return false
return a.toLowerCase().trim().includes(b.toLowerCase().trim()) return a.toLowerCase().trim().includes(b.toLowerCase().trim())
} }
return compare(contains, inputValue, value) return compare(contains, inputValue, value, 'some')
} }
case ComparisonOperators.NOT_CONTAINS: { case ComparisonOperators.NOT_CONTAINS: {
const notContains = (a: string | null, b: string | null) => { const notContains = (a: string | null, b: string | null) => {
if (b === '' || !b || !a) return true if (b === '' || !b || !a) return true
return !a.toLowerCase().trim().includes(b.toLowerCase().trim()) return !a.toLowerCase().trim().includes(b.toLowerCase().trim())
} }
return compare(notContains, inputValue, value, true) return compare(notContains, inputValue, value)
} }
case ComparisonOperators.EQUAL: { case ComparisonOperators.EQUAL: {
return compare((a, b) => a === b, inputValue, value) return compare((a, b) => a === b, inputValue, value)
} }
case ComparisonOperators.NOT_EQUAL: { case ComparisonOperators.NOT_EQUAL: {
return compare((a, b) => a !== b, inputValue, value, true) return compare((a, b) => a !== b, inputValue, value)
} }
case ComparisonOperators.GREATER: { case ComparisonOperators.GREATER: {
if (isNotDefined(inputValue) || isNotDefined(value)) return false if (isNotDefined(inputValue) || isNotDefined(value)) return false
if (typeof inputValue === 'string') { if (typeof inputValue === 'string') {
if (typeof value === 'string') if (typeof value === 'string')
return parseFloat(inputValue) > parseFloat(value) return parseDateOrNumber(inputValue) > parseDateOrNumber(value)
return parseFloat(inputValue) > value.length return Number(inputValue) > value.length
} }
if (typeof value === 'string') if (typeof value === 'string') return inputValue.length > Number(value)
return inputValue.length > parseFloat(value)
return inputValue.length > value.length return inputValue.length > value.length
} }
case ComparisonOperators.LESS: { case ComparisonOperators.LESS: {
if (isNotDefined(inputValue) || isNotDefined(value)) return false if (isNotDefined(inputValue) || isNotDefined(value)) return false
if (typeof inputValue === 'string') { if (typeof inputValue === 'string') {
if (typeof value === 'string') if (typeof value === 'string')
return parseFloat(inputValue) < parseFloat(value) return parseDateOrNumber(inputValue) < parseDateOrNumber(value)
return parseFloat(inputValue) < value.length return Number(inputValue) < value.length
} }
if (typeof value === 'string') if (typeof value === 'string') return inputValue.length < Number(value)
return inputValue.length < parseFloat(value)
return inputValue.length < value.length return inputValue.length < value.length
} }
case ComparisonOperators.IS_SET: { case ComparisonOperators.IS_SET: {
@ -98,20 +96,29 @@ const compare = (
compareStrings: (a: string | null, b: string | null) => boolean, compareStrings: (a: string | null, b: string | null) => boolean,
a: Exclude<Variable['value'], undefined>, a: Exclude<Variable['value'], undefined>,
b: Exclude<Variable['value'], undefined>, b: Exclude<Variable['value'], undefined>,
defaultReturnValue = false type: 'every' | 'some' = 'every'
): boolean => { ): boolean => {
if (!a || typeof a === 'string') { if (!a || typeof a === 'string') {
if (!b || typeof b === 'string') return compareStrings(a, b) if (!b || typeof b === 'string') return compareStrings(a, b)
return defaultReturnValue === true return type === 'every'
? b.every((b) => compareStrings(a, b)) ? b.every((b) => compareStrings(a, b))
: b.some((b) => compareStrings(a, b)) : b.some((b) => compareStrings(a, b))
} }
if (!b || typeof b === 'string') { if (!b || typeof b === 'string') {
return defaultReturnValue === true return type === 'every'
? a.every((a) => compareStrings(a, b)) ? a.every((a) => compareStrings(a, b))
: a.some((a) => compareStrings(a, b)) : a.some((a) => compareStrings(a, b))
} }
if (defaultReturnValue === true) if (type === 'every')
return a.every((a) => b.every((b) => compareStrings(a, b))) return a.every((a) => b.every((b) => compareStrings(a, b)))
return a.some((a) => b.some((b) => compareStrings(a, b))) return a.some((a) => b.some((b) => compareStrings(a, b)))
} }
const parseDateOrNumber = (value: string): number => {
const parsed = Number(value)
if (isNaN(parsed)) {
const time = Date.parse(value)
return time
}
return parsed
}