2
0

🚸 (number) Avoid parsing numbers starting with 0

This commit is contained in:
Baptiste Arnaud
2024-05-27 10:36:30 +02:00
parent e1f1b58c1c
commit 3e4e7531f6
4 changed files with 69 additions and 16 deletions

View File

@@ -1 +1,4 @@
export const parseNumber = (value: string) => Number(value).toString()
export const parseNumber = (value: string) => {
if (value.startsWith('0')) return value
return parseFloat(value).toString()
}

View File

@@ -12,6 +12,11 @@ export const validateNumber = (
variables: Variable[]
}
) => {
if (inputValue === '') return false
const parsedNumber = Number(inputValue)
if (isNaN(parsedNumber)) return false
const min =
options?.min && typeof options.min === 'string'
? Number(parseVariables(variables)(options.min))
@@ -20,10 +25,8 @@ export const validateNumber = (
options?.min && typeof options.min === 'string'
? Number(parseVariables(variables)(options.min))
: undefined
return (
inputValue !== '' &&
(isNotDefined(min) || Number(inputValue) >= min) &&
(isNotDefined(max) || Number(inputValue) <= max)
(isNotDefined(min) || parsedNumber >= min) &&
(isNotDefined(max) || parsedNumber <= max)
)
}