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

route-maker

v1.1.11

Published

Define and build URL pathes

Downloads

8

Readme

route-maker

For defining, building and matching named routes.

Framework agnostic - just works with strings, can be used in browser and node.js.

Zero dependency, production used.

Install

npm install route-maker

Defining routes

// routes.js of your project

import route from 'route-maker'

export default {
  root: route('/'),
  items: route('items'),
  item: route('items/:id')
}

Using routes

Now in router code to get original route string, react for example:

import routes from './routes'

<Route path={routes.item()} />
// Same as:
<Route path='/items/:id' />

And for get path with id:

import routes from './routes'

<Link to={routes.item({id: 123})} />
// Same as:
<Link to='/items/123' />

If param was not matched in string, it will become URI parameter:

routes.items({search: 'phrase'}) //= '/items?search=phrase'

match compares pattern and actual path, gives hash with parameters.

import match from 'route-maker/match'

match('path', 'path') // {}
match('path', 'wrongPath') // null
match('path/:type/path/:id/path', 'path/type_value/path/123/path') // {type: 'type_value', b: '123'}

URI params are matched too, notice that : embedded params are stronger.

import route from 'route-maker'

route(':a/:b').match('1/2?a=another_value&c=3') // {a: 'a', b: '2', c: '3'}

Positional parameters

Parameters can be passed as plain arguments

import routes from './routes'

routes.item(123) === '/items/123'
routes.item(123, {param: 'value'}) === '/items/123?param=value'
routes.item(123, {param: 'value'}, {prefix: 'api'}) === '/api/items/123?param=value'

Positional parameters can be used with hash-provided parameters:

import route from 'route-maker'

const url = route('path/:one/:two')
url(1, {two: 2}) === '/path/1/2'

Options with examples

Options can be set when creating and calling route.

Prefix example:

import route from 'route-maker'

let path = route('path', {prefix: 'api'})
path() === '/api/path'
path({}, {prefix: 'api/v2'}) === '/api/v2/path' // empty params as first argument, can pass null instead

Can be changed directly:

import route from 'route-maker'

route.options.prefix = 'api'

const path = route('path')
path() === '/api/path'

Imported constructor can be extended with config method:

import route from 'route-maker'

const apiRoute = route.config({prefix: 'api'})
let path = apiRoute('path')
path() === '/api/path'

let path = route('path')
path() === '/path'

Prefix and defaults example

import route from 'route-maker'

const apiRoute = route.config({prefix: 'api/v:apiVersion', defaults: {apiVersion: 1}})
let path = apiRoute('path')
path === '/api/v1/path'

let path = apiRoute('path')
path({apiVersion: 2}) === '/api/v2/path'

Default values can be dynamically calculated:

import route from 'route-maker'

let currentUser = {}
const userRoute = route.config({prefix: ':userType', defaults: {
  userType: () => currentUser.type
}})

let path = userRoute('path')

currentUser.type = 'customer'
path() === '/customer/path'

currentUser.type = 'admin'
path() === '/admin/path'

Options list

url string, full url if needed. Parameters inside are not allowed.

prefix string, adds prefix to resulting strings. Parameters inside are allowed.

prependSlash boolean, default true, adds slash at the begging of path if it is absent

assign object, will call Object.assign on route instances to add them provided properties

defaults object, default parameters. Works with parameters in prefix. If object value is function then it will be called, result will come to url.