npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

micro-regex-router

v1.0.3

Published

Dead simple routing with regexes. Framework agnostic, but comes with a react/redux example project

Downloads

4

Readme

NPM version Size Build Status Coverage Status Dependency Status Known Vulnerabilities PRs Welcome

micro-regex-router

Routing is one of those things you can make as complicated as you like. Or as simple as you like. This library (just a 30-line ES5 function), takes an array of routes (objects with a regex under the "pattern" key) and returns a router function. Pass a string into that function and it returns the matched route. Any named capture group in the regex will be returned, allowing you to use parameters.

Minimalistic, low-level, flexible and easy.

The only extra feature is using middleware on a route, allowing you to skip or overwrite the route according any condition you like (See API).

Installation

npm install micro-regex-router

Usage example

Please refer to example-react to see a fully functioning react/redux project using this router with the history package.

Or hit npm i and npm run dev and look at http://localhost:8080

The Router component from this project is copied below:

import React from 'react';
import { connect } from 'react-redux';

import createRouter from 'micro-regex-router';

import Page from 'components/Page';
import Admin from 'components/Admin';
import Home from 'components/Home';
import NotFound from 'components/NotFound';
import AccessDenied from 'components/AccessDenied';
import { selectPathName } from 'store/location';
import { selectLoggedIn } from 'store/user';

const protect = route => ({
	...route,
	middleware: (data, { loggedIn }) => loggedIn ? data : { component: AccessDenied },
});

const router = createRouter([
	protect({ pattern: '^/admin/?$', component: Admin }),
	{ pattern: '^/page/(?<param>.*)/?$', component: Page },
	{ pattern: '^/$', component: Home },
	{ pattern: '', component: NotFound },
]);

function Router({ pathname, loggedIn }) {
	const { component: Component, ...params } = router(pathname, { loggedIn });
	return <Component params={params} />;
}

export default connect(
	state => ({
		pathname: selectPathName(state),
		loggedIn: selectLoggedIn(state),
	})
)(Router);

API

The route type

A route is an object containing a RegExp or string under the key "pattern", an optional function "middleware" that can take the return value and transform it and any other data associated with the route (these could be React components). Any parameters in the pattern should be assigned to named capture groups.

Example:

{
	pattern: '/users/(?<id>[0-9]+)',
	component: UserPage
}

The middleware type

Middleware takes two arguments:

  1. An Object containing anything what the route would normally return
  2. An optional second argument containing the second argument passed to router

It returns:

  • An Object overwriting the return value, or a Boolean, dictating if the route should be returned (else it is skipped).

Examples:

// Authentication, taking an extra arg whether the user is logged in
(params, { loggedIn }) => loggedIn ? true : AccessDeniedPage

// Taking only odd id's
(params) => params.id % 2 === 1

// Mutating a parameter
(params) => {...params, value: doSomething(params.value)}

Default export: createRouter

function ([route]) -> function router(path, extraArgs?) -> Object

Creates a router function based on the given routes.

This router function takes the path for which the correct route will be returned and an optional second argument that is passed to any middleware any of the matched route(s) may have. It returns all the parameters of the route, being a combination any keys on the route object that are not pattern or middleware and any named capture groups matched.

arguments

  • routes (Array< route >): The routes the router will create (in order of precedence).

returns

  • router function(path (string), extraArgs? (any)) -> result (Object):
    • path (string): The string to match.
    • extraArgs (any): Any arguments to pass to the middleware, in case it is encountered.