Code Monkey home page Code Monkey logo

radix3's Introduction

๐ŸŒณ radix3

npm version npm downloads Github Actions Codecov bundle

Lightweight and fast router for JavaScript based on Radix Tree.

Usage

Install package:

# npm
npm i radix3

# yarn
yarn add radix3

# pnpm
pnpm i radix3

Import:

// ESM
import { createRouter } from 'radix3'

// CJS
const { createRouter } = require('radix3')

Create a router instance and insert routes:

const router = createRouter()

router.insert('/path', { payload: 'this path' })
router.insert('/path/:name', { payload: 'named route' })
router.insert('/path/foo/**', { payload: 'wildcard route' })
router.insert('/path/foo/**:name', { payload: 'named wildcard route' })

*Match route to access matched data:

// { payload: 'this path' }
router.lookup('/path')

// { payload: 'named route', params: { name: 'fooval' } }
router.lookup('/path/fooval')

// { payload: 'wildcard route' }
router.lookup('/path/foo/bar/baz')

// null (no route matched for/)
router.lookup('/')

Methods

router.insert(path, data)

path can be static or using :placeholders and ** for wildcard paths.

The data object will be returned on matching params. It should be an object like { handler } and not containing reserved keyword params.

router.lookup(path)

Returns matched data for path with optional params key if mached route using placeholders.

router.lookupAll(prefix)

Find all data nodes matching path prefix.

router.remove(path)

Remove route matching path.

Performance

See benchmark.

License

Based on original work of charlieduong94/radix-router by Charlie Duong (MIT)

MIT - Made with โค๏ธ

radix3's People

Contributors

harlan-zw avatar pi0 avatar renovate[bot] avatar wobsoriano 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.