Skip to content

Commit 3646b9c

Browse files
committed
feat: inverse spalt and other params in MatcherPatternPathdynamic
1 parent f065113 commit 3646b9c

File tree

5 files changed

+27
-25
lines changed

5 files changed

+27
-25
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ const r_profiles_detail = normalizeRouteRecord({
121121
// and therefore userId is of type number
122122
userId: PARAM_PARSER_INT,
123123
},
124-
['profiles', 0]
124+
['profiles', 1]
125125
),
126126
})
127127

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ describe('MatcherPatternPathDynamic', () => {
134134
// all defaults
135135
teamId: {},
136136
},
137-
['teams', 0, 'b']
137+
['teams', 1, 'b']
138138
)
139139

140140
expect(pattern.match('/teams/123/b')).toEqual({
@@ -155,7 +155,7 @@ describe('MatcherPatternPathDynamic', () => {
155155
{
156156
teamId: {},
157157
},
158-
['teams', 0]
158+
['teams', 1]
159159
)
160160
expect(pattern.match('/teams/a%20b')).toEqual({ teamId: 'a b' })
161161
expect(pattern.build({ teamId: 'a b' })).toBe('/teams/a%20b')
@@ -167,7 +167,7 @@ describe('MatcherPatternPathDynamic', () => {
167167
{
168168
teamId: {},
169169
},
170-
['teams', 0, 'b']
170+
['teams', 1, 'b']
171171
)
172172

173173
expect(pattern.match('/teams/b')).toEqual({ teamId: null })
@@ -186,7 +186,7 @@ describe('MatcherPatternPathDynamic', () => {
186186
{
187187
teamId: {},
188188
},
189-
['teams', 0, 'b']
189+
['teams', 1, 'b']
190190
)
191191

192192
expect(pattern.match('/teams/b')).toEqual({ teamId: null })
@@ -205,7 +205,7 @@ describe('MatcherPatternPathDynamic', () => {
205205
{
206206
teamId: { repeat: true },
207207
},
208-
['teams', 0, 'b']
208+
['teams', 1, 'b']
209209
)
210210

211211
expect(pattern.match('/teams/123/b')).toEqual({ teamId: ['123'] })
@@ -228,7 +228,7 @@ describe('MatcherPatternPathDynamic', () => {
228228
{
229229
pathMatch: {},
230230
},
231-
['teams', 1]
231+
['teams', 0]
232232
)
233233
expect(pattern.match('/teams/')).toEqual({ pathMatch: '' })
234234
expect(pattern.match('/teams/123/b')).toEqual({ pathMatch: '123/b' })
@@ -247,7 +247,7 @@ describe('MatcherPatternPathDynamic', () => {
247247
{
248248
teamId: { repeat: true },
249249
},
250-
['teams', 0, 'b']
250+
['teams', 1, 'b']
251251
)
252252

253253
expect(pattern.match('/teams/123/b')).toEqual({ teamId: ['123'] })
@@ -271,7 +271,7 @@ describe('MatcherPatternPathDynamic', () => {
271271
teamId: {},
272272
otherId: {},
273273
},
274-
['teams', 0, 0]
274+
['teams', 1, 1]
275275
)
276276

277277
expect(pattern.match('/teams/123/456')).toEqual({
@@ -293,7 +293,7 @@ describe('MatcherPatternPathDynamic', () => {
293293
teamId: {},
294294
otherId: {},
295295
},
296-
['teams', [0, '-b-', 0]]
296+
['teams', [1, '-b-', 1]]
297297
)
298298

299299
expect(pattern.match('/teams/123-b-456')).toEqual({
@@ -314,7 +314,7 @@ describe('MatcherPatternPathDynamic', () => {
314314
{
315315
teamId: {},
316316
},
317-
['teams', [0, '/']]
317+
['teams', [1, '/']]
318318
)
319319

320320
expect(pattern.match('/teams/123/')).toEqual({
@@ -345,7 +345,7 @@ describe('MatcherPatternPathDynamic', () => {
345345
{
346346
teamId: { repeat: true },
347347
},
348-
['teams', [0, '/']]
348+
['teams', [1, '/']]
349349
)
350350

351351
expect(pattern.match('/teams/123/')).toEqual({ teamId: ['123'] })
@@ -365,7 +365,7 @@ describe('MatcherPatternPathDynamic', () => {
365365
{
366366
teamId: { repeat: true },
367367
},
368-
['teams', [0, '/']]
368+
['teams', [1, '/']]
369369
)
370370

371371
expect(pattern.match('/teams/123/')).toEqual({ teamId: ['123'] })

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('MatcherPatternPathDynamic', () => {
99
const matcher = new MatcherPatternPathDynamic(
1010
/^\/users\/([^/]+)$/i,
1111
{ userId: { ...PATH_PARAM_PARSER_DEFAULTS } },
12-
['users', 0]
12+
['users', 1]
1313
)
1414

1515
expectTypeOf(matcher.match('/users/123')).toEqualTypeOf<{
@@ -34,7 +34,7 @@ describe('MatcherPatternPathDynamic', () => {
3434
const matcher = new MatcherPatternPathDynamic(
3535
/^\/users\/([^/]+)\/([^/]+)$/i,
3636
{ userId: { ...PATH_PARAM_SINGLE_DEFAULT, repeat: true } },
37-
['users', 0]
37+
['users', 1]
3838
)
3939
expectTypeOf(matcher.match('/users/123/456')).toEqualTypeOf<{
4040
userId: string
@@ -57,7 +57,7 @@ describe('MatcherPatternPathDynamic', () => {
5757
// parser: PATH_PARAM_DEFAULT_PARSER,
5858
},
5959
},
60-
['profiles', 0]
60+
['profiles', 1]
6161
)
6262

6363
expectTypeOf(matcher.match('/profiles/2')).toEqualTypeOf<{

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export class MatcherPatternPathDynamic<
137137
// otherwise, we need to use a factory function: https://github.com/microsoft/TypeScript/issues/40451
138138
readonly params: TParamsOptions &
139139
Record<string, MatcherPatternPathDynamic_ParamOptions<any, any>>,
140-
// 0 means a regular param, 1 means a splat, the order comes from the keys in params
140+
// 1 means a regular param, 0 means a splat, the order comes from the keys in params
141141
readonly pathParts: Array<string | number | Array<string | number>>
142142
) {
143143
this.paramsKeys = Object.keys(this.params) as Array<keyof TParamsOptions>
@@ -198,8 +198,8 @@ export class MatcherPatternPathDynamic<
198198

199199
return Array.isArray(value)
200200
? value.map(encodeParam).join('/')
201-
: // part == 0 means a regular param, 1 means a splat
202-
(part /* part !== 0 */ ? encodePath : encodeParam)(value)
201+
: // part == 1 means a regular param, 0 means a splat
202+
(part ? encodeParam : encodePath)(value)
203203
} else {
204204
return part
205205
.map(subPart => {
@@ -235,7 +235,9 @@ export class MatcherPatternPathDynamic<
235235
* with the original splat path: e.g. /teams/[...pathMatch] does not match /teams, so it makes
236236
* no sense to build a path it cannot match.
237237
*/
238-
return lastParamPart && !value ? path + '/' : path
238+
return !lastParamPart /** lastParamPart == 0 */ && !value
239+
? path + '/'
240+
: path
239241
}
240242
}
241243

packages/router/src/experimental/router.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,25 @@ import { mockWarn } from '../../__tests__/vitest-mock-warn'
5555
const paramMatcher = new MatcherPatternPathDynamic(
5656
/^\/p\/([^/]+)$/,
5757
{ p: {} },
58-
['p', 0]
58+
['p', 1]
5959
)
6060

6161
const optionalMatcher = new MatcherPatternPathDynamic(
6262
/^\/optional(?:\/([^/]+))?$/,
6363
{ p: {} },
64-
['optional', 0]
64+
['optional', 1]
6565
)
6666

6767
const repeatMatcher = new MatcherPatternPathDynamic(
6868
/^\/repeat\/(.+)$/,
6969
{ r: { repeat: true } },
70-
['repeat', 1]
70+
['repeat', 0]
7171
)
7272

7373
const catchAllMatcher = new MatcherPatternPathDynamic(
7474
/^\/(.*)$/,
7575
{ pathMatch: { repeat: true } },
76-
[1]
76+
[0]
7777
)
7878

7979
// Create experimental route records using proper structure
@@ -380,7 +380,7 @@ describe('Experimental Router', () => {
380380
const testCatchAllMatcher = new MatcherPatternPathDynamic(
381381
/^\/(.*)$/,
382382
{ pathMatch: { repeat: true } },
383-
[1]
383+
[0]
384384
)
385385
const catchAllRecord = normalizeRouteRecord({
386386
name: 'notfound',

0 commit comments

Comments
 (0)