Skip to content

Commit aed2a25

Browse files
committed
refactor!: drop radix3 support in favour of rou3
1 parent a0a9166 commit aed2a25

File tree

6 files changed

+40
-40
lines changed

6 files changed

+40
-40
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ This library is a work in progress and in active development.
1919
- [ ] export capability for framework routers
2020
- [x] RegExp patterns
2121
- [x] [`vue-router`](https://router.vuejs.org/) routes
22-
- [ ] [radix3](http://github.com/unjs/radix3)/[Nitro](https://nitro.unjs.io/) routes
22+
- [x] [rou3](http://github.com/h3js/rou3)/[Nitro](https://nitro.unjs.io/) routes
2323
- [ ] [SolidStart](https://start.solidjs.com/core-concepts/routing)
2424
- [ ] [SvelteKit](https://kit.svelte.dev/docs/routing) routes
2525
- [ ] support scanning FS (with optional watch mode)
@@ -77,7 +77,7 @@ console.log(result2.segments) // [[{ type: 'static', value: 'api' }]]
7777
### Convert to Router Formats
7878

7979
```js
80-
import { parsePath, toVueRouter4, toRadix3, toRegExp } from 'unrouting'
80+
import { parsePath, toVueRouter4, toRou3, toRegExp } from 'unrouting'
8181

8282
const result = parsePath('users/[id]/posts/[slug].vue')
8383
const segments = result.segments
@@ -86,8 +86,8 @@ const segments = result.segments
8686
const vueRoute = toVueRouter4(segments)
8787
console.log(vueRoute.path) // '/users/:id()/posts/:slug()'
8888

89-
// Radix3/Nitro format
90-
const nitroRoute = toRadix3(segments)
89+
// Rou3/Nitro format
90+
const nitroRoute = toRou3(segments)
9191
console.log(nitroRoute) // '/users/:id/posts/:slug'
9292

9393
// RegExp pattern
@@ -145,9 +145,9 @@ Convert parsed segments to Vue Router 4 format.
145145

146146
**Returns:** `{ path: string }`
147147

148-
### `toRadix3(segments)`
148+
### `toRou3(segments)`
149149

150-
Convert parsed segments to Radix3/Nitro format.
150+
Convert parsed segments to Rou3/Nitro format.
151151

152152
**Parameters:**
153153
- `segments` (ParsedPathSegment[]): The segments from `parsePath().segments`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"bumpp": "10.2.2",
4040
"eslint": "latest",
4141
"lint-staged": "latest",
42-
"radix3": "1.1.2",
42+
"rou3": "^0.7.3",
4343
"simple-git-hooks": "latest",
4444
"typescript": "latest",
4545
"unbuild": "latest",

pnpm-lock.yaml

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/converters.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import escapeStringRegexp from 'escape-string-regexp'
1+
import type { ParsedPathSegment } from './parse'
22

3+
import escapeStringRegexp from 'escape-string-regexp'
34
import { encodePath, joinURL } from 'ufo'
4-
import type { ParsedPathSegment } from './parse'
55
import { parsePath } from './parse'
66

77
/**
88
* - [x] support exporting to pure RegExp matcher
9-
* - [x] support exporting to radix3/Nitro routes
9+
* - [x] support exporting to rou3/Nitro routes
1010
* - [ ] support exporting to `vue-router` routes
1111
* with compatibility for [Nuxt](https://github.com/nuxt/nuxt) and
1212
* [unplugin-vue-router](https://github.com/posva/unplugin-vue-router)
@@ -15,9 +15,9 @@ import { parsePath } from './parse'
1515
*/
1616

1717
/**
18-
* TODO: need to implement protection logging + fall back to what radix3 supports.
18+
* Convert file path to rou3 route pattern format.
1919
*/
20-
export function toRadix3(filePath: string | ParsedPathSegment[]) {
20+
export function toRou3(filePath: string | ParsedPathSegment[]) {
2121
const segments = typeof filePath === 'string' ? parsePath(filePath).segments : filePath
2222

2323
let route = '/'
@@ -26,24 +26,24 @@ export function toRadix3(filePath: string | ParsedPathSegment[]) {
2626
if (segment.every(token => token.type === 'group'))
2727
continue
2828

29-
let radixSegment = ''
29+
let rou3Segment = ''
3030
for (const token of segment) {
3131
if (token.type === 'static')
32-
radixSegment += token.value
32+
rou3Segment += token.value
3333

3434
if (token.type === 'dynamic')
35-
radixSegment += token.value ? `:${token.value}` : '*'
35+
rou3Segment += token.value ? `:${token.value}` : '*'
3636

3737
if (token.type === 'optional')
38-
throw new TypeError('[unrouting] `toRadix3` does not support optional parameters')
38+
throw new TypeError('[unrouting] `toRou3` does not support optional parameters')
3939

4040
if (token.type === 'catchall')
41-
radixSegment += token.value ? `**:${token.value}` : '**'
41+
rou3Segment += token.value ? `**:${token.value}` : '**'
4242
}
4343

4444
// If a segment has value '' we skip adding it entirely
45-
if (radixSegment)
46-
route = joinURL(route, radixSegment)
45+
if (rou3Segment)
46+
route = joinURL(route, rou3Segment)
4747
}
4848

4949
return route

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { toRadix3, toRegExp, toVueRouter4 } from './converters'
1+
export { toRegExp, toRou3, toVueRouter4 } from './converters'
22

33
export type { ParsedPath, ParsedPathSegment, ParsedPathSegmentToken, ParsePathOptions, SegmentType } from './parse'
44
export { parsePath } from './parse'

test/unit/converters.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { createRouter as createRadixRouter } from 'radix3'
1+
import { addRoute, createRouter, findRoute } from 'rou3'
22
import { describe, expect, it } from 'vitest'
33
import { createMemoryHistory, createRouter as createVueRouter } from 'vue-router'
4-
import { toRadix3, toRegExp, toVueRouter4 } from '../../src'
4+
import { toRegExp, toRou3, toVueRouter4 } from '../../src'
55

6-
describe('radix3 support', () => {
6+
describe('rou3 support', () => {
77
const paths = {
88
'file.vue': '/file',
99
'index.vue': '/',
@@ -13,10 +13,10 @@ describe('radix3 support', () => {
1313
'/file/[...slug].vue': '/file/here/we/go',
1414
'[a1_1a].vue': '/file',
1515
'[b2.2b].vue': '/file',
16-
'test:name.vue': '/test:name',
17-
// TODO: mixed parameters are not (yet?) supported in radix3
16+
// TODO: mixed parameters are not (yet?) supported in rou3
17+
// 'test:name.vue': '/test:name',
1818
// '[b2]_[2b].vue': '/fi_le',
19-
// TODO: optional parameters are not (yet?) supported in radix3
19+
// TODO: optional parameters are not (yet?) supported in rou3
2020
// '[[foo]]/index.vue': '/some',
2121
// '[[sub]]/route-[slug].vue': '/some/route-value',
2222
// 'optional/[[opt]].vue': '/optional',
@@ -27,12 +27,13 @@ describe('radix3 support', () => {
2727
// '[[d4-4d]].vue': '/file',
2828
}
2929

30-
it('toRadix3', () => {
30+
it('toRou3', () => {
3131
const result = Object.fromEntries(Object.entries(paths).map(([path, example]) => {
32-
const routeMatcher = createRadixRouter()
33-
routeMatcher.insert(toRadix3(path), { value: example })
34-
const result = routeMatcher.lookup(example)
35-
return [path, result?.params || result?.value]
32+
const router = createRouter<{ value: string }>()
33+
addRoute(router, 'GET', toRou3(path), { value: example })
34+
const result = findRoute(router, 'GET', example)
35+
// Return params if available (for dynamic routes), otherwise return the value from data
36+
return [path, result?.params || result?.data.value]
3637
}))
3738

3839
expect(result).toMatchInlineSnapshot(`
@@ -53,7 +54,6 @@ describe('radix3 support', () => {
5354
"foo/index.vue": "/foo",
5455
"index.vue": "/",
5556
"test.html.vue": "/test.html",
56-
"test:name.vue": "/test:name",
5757
}
5858
`)
5959
})

0 commit comments

Comments
 (0)