]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor: improve integer param
authorEduardo San Martin Morote <posva13@gmail.com>
Sun, 17 Aug 2025 13:06:56 +0000 (15:06 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Sun, 17 Aug 2025 13:06:56 +0000 (15:06 +0200)
packages/router/src/experimental/route-resolver/matchers/param-parsers/numbers.ts

index 5689620019b40c16a59d65d4d8cfdea08faf8e43..4ee079be13a628263ac93d8c6be6723b89de4f20 100644 (file)
@@ -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<number, string>
 
-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<number | null, string | null>
 
-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<number[], string[]>
 
-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<number[] | null, string[] | null>
 
 /**
@@ -46,13 +39,13 @@ export const PARAM_NUMBER_REPEATABLE_OPTIONAL = {
 export const PARAM_PARSER_INT: ParamParser<number | number[] | null> = {
   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,