From 7bcf4c61cacfaec53e106552c2907d98bd51b68f Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sun, 17 Aug 2025 15:06:56 +0200 Subject: [PATCH] refactor: improve integer param --- .../matchers/param-parsers/numbers.ts | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/packages/router/src/experimental/route-resolver/matchers/param-parsers/numbers.ts b/packages/router/src/experimental/route-resolver/matchers/param-parsers/numbers.ts index 56896200..4ee079be 100644 --- a/packages/router/src/experimental/route-resolver/matchers/param-parsers/numbers.ts +++ b/packages/router/src/experimental/route-resolver/matchers/param-parsers/numbers.ts @@ -1,41 +1,34 @@ import { miss } from '../errors' import { ParamParser } from './types' -/** - * @internal - */ -const IS_INTEGER_RE = /^-?\d+$/ - export const PARAM_INTEGER_SINGLE = { get: (value: string) => { - if (IS_INTEGER_RE.test(value)) { - const num = Number(value) - if (Number.isFinite(num)) { - return num - } + const num = Number(value) + if (Number.isInteger(num)) { + return num } throw miss() }, set: (value: number) => String(value), } satisfies ParamParser -export const PARAM_NUMBER_OPTIONAL = { +export const PARAM_INTEGER_OPTIONAL = { get: (value: string | null) => value == null ? null : PARAM_INTEGER_SINGLE.get(value), set: (value: number | null) => value != null ? PARAM_INTEGER_SINGLE.set(value) : null, } satisfies ParamParser -export const PARAM_NUMBER_REPEATABLE = { +export const PARAM_INTEGER_REPEATABLE = { get: (value: string[]) => value.map(PARAM_INTEGER_SINGLE.get), set: (value: number[]) => value.map(PARAM_INTEGER_SINGLE.set), } satisfies ParamParser -export const PARAM_NUMBER_REPEATABLE_OPTIONAL = { +export const PARAM_INTEGER_REPEATABLE_OPTIONAL = { get: (value: string[] | null) => - value == null ? null : PARAM_NUMBER_REPEATABLE.get(value), + value == null ? null : PARAM_INTEGER_REPEATABLE.get(value), set: (value: number[] | null) => - value != null ? PARAM_NUMBER_REPEATABLE.set(value) : null, + value != null ? PARAM_INTEGER_REPEATABLE.set(value) : null, } satisfies ParamParser /** @@ -46,13 +39,13 @@ export const PARAM_NUMBER_REPEATABLE_OPTIONAL = { export const PARAM_PARSER_INT: ParamParser = { get: value => Array.isArray(value) - ? PARAM_NUMBER_REPEATABLE.get(value) + ? PARAM_INTEGER_REPEATABLE.get(value) : value != null ? PARAM_INTEGER_SINGLE.get(value) : null, set: value => Array.isArray(value) - ? PARAM_NUMBER_REPEATABLE.set(value) + ? PARAM_INTEGER_REPEATABLE.set(value) : value != null ? PARAM_INTEGER_SINGLE.set(value) : null, -- 2.47.3