Comparing version 0.1.2 to 0.2.0
@@ -19,3 +19,3 @@ declare const NODE_TYPES: { | ||
parent: RadixNode<T> | null; | ||
children: Record<string, RadixNode<T>>; | ||
children: Map<string, RadixNode<T>>; | ||
data: RadixNodeData | null; | ||
@@ -26,9 +26,11 @@ paramName: string | null; | ||
} | ||
interface RadixRouterOptions { | ||
strictTrailingSlash?: boolean; | ||
routes?: Record<string, any>; | ||
} | ||
interface RadixRouterContext<T extends RadixNodeData = RadixNodeData> { | ||
options: RadixRouterOptions; | ||
rootNode: RadixNode<T>; | ||
staticRoutesMap: Record<string, RadixNode>; | ||
} | ||
interface RadixRouterInitOptions { | ||
routes?: Record<string, any>; | ||
} | ||
interface RadixRouter<T extends RadixNodeData = RadixNodeData> { | ||
@@ -44,9 +46,2 @@ ctx: RadixRouterContext<T>; | ||
/** | ||
* Perform lookup of all paths that start with the given prefix | ||
* @param The prefix to match | ||
* | ||
* @returns An array of matches along with any data that was originally passed in when inserted | ||
*/ | ||
lookupAll(prefix: string): MatchedRoute<T>[]; | ||
/** | ||
* Perform an insert into the radix tree | ||
@@ -67,4 +62,17 @@ * @param path - the prefix to match | ||
declare function createRouter<T extends RadixNodeData = RadixNodeData>(options?: RadixRouterInitOptions): RadixRouter<T>; | ||
declare function createRouter<T extends RadixNodeData = RadixNodeData>(options?: RadixRouterOptions): RadixRouter<T>; | ||
export { MatchedRoute, NODE_TYPE, NODE_TYPES, RadixNode, RadixNodeData, RadixRouter, RadixRouterContext, RadixRouterInitOptions, createRouter }; | ||
interface RouteTable { | ||
static: Map<string, RadixNodeData>; | ||
wildcard: Map<string, RadixNodeData>; | ||
dynamic: Map<string, RouteTable>; | ||
} | ||
interface RouteMatcher { | ||
ctx: { | ||
table: RouteTable; | ||
}; | ||
matchAll: (path: string) => RadixNodeData[]; | ||
} | ||
declare function toRouteMatcher(router: RadixRouter): RouteMatcher; | ||
export { MatchedRoute, NODE_TYPE, NODE_TYPES, RadixNode, RadixNodeData, RadixRouter, RadixRouterContext, RadixRouterOptions, RouteMatcher, RouteTable, createRouter, toRouteMatcher }; |
{ | ||
"name": "radix3", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Lightweight and fast router for JavaScript based on Radix Tree", | ||
@@ -10,4 +10,7 @@ "repository": "unjs/radix3", | ||
"exports": { | ||
"require": "./dist/index.cjs", | ||
"import": "./dist/index.mjs" | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"require": "./dist/index.cjs", | ||
"import": "./dist/index.mjs" | ||
} | ||
}, | ||
@@ -23,5 +26,6 @@ "main": "./dist/index.cjs", | ||
"@nuxtjs/eslint-config-typescript": "latest", | ||
"@vitest/coverage-c8": "latest", | ||
"autocannon": "latest", | ||
"benchmark": "latest", | ||
"c8": "latest", | ||
"changelogen": "^0.3.2", | ||
"eslint": "latest", | ||
@@ -32,7 +36,7 @@ "jiti": "latest", | ||
"standard-version": "latest", | ||
"typescript": "^4.6.4", | ||
"typescript": "^4.8.4", | ||
"unbuild": "latest", | ||
"vitest": "latest" | ||
}, | ||
"packageManager": "pnpm@7.0.0", | ||
"packageManager": "pnpm@7.13.4", | ||
"scripts": { | ||
@@ -46,5 +50,5 @@ "bench": "node ./benchmark/direct.mjs", | ||
"playground": "pnpm jiti ./playground.ts", | ||
"release": "pnpm test && pnpm build && standard-version && git push --follow-tags && pnpm publish", | ||
"release": "pnpm test && pnpm build && changelogen --release && git push --follow-tags && pnpm publish", | ||
"test": "pnpm lint && vitest run" | ||
} | ||
} |
@@ -1,2 +0,1 @@ | ||
# 🌳 radix3 | ||
@@ -40,3 +39,3 @@ | ||
```js | ||
const router = createRouter() | ||
const router = createRouter(/* options */) | ||
@@ -77,6 +76,2 @@ router.insert('/path', { payload: 'this path' }) | ||
### `router.lookupAll(prefix)` | ||
Find all data nodes matching path prefix. | ||
### `router.remove(path)` | ||
@@ -86,2 +81,55 @@ | ||
## Options | ||
You can initialize router instance with options: | ||
```ts | ||
const router = createRouter({ | ||
strictTrailingSlash: true, | ||
routes: { | ||
'/foo': {} | ||
} | ||
}) | ||
``` | ||
- `routes`: An object specifying initial routes to add | ||
- `strictTrailingSlash`: By default router ignored trailing slash for matching and adding routes. When set to `true`, matching with trailing slash is different. | ||
### Route Matcher | ||
**Experimental feature:** Behavior might change in a semver-minor release. | ||
Creates a multi matcher from router tree that can match **all routes** matching path: | ||
```ts | ||
import { createRouter, toRouteMatcher } from 'radix3' | ||
const router = createRouter({ | ||
routes: { | ||
'/foo': { m: 'foo' }, // Matches /foo only | ||
'/foo/**': { m: 'foo/**' }, // Matches /foo/<any> | ||
'/foo/bar': { m: 'foo/bar' }, // Matches /foo/bar only | ||
'/foo/bar/baz': { m: 'foo/bar/baz' }, // Matches /foo/bar/baz only | ||
'/foo/*/baz': { m: 'foo/*/baz' } // Matches /foo/<any>/baz | ||
} | ||
}) | ||
const matcher = toRouteMatcher(router) | ||
const matches = matcher.matchAll('/foo/bar/baz') | ||
// [ | ||
// { | ||
// "m": "foo/**", | ||
// }, | ||
// { | ||
// "m": "foo/*/baz", | ||
// }, | ||
// { | ||
// "m": "foo/bar/baz", | ||
// }, | ||
// ] | ||
``` | ||
## Performance | ||
@@ -88,0 +136,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20927
489
158
14