Skip to content

Commit f006032

Browse files
committed
chore: initial param parsers
1 parent 7df84d9 commit f006032

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

packages/experiments-playground/src/router/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { createWebHistory, type RouteParamValue } from 'vue-router'
1+
import { createWebHistory } from 'vue-router'
22
import {
33
experimental_createRouter,
44
createStaticResolver,
55
MatcherPatternPathStatic,
6+
MatcherPatternPathStar,
67
MatcherPatternPathCustomParams,
78
normalizeRouteRecord,
9+
PARAM_INTEGER,
810
} from 'vue-router/experimental'
911
import type {
1012
EXPERIMENTAL_RouteRecordNormalized_Matchable,
@@ -143,12 +145,8 @@ const r_profiles_detail = normalizeRouteRecord({
143145
/^\/profiles\/(?<userId>[^/]+)$/i,
144146
{
145147
userId: {
146-
parser: {
147-
// @ts-expect-error: FIXME: should would with generic class
148-
get: (value: string): number => Number(value),
149-
// @ts-expect-error: FIXME: should would with generic class
150-
set: (value: number): string => String(value),
151-
},
148+
// @ts-expect-error: FIXME: should allow the type
149+
parser: PARAM_INTEGER,
152150
},
153151
},
154152
({ userId }) => {

packages/router/src/experimental/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ export {
2323
MatcherPatternPathStatic,
2424
MatcherPatternPathStar,
2525
MatcherPatternPathCustomParams,
26+
// custom param parsers
27+
// TODO: find a more elegant format than having 4 variants per param type
28+
PARAM_INTEGER,
29+
PARAM_NUMBER_OPTIONAL,
30+
PARAM_NUMBER_REPEATABLE,
31+
PARAM_NUMBER_REPEATABLE_OPTIONAL,
2632
} from './route-resolver/matchers/matcher-pattern'
2733
export type {
2834
MatcherPattern,

packages/router/src/experimental/route-resolver/matchers/matcher-pattern.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,15 @@ interface MatcherPatternPathCustomParamOptions<
202202
parser?: Param_GetSet<TIn, TOut>
203203
}
204204

205-
export const PARAM_NUMBER = {
205+
const IS_INTEGER_RE = /^-?[1-9]\d*$/
206+
207+
export const PARAM_INTEGER = {
206208
get: (value: string) => {
207-
const num = Number(value)
208-
if (Number.isFinite(num)) {
209-
return num
209+
if (IS_INTEGER_RE.test(value)) {
210+
const num = Number(value)
211+
if (Number.isFinite(num)) {
212+
return num
213+
}
210214
}
211215
throw miss()
212216
},
@@ -215,16 +219,23 @@ export const PARAM_NUMBER = {
215219

216220
export const PARAM_NUMBER_OPTIONAL = {
217221
get: (value: string | null) =>
218-
value == null ? null : PARAM_NUMBER.get(value),
222+
value == null ? null : PARAM_INTEGER.get(value),
219223
set: (value: number | null) =>
220-
value != null ? PARAM_NUMBER.set(value) : null,
224+
value != null ? PARAM_INTEGER.set(value) : null,
221225
} satisfies Param_GetSet<string | null, number | null>
222226

223227
export const PARAM_NUMBER_REPEATABLE = {
224-
get: (value: string[]) => value.map(PARAM_NUMBER.get),
225-
set: (value: number[]) => value.map(PARAM_NUMBER.set),
228+
get: (value: string[]) => value.map(PARAM_INTEGER.get),
229+
set: (value: number[]) => value.map(PARAM_INTEGER.set),
226230
} satisfies Param_GetSet<string[], number[]>
227231

232+
export const PARAM_NUMBER_REPEATABLE_OPTIONAL = {
233+
get: (value: string[] | null) =>
234+
value == null ? null : PARAM_NUMBER_REPEATABLE.get(value),
235+
set: (value: number[] | null) =>
236+
value != null ? PARAM_NUMBER_REPEATABLE.set(value) : null,
237+
} satisfies Param_GetSet<string[] | null, number[] | null>
238+
228239
export class MatcherPatternPathCustomParams implements MatcherPatternPath {
229240
// private paramsKeys: string[]
230241

0 commit comments

Comments
 (0)