Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
radix3
Advanced tools
Changelog
Readme
Lightweight and fast router for JavaScript based on Radix Tree.
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(/* 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:
router.lookup("/path");
// { payload: 'this path' }
router.lookup("/path/fooval");
// { payload: 'named route', params: { name: 'fooval' } }
router.lookup("/path/foo/bar/baz");
// { payload: 'wildcard route' }
router.lookup("/");
// null (no route matched for/)
router.insert(path, data)
path
can be static or using :placeholder
or **
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.remove(path)
Remove route matching path
.
You can initialize router instance with options:
const router = createRouter({
strictTrailingSlash: true,
routes: {
"/foo": {},
},
});
routes
: An object specifying initial routes to addstrictTrailingSlash
: By default router ignored trailing slash for matching and adding routes. When set to true
, matching with trailing slash is different.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
},
});
const matcher = toRouteMatcher(router);
const matches = matcher.matchAll("/foo/bar/baz");
// [
// {
// "m": "foo/**",
// },
// {
// "m": "foo/*/baz",
// },
// {
// "m": "foo/bar/baz",
// },
// ]
It is also possible to export and then rehydrate a matcher from pre-compiled rules.
import { exportMatcher, createMatcherFromExport } from "radix3";
// Assuming you already have a matcher
// you can export this to a JSON-type object
const json = exportMatcher(matcher);
// and then rehydrate this later
const newMatcher = createMatcherFromExport(json);
const matches = newMatcher.matchAll("/foo/bar/baz");
See benchmark.
Based on original work of charlieduong94/radix-router
by Charlie Duong (MIT)
MIT - Made with ❤️
FAQs
Lightweight and fast router for JavaScript based on Radix Tree
The npm package radix3 receives a total of 881,901 weekly downloads. As such, radix3 popularity was classified as popular.
We found that radix3 demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.