2
0

🐛 Normalize user inputs before comparing

This commit is contained in:
Baptiste Arnaud
2023-08-08 08:24:15 +02:00
parent a72cb5e20d
commit 2b4ada007d
3 changed files with 66 additions and 27 deletions

View File

@ -41,13 +41,17 @@ const matchComparison = (
switch (comparisonOperator) { switch (comparisonOperator) {
case ComparisonOperators.CONTAINS: { case ComparisonOperators.CONTAINS: {
if (!inputValue || !value) return false if (!inputValue || !value) return false
return inputValue.toLowerCase().includes(value.toLowerCase()) return inputValue
.toLowerCase()
.trim()
.normalize()
.includes(value.toLowerCase().trim().normalize())
} }
case ComparisonOperators.EQUAL: { case ComparisonOperators.EQUAL: {
return inputValue === value return inputValue?.normalize() === value?.normalize()
} }
case ComparisonOperators.NOT_EQUAL: { case ComparisonOperators.NOT_EQUAL: {
return inputValue !== value return inputValue?.normalize() !== value?.normalize()
} }
case ComparisonOperators.GREATER: { case ComparisonOperators.GREATER: {
if (!inputValue || !value) return false if (!inputValue || !value) return false
@ -65,15 +69,27 @@ const matchComparison = (
} }
case ComparisonOperators.STARTS_WITH: { case ComparisonOperators.STARTS_WITH: {
if (!inputValue || !value) return false if (!inputValue || !value) return false
return inputValue.toLowerCase().startsWith(value.toLowerCase()) return inputValue
.toLowerCase()
.trim()
.normalize()
.startsWith(value.toLowerCase().trim().normalize())
} }
case ComparisonOperators.ENDS_WITH: { case ComparisonOperators.ENDS_WITH: {
if (!inputValue || !value) return false if (!inputValue || !value) return false
return inputValue.toLowerCase().endsWith(value.toLowerCase()) return inputValue
.toLowerCase()
.trim()
.normalize()
.endsWith(value.toLowerCase().trim().normalize())
} }
case ComparisonOperators.NOT_CONTAINS: { case ComparisonOperators.NOT_CONTAINS: {
if (!inputValue || !value) return false if (!inputValue || !value) return false
return !inputValue?.toLowerCase().includes(value.toLowerCase()) return !inputValue
?.toLowerCase()
.trim()
.normalize()
.includes(value.toLowerCase().trim().normalize())
} }
} }
} }

View File

@ -32,22 +32,46 @@ const executeComparison =
case ComparisonOperators.CONTAINS: { case ComparisonOperators.CONTAINS: {
const contains = (a: string | null, b: string | null) => { const contains = (a: string | null, b: string | null) => {
if (b === '' || !b || !a) return false if (b === '' || !b || !a) return false
return a.toLowerCase().trim().includes(b.toLowerCase().trim()) return a
.toLowerCase()
.trim()
.normalize()
.includes(b.toLowerCase().trim().normalize())
} }
return compare(contains, inputValue, value, 'some') 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()
.normalize()
.includes(b.toLowerCase().trim().normalize())
} }
return compare(notContains, inputValue, value) return compare(notContains, inputValue, value)
} }
case ComparisonOperators.EQUAL: { case ComparisonOperators.EQUAL: {
return compare((a, b) => a === b, inputValue, value) return compare(
(a, b) => {
if (typeof a === 'string' && typeof b === 'string')
return a.normalize() === b.normalize()
return a === b
},
inputValue,
value
)
} }
case ComparisonOperators.NOT_EQUAL: { case ComparisonOperators.NOT_EQUAL: {
return compare((a, b) => a !== b, inputValue, value) return compare(
(a, b) => {
if (typeof a === 'string' && typeof b === 'string')
return a.normalize() !== b.normalize()
return a !== b
},
inputValue,
value
)
} }
case ComparisonOperators.GREATER: { case ComparisonOperators.GREATER: {
if (isNotDefined(inputValue) || isNotDefined(value)) return false if (isNotDefined(inputValue) || isNotDefined(value)) return false
@ -78,14 +102,22 @@ const executeComparison =
case ComparisonOperators.STARTS_WITH: { case ComparisonOperators.STARTS_WITH: {
const startsWith = (a: string | null, b: string | null) => { const startsWith = (a: string | null, b: string | null) => {
if (b === '' || !b || !a) return false if (b === '' || !b || !a) return false
return a.toLowerCase().trim().startsWith(b.toLowerCase().trim()) return a
.toLowerCase()
.trim()
.normalize()
.startsWith(b.toLowerCase().trim().normalize())
} }
return compare(startsWith, inputValue, value) return compare(startsWith, inputValue, value)
} }
case ComparisonOperators.ENDS_WITH: { case ComparisonOperators.ENDS_WITH: {
const endsWith = (a: string | null, b: string | null) => { const endsWith = (a: string | null, b: string | null) => {
if (b === '' || !b || !a) return false if (b === '' || !b || !a) return false
return a.toLowerCase().trim().endsWith(b.toLowerCase().trim()) return a
.toLowerCase()
.trim()
.normalize()
.endsWith(b.toLowerCase().trim().normalize())
} }
return compare(endsWith, inputValue, value) return compare(endsWith, inputValue, value)
} }

View File

@ -266,21 +266,10 @@ const getOutgoingEdgeId =
reply reply
) { ) {
const matchedItem = block.items.find( const matchedItem = block.items.find(
(item) => parseVariables(variables)(item.content) === reply (item) =>
parseVariables(variables)(item.content).normalize() ===
reply.normalize()
) )
// TEMPORARY LOG
if (block.id === 'n62dzuz2ye73tivnjw7bnxd5') {
console.log(
'Debug Safari bug:',
JSON.stringify({
items: block.items.map((item) =>
parseVariables(variables)(item.content)
),
reply,
})
)
}
if (matchedItem?.outgoingEdgeId) return matchedItem.outgoingEdgeId if (matchedItem?.outgoingEdgeId) return matchedItem.outgoingEdgeId
} }
if ( if (
@ -289,7 +278,9 @@ const getOutgoingEdgeId =
reply reply
) { ) {
const matchedItem = block.items.find( const matchedItem = block.items.find(
(item) => parseVariables(variables)(item.title) === reply (item) =>
parseVariables(variables)(item.title).normalize() ===
reply.normalize()
) )
if (matchedItem?.outgoingEdgeId) return matchedItem.outgoingEdgeId if (matchedItem?.outgoingEdgeId) return matchedItem.outgoingEdgeId
} }