Coder Social home page Coder Social logo

immobiliare / radix3 Goto Github PK

View Code? Open in Web Editor NEW
12.0 2.0 0.0 1.24 MB

๐ŸŒณ Lightweight and fast router for JavaScript based on Radix Tree. This fork adds functional matching!

License: MIT License

JavaScript 11.63% TypeScript 88.02% Shell 0.36%
javascript nodejs radix-tree router

radix3's Introduction

๐ŸŒณ radix3

release workflow code style: prettier semantic-release npm (scoped) license

Lightweight and fast router for JavaScript based on Radix Tree. This fork of unjs/radix3 adds functional matching ::function to the base library.

Usage

Install package:

# npm
npm i @immobiliarelabs/radix3

# yarn
yarn add @immobiliarelabs/radix3

# pnpm
pnpm i @immobiliarelabs/radix3

Import:

// ESM
import { createRouter } from '@immobiliarelabs/radix3'

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

Create a router instance and insert routes:

const router = createRouter(/* options */)

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 or else using ::functionName for functional match.

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.remove(path)

Remove route matching path.

Options

You can initialize router instance with options:

const router = createRouter({
  strictTrailingSlash: true,
  routes: {
    '/foo': {},
    '/::len': {} // Matches all routes /<two length string>/
  },
  funcs: {
    len: (str) => str.length === 2,
  }
})
  • 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.
  • funcs: An object containing the functions used in the routes
  • parseParameters: By default this variable is set to true. When set to false, the params attribute will not be present in the lookup function output object. Putting this parameter to false increases the lookup performance by 3x for dynamic routes.

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:

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
  },
  funcs: {
    len: (str) => str.length === 2,
  }
})

const matcher = toRouteMatcher(router)

const matches = matcher.matchAll('/foo/bar/baz')

// [
//   {
//     "m": "foo/**",
//   },
//   {
//     "m": "foo/*/baz",
//   },
//   {
//     "m": "foo/bar/baz",
//   },
// ]

Performance

See benchmark.

Powered Apps

@immobiliarelabs/radix3 is a fork of radix3 and @immobiliarelabs/radix3 was created by the amazing Node.js team at ImmobiliareLabs, the Tech dept of Immobiliare.it, the #1 real estate company in Italy.

We are currently using radix3 in our products as well as our internal toolings.

If you are using radix3 in production drop us a message.

Support & Contribute

Made with โค๏ธ by ImmobiliareLabs & Contributors

We'd love for you to contribute to radix3! If you have any questions on how to use radix3, bugs and enhancement please feel free to reach out by opening a GitHub Issue.

License

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

MIT - Made with โค๏ธ

radix3's People

Contributors

antoniomuso avatar danielroe avatar dependabot[bot] avatar harlan-zw avatar pi0 avatar renovate[bot] avatar rslabbert avatar semantic-release-bot avatar simonecorsi avatar wobsoriano avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

radix3's Issues

ignore parameters

We can implement a new router option: ignoreParameters.

This option removes adding parameters in the output of the lookup function, increasing the lookup performance.

ignoreParameters false

const router = createRouter({
        ignoreParameters: false,
        routes: {
            'hello/*': { out: 'myValue' },
        },
});
router.lookup('hello/some') // {out: 'myValue', params: { _0: 'some'}}

ignoreParameters true

const router = createRouter({
        ignoreParameters: true,
        routes: {
            'hello/*': { out: 'myValue' },
        },
});
router.lookup('hello/some') // {out: 'myValue'}

@simonecorsi @dnlup

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.