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

View File

@ -266,21 +266,10 @@ const getOutgoingEdgeId =
reply
) {
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 (
@ -289,7 +278,9 @@ const getOutgoingEdgeId =
reply
) {
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
}