Code Monkey home page Code Monkey logo

routekit's Introduction

RouteKit is a set of tools for generating lightweight and efficient routers.

packages NPM version
routekit npm version
routekit-resolver npm version

Installation

$ npm install -D routekit

Usage Example

Create a routes file: routes.build.js

#!/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

$ node routes.build.js > routes.js

And it will generate routes.js file with a compact flattened trie.

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.

export function emitter<T>(name = "ROUTES"): (...routes: Route<T>[]) => string;
export function injector<T>(src: string): (...routes: Route<T>[]) => string;

inject function injects code blocks into existing code using 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.

npm install routekit-resolver

Usage Example

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

export interface ResolveResult<T> {
  readonly state: T;
  readonly vars: string[];
}

export function resolve<T>(map: RouteMap<T>, path: string): ResolveResult<T> | 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.

routekit's People

Contributors

localvoid avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.