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

routility

v0.1.1

Published

Routing utility converting urls to hash maps

Downloads

3

Readme

Routility

npm version Circle CI

Sauce Test Status

A generic routing utility navigate users in browser and convert URLs to objects, so your application logic can consume them without wrestling with a specify API, which uncovers the endless possibilities of doing awesome stuff. Because all you need to do now is managing data instead of learning 3rd party libraries. This works exceptionally great with architectures that are reactive or has a single source of truth, e.g. React, Redux or Flux in general.

Before reaching 1.0, API and semantic will change between minor versions.

Usage

var Routility = require('routility');
var r = Routility.r;
var redirect = Routility.redirect;

var routes = ( // Parentheses are not required, it looks nice to align all the routes
  r('/', 'root', [
    redirect('/', '/login'),
    r('/login', 'login'),
    r('/user/:id', 'user', [
      r('/', 'index'), // If you don't define "/", "/user/:id" won't be handled
      r('/profile', 'profile')
    ])
  ])
);

var navTo = Routility.start(routes, function (state) {
  // This handler will be called as soon as you call "navTo"
});

// Will first change url to "/user/123" and return new state
// Those will happen synchronously
navTo('/user/123');
// This returns current state:
// {
//   root: {
//     user: {
//       id: '123',
//       index: {}
//     }
//   },
//   queryParams: {}
// }

Why

Many front end frameworks and view libraries have to manage state in their application. There are many solutions to manage state. But integrated route management is often overlooked. Most libraries will have a router abstraction to deal with routing in the browser. A common mistake of those abstractions is that they didn't treat routes as data. This caused their API is be highly coupled with specific use cases.

A route to a browser application should just be some states. Nothing more. When we interpret route at this level, the application logic can be easily fully integrated with the rest of state management layer, which enables lots of possibilities.

API

r(path, name, subRoutes) -> RouteDefinition

Define route hierarchy. (The ( and ) are not necessary, it just help align the r function calls, makes them nice to read.) name argument is related to the structure of state object.

var routes = ( // Parentheses are not required
  r('/', 'root', [
    redirect('/', '/login'),
    r('/login', 'login'),
    r('/user/:id', 'user', [
      r('/', 'index'), // If you don't define "/", "/user/:id" won't be handled
      r('/profile', 'profile')
    ])
  ])
);

redirect(path, targetPath)

path has the same semantic as the path argument of r function. targetPath is an absolute path to the new path.

start(definition, handler, [opts]) -> navTo

Start routing for browser. Return a navTo helper. handler will only be called when user use browser back and forward button to navigate. Refer to Structure of state section to learn what the state object will look like.

var navTo = start(definition, function (state) {
  // You can notify the application that state has changed
});

handler(state)

state is same as state object returned by navTo function

options

  • opts.browserHistory = false Use HTML5 history. If false, will use url hashtag. If true, will use the entire path.

navTo([path]) -> state|null

Navigate to target URL.

  • If called without arguments, will return current state.
  • If hit a redirect route, it will go directly to the redirect target path, and only return the final state object. The final state object will contain two additional properties, redirectTo and redirectFrom. (Refer to Structure of state section to learn what the state object will look like.)
navTo(); // return current state, no effect on url
navTo('/');
navTo('/login');
navTo('/user/123');
navTo('/user/123/profile?q=abc');

parse(definition, path) -> state|null

A helper to take a definition and a path, and generate the state object for that path. This function has no effect on browser url. It just return the state object, and that's it! This can be used to run on Node.js (server) environment. It will return the final state object if it the path hits a redirect route. It will return null if path matches no route (both when directly or after redirect).

Structure of state

// A example route definition
r('/', 'root', [
  r('/', 'index'),
  redirect('/sign-in', '/login'),
  r('/login', 'login'),
  r('/user/:id', 'user', [
    r('/', 'index'),
    r('/profile', 'profile')
  ])
]);

// For '/'
{
  "root": {
    "index": {}
  },
  "queryParams": {}
}

// For '/login'
{
  "root": {
    "login": {}
  },
  "queryParams": {}
}

// For '/sign-in'
{
  "root": {
    "login": {} // Show login instead because it hits a redirect route
  },
  "redirectFrom": "/sign-in", // Redirect route property
  "redirectTo": "/login", // Redirect route property
  "queryParams": {}
}

// For '/user/123'
{
  "root": {
    "user": {
      "id": "123" // Notice "id" will always be a string
    }
  },
  "queryParams": {}
}

// For '/user/123/profile?q=abc&name=one'
{
  "root": {
    "user": {
      "id": "123",
      "profile": {}
    }
  },
  "queryParams": {
    "q": "abc",
    "name": "one"
  }
}