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

es-router

v1.0.1

Published

Routers are great for SPAs! except most routers are tied to your framework. But what if you like to keep upgrading your project? use as much vanilla javascript as possible!

Downloads

8

Readme

es-router

Routers are great for SPAs! except most routers are tied to your framework. But what if you like to keep upgrading your project? use as much vanilla javascript as possible!

That's where es-router comes in. es-router is a vanilla router that can be imported into any project. it uses a pub-sub style of routing, so it's simple to track changes in the url!

setup

to get started you'll need to import es-router and give it a config

import Router from 'es-router';
const router = new Router({
  useHash: false,
  home: 'home',
  notStrictRouting: false,
  base: '/testurl/`,
  routes: [
    {
      name: 'home',
      route: '/',
    },
    {
      name: 'path1',
      route: '/path',
    },
    {
      name: 'routes',
      route: '/route/:router',
    },
    {
      name: 'floater',
      route: '/something/:router',
    }],
});
export {router};

let's go through each option.

useHash

useHash defines whether or not you'd like to use pushState, or the equivalent to Angular's html5Mode. This is a great option if your base is not consistent or you're on IE9

home

this is a default path that you'd like to go to if the url doesn't match to any of the paths you've defined

notStrictRouting

this gives you the ability to go to any path you'd like, regardless of whether you've declared it in this config or not

base

this is the base url for your application if you aren't using the hash. if this isn't declared and you are using useHash, it will try and retrieve the base from the base tag in the html5Mode

routes

routes are the locations that you've defined to use in your application. the : means that it is a variable route, and can be changed

using in application

Okay, you've configured your router, now to use it!

first, import your router in your js where you'd like to control the view

import {router} from './active-router';

router.path('/something/somethingElse);

this will cause the url to update to this path, and it fires two subscription events, called beginRouteChange and finishRouteChange.

you subscribe to these using

router.subscribe('finishRouteChange', (oldPath, newPath) => {
});

oldPath is what the path routed from, newPath is the path that was routed to. the structure comes back similarly to the object that was declared in the config

{
  name: 'floater',
  route: '/something/:router',
  variablePath: {
    router: 'somethingElse',
  }
}

you'll notice that there's a third key called variablePath which maps the name of the variable passed in to the path that was called.

query parameters

but how powerful is a router without query parameters? es-router uses persistent query parameters and will update them using router.search().

router.search() has four uses

//will set the query parameter `new-param` to the value of `truth`
router.search('new-param', 'truth');
//will set all keys of the object to the value in the url
router.search({'new-param': 'truth', 'old-param': 'also-true'});
//will delete the query paramter `new-param`
router.search('new-param');
//will return an object of all query paramters in the url
router.search()

any time a query parameter is updated, the event paramChange will be fired, which cane be subscribed to the same way the paths are subscribed to

router.subscribe('paramChange', (params) => {
});

params is the equivalent of calling router.search().

examples

The idea behind the router is that you install it in your favorite SPA and use it to toggle components. Here's how it is being used in both Angular and React.

Angular

import angular from 'angular';
import routes from './routes';

angular.module('myApp')
.component('myComponent', {
  template: `
  <path-one ng-if="$ctrl.path === 'path1'"></path-one>
  <routes ng-if="$ctrl.path === 'routes'"></routes>
  <home ng-if="$ctrl.path === 'home'"></home>
  <floater ng-if="$ctrl.path === 'floater'"></floater>`,
  controller: ['$rootScope', '$timeout', class MyComponent {
    constructor($rootScope, $timeout) {
      const initialRoute = routes.getState();
      this.path = initialRoute.name;
      routes.subscribe('finishRouteChange', (fromState, toState) => {
        this.path = toState && toState.name;
        if (!$rootScope.$$phase) {
          $timeout(() => {
            $rootScope.$apply();
          });
        }
      });
    }
  }],
  controllerAs: '$ctrl',
});

React

import React, { Component } from 'react';
import routes from './routes';

class App extends Component {
  componentWillMount() {
    routes.subscribe('finishRouteChange', (fromState, toState) => {
        this.setState({ path: toState && toState.name});
      });
  }
  render() {
    switch(this.state.path) {
      case 'path1' {
        return (<pathOne />);
      }
      case 'routes' {
        return (<routes />);
      }
      case 'home' {
        return (<home />);
      }
      case 'floater' {
        return (<floater />);
      }
    }
  }
}