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

vs-router

v0.0.1

Published

A small, framework-agnostic, event-based, client-side router.

Downloads

4

Readme

The Very Simple Router (vs-router) is:

  • a router library for the browser
  • very small (<2kB gzipped)
  • very fast
  • synchronous
  • event-based
  • framework-agnostic
  • really very simple

Installation

npm install --save vs-router

// or for yarn:
yarn add vs-router

Usage

import { createRouter } from 'vs-router';

const router = createRouter('/', {
  users: '/users',
  user: '/users/:id'
});

router.addListener('transition', logTransition);

function logTransition({ last, next }) {
  console.log(`Last: ${JSON.stringify(last)}`);
  console.log(`Next: ${JSON.stringify(next)}`);
}


// go to the users route
router.navigate('users');

/*
console output:

Last: { name: 'root', params: null }
Next: { name: 'users', params: null }
*/

router.navigate('user', { id: 1 });

/*
console output:

Last: { name: 'users', params: null }
Next: { name: 'user', params: { id: 1 } }
*/

router.back();

/*
console output:

Last: { name: 'root', params: null }
Next: { name: 'users', params: null }
*/

router.forward();

/*
console output:

Last: { name: 'users', params: null }
Next: { name: 'user', params: { id: 1 } }
*/

router.go(-1);

/*
console output:

Last: { name: 'root', params: null }
Next: { name: 'users', params: null }
*/

Removing listeners

router.removeListener('transition', logTransition);

Destroying the router

router.destroy();

root route

The root path is added automatically, according to the first parameter passed to createRouter. Transitioning back to the root therefore:

router.navigate('root');

The notFound route

A notFound route is added automatically and defaults to /404. In order to override it, register a new root with the notFound name:

const router = createRouter('/', {
  notFound: '/my-not-found-page',
});

Registering new routes

New routes can be added, by passing a name and path pattern:

router.register('userPosts', '/posts/:userId');
router.navigate('userPosts', { userId: 1 }); // works!

Patterns API

URL parameters:

Pattern: /posts/:user
Params:  { user: 'admin' }
Path:    /posts/admin
Pattern: /posts/:user/:tag
Params:  { user: 'admin', tag: 'important' }
Path:    /posts/admin/important

Query parameters:

Pattern: /posts?page=:page
Params:  { page: 1 }
Path:    /posts?page=1

Hash:

Pattern: /posts#:section
Params:  { section: 'comments' }
Path:    /posts#comments

Optional query parameters

Pattern: /posts?page?=:page
Params:  {} // or { page: null }
Path:    /posts

List parameters

Pattern: /posts?id*=:ids
Params:  { ids: [1, 2] }
Path:    /posts?id=1&id=2

Regex can also be used for the patern, so for advanced usage, see the tests.

Credit

The parser is a fork of the excellent teki URL parsing library by Philip Nilsson. It's the beating heart of very-simple-router and is essentially what allows it to stay small and very, very fast.