https://github.com/localvoid/routekit
:twisted_rightwards_arrows: RouteKit is a set of tools for generating efficient routers.
https://github.com/localvoid/routekit
javascript perfmatters routekit router typescript
Last synced: 12 months ago
JSON representation
:twisted_rightwards_arrows: RouteKit is a set of tools for generating efficient routers.
- Host: GitHub
- URL: https://github.com/localvoid/routekit
- Owner: localvoid
- License: mit
- Created: 2017-02-25T03:27:42.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2018-11-14T07:44:25.000Z (over 7 years ago)
- Last Synced: 2025-04-11T21:13:44.836Z (12 months ago)
- Topics: javascript, perfmatters, routekit, router, typescript
- Language: TypeScript
- Homepage:
- Size: 144 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# [RouteKit](https://github.com/localvoid/routekit) · [](https://github.com/localvoid/routekit/blob/master/LICENSE) [](https://codecov.io/gh/localvoid/routekit) [](https://circleci.com/gh/localvoid/routekit) [](https://github.com/localvoid/routekit)
RouteKit is a set of tools for generating lightweight and efficient routers.
|packages |NPM version |
|-----------------|---------------------------------------------------------------------------------------------------------------------|
|routekit |[](https://www.npmjs.com/package/routekit) |
|routekit-resolver|[](https://www.npmjs.com/package/routekit-resolver)|
## Installation
```sh
$ npm install -D routekit
```
## Usage Example
Create a routes file: `routes.build.js`
```ts
#!/usr/bin/env node
import { r, emitter } from "routekit";
const enum Location { Home, UserView, UserEdit }
process.stdout.write(emitter()(
r("/", Location.Home),
r("/user/:id", Location.UserView),
r("/user/:id/edit", Location.UserEdit),
));
```
Run `routes.build.js` file
```sh
$ node routes.build.js > routes.js
```
And it will generate `routes.js` file with a compact flattened trie.
```js
const ROUTES = {
f: [35, 38, 33, 7],
p: ["user/", "/edit"],
s: [0, 1, 2],
};
```
## Emmiter
Emitter generates flattened tries that can be used by `routekit-resolver` for matching urls.
```ts
export function emitter(name = "ROUTES"): (...routes: Route[]) => string;
export function injector(src: string): (...routes: Route[]) => string;
```
`inject` function injects code blocks into existing code using [incode](https://github.com/localvoid/incode) package.
This function is using `routekit` prefix to detect injectable regions.
## Resolver
### Installation
NPM package `routekit-resolver` provides a commonjs, es2015 modules and TypeScript typings.
```sh
npm install routekit-resolver
```
### Usage Example
```js
import { resolve } from "routekit-resolver";
// routekit:emit("routes")
const ROUTES = {
f: [35, 38, 33, 7],
p: ["user/", "/edit"],
s: [0, 1, 2],
};
// routekit:end
const match = (path) => resolve(ROUTES, path);
match("/user/123");
// {
// state: 1,
// vars: ["123"],
// }
```
### API
```ts
export interface ResolveResult {
readonly state: T;
readonly vars: string[];
}
export function resolve(map: RouteMap, path: string): ResolveResult | null;
```
`resolve()` function has 2 parameters:
- `map` is a flattened trie generated by routekit.
- `path` is a path that should be resolved.
When resolve function returns `null` value it means that no match was found.