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

uniloc-michr-fork

v0.3.4

Published

Universal JavaScript Route Parsing and Generation in 1.3KB compressed/minified

Downloads

3

Readme

uniloc

Uniloc is a utility to match URIs to named routes, and to generate URIs given a route name and options.

In contrast to many popular routing systems, uniloc's behavior is not affected by the order in which routes are specified.

Additionally, by assuming that each location in your app has a single canonical URI/method, and assuming the format of your URIs follow a number of fairly uncontroversial conventions, uniloc is able to detect conflicts in your config which may would have resulted in hours of debugging pain in a first-match-wins system.

Installation

install with npm:

npm install uniloc-michr-fork

Example

/*
 * Configure routes and aliases
 */

var ROUTER = uniloc(
  // Routes
  { 
    listContacts: 'GET /contacts',
    postContact: 'POST /contacts',
    editContact: 'GET /contacts/:id/edit',
  }, 

  // Aliases
  {
    'GET /': 'listContacts',
  }
);


/*
 * Lookup URIs
 */

ROUTER.lookup('/contacts/13/edit?details=true')
// {name: 'editContact', options: {id: 13, details: true}}

ROUTER.lookup('/?page=10')
// {name: 'listContacts', options: {page: 10}}

ROUTER.lookup('/?page=10', 'PATCH')
// null


/*
 * Generate URIs
 */

ROUTER.generate('listContacts', {page: 10})
// '/contacts?page=10'

ROUTER.generate('editContact', {id: 'james'})
// '/contacts/james/edit'

Location strings

Uniloc assumes that each of the locations in your app is associated with a single HTTP method, and a single URI or set of URIs where the only varying parts represent the route parameters.

Given this assumption, it is possible to represent each location in the application with a location string, which includes the HTTP method, and a URI where the varying parts are marked as parameters. For example:

PATCH /contacts/:contactId/photos/:photoId

Format

  • Location strings must consist of a HTTP method and URI template joined by whitespace
  • The HTTP method must be in UPPERCASE
  • The URI template must start with a /, end without a /, and must not include the ? or # characters
  • Route parameters are delineated with / character or the end of the string, and start with a : character followed by the route parameter's name.

API

uniloc(routes, aliases={}) -> {lookup, generate}

uniloc takes two objects:

  • routes: an object mapping route names to their canonical location string
  • aliases: optional an object mapping other location strings to the routes named in routes

Route names are just strings. The . character is reserved, as it may be used in a future release to specify hierarchy.

While two routes or aliases cannot use the same location string, a route without a route parameter in one position can overlap another URI with a route parameter in the same position. In this case, the route without the route parameter will be given priority.

Example

var ROUTER = uniloc(
  // Routes
  { 
    listContacts: 'GET /contacts',
    postContact: 'POST /contacts',
    editContact: 'GET /contacts/:id/edit',
  }, 

  // Aliases
  {
    'GET /': 'listContacts',
  }
);

lookup(URI, method='GET') -> {name, options}

lookup is one of the properties of the object returned by uniloc.

lookup takes a URI and HTTP method, and returns the route name which matches that URI/method, with an options object containing any route and query parameters. If no location is defined which matches the passed in URI/method, it returns null.

Examples

ROUTER.lookup('/contacts/13/edit?details=true')
// Returns {name: 'editContact', options: {id: 13, details: true}}

ROUTER.lookup('/?page=10')
// Returns {name: 'listContacts', options: {page: 10}}

ROUTER.lookup('/?page=10', 'PATCH')
// Returns null

generate(name, options) -> URI

generate is one of the properties of the object returned by uniloc.

generate takes a route name and options object, and returns a URI string based on the canonical URI for the given route. Options are substituted in for route parameters where possible, and the remaining options are appended as a query string.

If the given route name doesn't exist, an exception is thrown.

Examples

ROUTER.generate('listContacts', {page: 10})
// Returns '/contacts?page=10'

ROUTER.generate('editContact', {id: 'james'})
// Returns '/contacts/james/edit'

ROUTER.generate('editFoo', {id: 'james'})
// Exception!