Skip to content

Commit 7bcf4c6

Browse files
committed
refactor: improve integer param
1 parent 2d1abf5 commit 7bcf4c6

File tree

1 file changed

+10
-17
lines changed
  • packages/router/src/experimental/route-resolver/matchers/param-parsers

1 file changed

+10
-17
lines changed

packages/router/src/experimental/route-resolver/matchers/param-parsers/numbers.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,34 @@
11
import { miss } from '../errors'
22
import { ParamParser } from './types'
33

4-
/**
5-
* @internal
6-
*/
7-
const IS_INTEGER_RE = /^-?\d+$/
8-
94
export const PARAM_INTEGER_SINGLE = {
105
get: (value: string) => {
11-
if (IS_INTEGER_RE.test(value)) {
12-
const num = Number(value)
13-
if (Number.isFinite(num)) {
14-
return num
15-
}
6+
const num = Number(value)
7+
if (Number.isInteger(num)) {
8+
return num
169
}
1710
throw miss()
1811
},
1912
set: (value: number) => String(value),
2013
} satisfies ParamParser<number, string>
2114

22-
export const PARAM_NUMBER_OPTIONAL = {
15+
export const PARAM_INTEGER_OPTIONAL = {
2316
get: (value: string | null) =>
2417
value == null ? null : PARAM_INTEGER_SINGLE.get(value),
2518
set: (value: number | null) =>
2619
value != null ? PARAM_INTEGER_SINGLE.set(value) : null,
2720
} satisfies ParamParser<number | null, string | null>
2821

29-
export const PARAM_NUMBER_REPEATABLE = {
22+
export const PARAM_INTEGER_REPEATABLE = {
3023
get: (value: string[]) => value.map(PARAM_INTEGER_SINGLE.get),
3124
set: (value: number[]) => value.map(PARAM_INTEGER_SINGLE.set),
3225
} satisfies ParamParser<number[], string[]>
3326

34-
export const PARAM_NUMBER_REPEATABLE_OPTIONAL = {
27+
export const PARAM_INTEGER_REPEATABLE_OPTIONAL = {
3528
get: (value: string[] | null) =>
36-
value == null ? null : PARAM_NUMBER_REPEATABLE.get(value),
29+
value == null ? null : PARAM_INTEGER_REPEATABLE.get(value),
3730
set: (value: number[] | null) =>
38-
value != null ? PARAM_NUMBER_REPEATABLE.set(value) : null,
31+
value != null ? PARAM_INTEGER_REPEATABLE.set(value) : null,
3932
} satisfies ParamParser<number[] | null, string[] | null>
4033

4134
/**
@@ -46,13 +39,13 @@ export const PARAM_NUMBER_REPEATABLE_OPTIONAL = {
4639
export const PARAM_PARSER_INT: ParamParser<number | number[] | null> = {
4740
get: value =>
4841
Array.isArray(value)
49-
? PARAM_NUMBER_REPEATABLE.get(value)
42+
? PARAM_INTEGER_REPEATABLE.get(value)
5043
: value != null
5144
? PARAM_INTEGER_SINGLE.get(value)
5245
: null,
5346
set: value =>
5447
Array.isArray(value)
55-
? PARAM_NUMBER_REPEATABLE.set(value)
48+
? PARAM_INTEGER_REPEATABLE.set(value)
5649
: value != null
5750
? PARAM_INTEGER_SINGLE.set(value)
5851
: null,

0 commit comments

Comments
 (0)