Skip to content

Commit 6281123

Browse files
committed
refactor: exports and rename MatcherPatternPathDynamic
1 parent 1def9ae commit 6281123

File tree

9 files changed

+177
-591
lines changed

9 files changed

+177
-591
lines changed

packages/router/src/experimental/index.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,26 @@ export type {
1919
MatcherQueryParamsValue,
2020
} from './route-resolver/resolver-abstract'
2121
export {
22-
MatcherPatternPathDynamic,
2322
MatcherPatternPathStatic,
24-
MatcherPatternPathStar,
25-
MatcherPatternPathCustomParams,
26-
// native param parsers
27-
PARAM_PARSER_INT,
23+
MatcherPatternPathDynamic,
2824
} from './route-resolver/matchers/matcher-pattern'
25+
2926
export type {
27+
EmptyParams,
3028
MatcherPattern,
3129
MatcherPatternHash,
3230
MatcherPatternPath,
3331
MatcherPatternQuery,
3432
MatcherParamsFormatted,
35-
EmptyParams,
36-
ParamParser,
33+
MatcherPatternPathDynamic_ParamOptions,
3734
} from './route-resolver/matchers/matcher-pattern'
3835

36+
export {
37+
PARAM_PARSER_INT,
38+
type ParamParser,
39+
defineParamParser,
40+
} from './route-resolver/matchers/param-parsers'
41+
3942
export { miss, MatchMiss } from './route-resolver/matchers/errors'
4043

4144
// in the new experimental router, there are only parents
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { miss } from './errors'
2+
import { MatcherPatternPath } from './matcher-pattern'
3+
4+
/**
5+
* Allows matching a static path folllowed by anything.
6+
*
7+
* @example
8+
*
9+
* ```ts
10+
* const matcher = new MatcherPatternPathStar('/team')
11+
* matcher.match('/team/123') // { pathMatch: '/123' }
12+
* matcher.match('/team/123/more') // { pathMatch: '/123/more' }
13+
* matcher.match('/team-123') // { pathMatch: '-123' }
14+
* matcher.match('/team') // { pathMatch: '' }
15+
* matcher.build({ pathMatch: '/123' }) // '/team/123'
16+
* ```
17+
*/
18+
export class MatcherPatternPathStar
19+
implements MatcherPatternPath<{ pathMatch: string }>
20+
{
21+
private path: string
22+
constructor(path: string = '') {
23+
this.path = path.toLowerCase()
24+
}
25+
26+
match(path: string): { pathMatch: string } {
27+
if (!path.toLowerCase().startsWith(this.path)) {
28+
throw miss()
29+
}
30+
return {
31+
pathMatch: path.slice(this.path.length),
32+
}
33+
}
34+
35+
build(params: { pathMatch: string }): string {
36+
return this.path + params.pathMatch
37+
}
38+
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { describe, expect, it } from 'vitest'
22
import {
33
MatcherPatternPathStatic,
4-
MatcherPatternPathStar,
5-
MatcherPatternPathCustomParams,
4+
MatcherPatternPathDynamic,
65
} from './matcher-pattern'
6+
import { MatcherPatternPathStar } from './matcher-pattern-path-star'
77

88
describe('MatcherPatternPathStatic', () => {
99
describe('match()', () => {
@@ -109,7 +109,7 @@ describe('MatcherPatternPathStar', () => {
109109

110110
describe('MatcherPatternPathCustom', () => {
111111
it('single param', () => {
112-
const pattern = new MatcherPatternPathCustomParams(
112+
const pattern = new MatcherPatternPathDynamic(
113113
/^\/teams\/([^/]+?)\/b$/i,
114114
{
115115
// all defaults
@@ -131,7 +131,7 @@ describe('MatcherPatternPathCustom', () => {
131131
})
132132

133133
it('decodes single param', () => {
134-
const pattern = new MatcherPatternPathCustomParams(
134+
const pattern = new MatcherPatternPathDynamic(
135135
/^\/teams\/([^/]+?)$/i,
136136
{
137137
teamId: {},
@@ -143,7 +143,7 @@ describe('MatcherPatternPathCustom', () => {
143143
})
144144

145145
it('optional param', () => {
146-
const pattern = new MatcherPatternPathCustomParams(
146+
const pattern = new MatcherPatternPathDynamic(
147147
/^\/teams(?:\/([^/]+?))?\/b$/i,
148148
{
149149
teamId: {},
@@ -160,7 +160,7 @@ describe('MatcherPatternPathCustom', () => {
160160
})
161161

162162
it('repeatable param', () => {
163-
const pattern = new MatcherPatternPathCustomParams(
163+
const pattern = new MatcherPatternPathDynamic(
164164
/^\/teams\/(.+?)\/b$/i,
165165
{
166166
teamId: { repeat: true },
@@ -179,7 +179,7 @@ describe('MatcherPatternPathCustom', () => {
179179
})
180180

181181
it('repeatable optional param', () => {
182-
const pattern = new MatcherPatternPathCustomParams(
182+
const pattern = new MatcherPatternPathDynamic(
183183
/^\/teams(?:\/(.+?))?\/b$/i,
184184
{
185185
teamId: { repeat: true },
@@ -202,7 +202,7 @@ describe('MatcherPatternPathCustom', () => {
202202
})
203203

204204
it('multiple params', () => {
205-
const pattern = new MatcherPatternPathCustomParams(
205+
const pattern = new MatcherPatternPathDynamic(
206206
/^\/teams\/([^/]+?)\/([^/]+?)$/i,
207207
{
208208
teamId: {},
@@ -224,7 +224,7 @@ describe('MatcherPatternPathCustom', () => {
224224
})
225225

226226
it('sub segments (params + static)', () => {
227-
const pattern = new MatcherPatternPathCustomParams(
227+
const pattern = new MatcherPatternPathDynamic(
228228
/^\/teams\/([^/]+?)-b-([^/]+?)$/i,
229229
{
230230
teamId: {},

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { describe, expectTypeOf, it } from 'vitest'
2-
import {
3-
MatcherPatternPathCustomParams,
4-
PARAM_INTEGER_SINGLE,
5-
PATH_PARAM_DEFAULT_PARSER,
6-
PATH_PARAM_SINGLE_DEFAULT,
7-
} from './matcher-pattern'
2+
import { MatcherPatternPathDynamic } from './matcher-pattern'
3+
import { PARAM_INTEGER_SINGLE } from './param-parsers/numbers'
4+
import { PATH_PARAM_DEFAULT_PARSER } from './param-parsers'
5+
import { PATH_PARAM_SINGLE_DEFAULT } from './param-parsers'
86

97
describe('MatcherPatternPathCustomParams', () => {
108
it('can be generic', () => {
11-
const matcher = new MatcherPatternPathCustomParams(
9+
const matcher = new MatcherPatternPathDynamic(
1210
/^\/users\/([^/]+)$/i,
1311
{ userId: { ...PATH_PARAM_DEFAULT_PARSER } },
1412
['users', 0]
@@ -33,7 +31,7 @@ describe('MatcherPatternPathCustomParams', () => {
3331
})
3432

3533
it('can be a simple param', () => {
36-
const matcher = new MatcherPatternPathCustomParams(
34+
const matcher = new MatcherPatternPathDynamic(
3735
/^\/users\/([^/]+)\/([^/]+)$/i,
3836
{ userId: { ...PATH_PARAM_SINGLE_DEFAULT, repeat: true } },
3937
['users', 0]
@@ -51,7 +49,7 @@ describe('MatcherPatternPathCustomParams', () => {
5149
})
5250

5351
it('can be a custom type', () => {
54-
const matcher = new MatcherPatternPathCustomParams(
52+
const matcher = new MatcherPatternPathDynamic(
5553
/^\/profiles\/([^/]+)$/i,
5654
{
5755
userId: {

0 commit comments

Comments
 (0)