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

lite-router

v1.1.4

Published

A very light router based on history package

Downloads

8

Readme

lite-router

npm version Build Status

Introduction

I needed some simple routing solution for my rxjs-experiments project (not based on any framework or libraries that provides routers out of the box). So, I made a micro router, based on [email protected].

The next step was to publish it as an npm package (my main interest being the build workflow to setup on an es6+ code base specific to npm packages with multiples files).

Read the blog post about the packaging workflow to npm in multiple formats

Features

  • history support (back button)
  • mutiple routes / customizable routes
  • deferred mounting (only mount a route when a promise is resolved)
  • params support (optional)

This package is available as multiple formats: commonjs, umd and ecmascript modules.

Install

npm install lite-router

Use

Code example

import router from 'lite-router';

/**
 * Simple handler on route "/home" and any routes (since using the wildcard "*")
 */
const mountHome = () => {
  // code to run on route match
  document.getElementById('home-content').style.display = 'block';
  const unMount = () => {
    // code to run on route leave (you wight wanna clean listeners or others)
    document.getElementById('home-content').style.display = 'none';
  };
  // always return the unMount method
  return unMount;
};

/**
 * Param matching on route "/router/posts/:category/:title/edit"
 * params will contain an object like {category: "foo", title: "bar"}
 *
 * Also using Regexp matching: /^\/router\/user\/([^\/?#]+)\/([^\/?#]+)$/i
 */
const mountParams = ({ location, params, history }) => {
  document.getElementById('params-content').style.display = 'block';
  console.log(location, params, history);
  const unMount = () => {
    document.getElementById('params-content').style.display = 'none';
  };
  return unMount;
}

const routes = [
  { pattern: '/', handler: mountHome },
  { pattern: '/router/posts/:category/:title/edit', handler: mountParams },
  { pattern: /^\/router\/user\/([^\/?#]+)\/([^\/?#]+)$/i, handler: routerHandler },
  { pattern: '/defered-mounting', handler: mountHome, resolve: new Promise(res => setTimeout(res, 1000)) } // will mount mountHome handler on /defered-mounting once the resolve promise is resolve
  { pattern: '*' handler: mountHome }
];

// init router and return the unlisten function to use eventually when you'll be done
const unlisten = router(routes);

More examples

You'll find the example folder which contains basic examples. A DEMO is available here.

More advanced examples can be found on rxjs-experiments:

Api

Import router

import router from lite-router

Init routes

router(routes)

The routes array contains objects with the following properties:

  • pattern: can be:
    • a simple string - ex: /foo/bar
    • a string containing matching params (identified with a colon) - ex: /posts/:category/:title/edit
      • you will retrieve the matched params in the params parameter of the handler
    • a regexp - ex: /^\/router\/user\/([^\/?#]+)\/([^\/?#]+)$/i
  • handler: the function that will mount your view, must return an unmount function (to cleanup eventually)
    • signature: ({ location, params, history }) => [Function] unmount
  • resolve: (optional) a Promise, that when resolved, will let the matched handler mount

Contributers

Install

git clone https://github.com/topheman/lite-router.git
cd lite-router
yarn

Install, then eventually, npm link the library to use it in local.

In your own project where you want to locally test lite-router, just run npm link lite-router.

Build

  • One shot
    • all formats: npm run build
    • commonjs (output in lib dir): npm run build:commonjs
    • ecmascript module (ouput in es dir): npm run build:es
    • umd (output as dist/lite-router.js) : npm run build:umd
    • umd - minified (output as dist/lite-router.min.js) : npm run build:umd:min
  • Watch mode:
    • commonjs: npm run build:watch or npm run build:commonjs:watch
    • umd: npm run build:umd:watch
    • es: npm run build:es:watch

Test

  • One shot: npm test (will also lint the source code)
  • Only unit tests: npm run jest
  • Watch mode: npm run jest:watch

The tests are in the tests folder, it is ran via jest.

Publish

Publish npm package
npm run build
npm publish
Publish gh-pages

The gh-pages tracking branch is binded in build/dist folder. If you haven't it yet:

mkdir build
cd build
mkdir dist
cd dist
git init
git remote add origin https://github.com/topheman/lite-router.git
git fetch origin gh-pages
git checkout gh-pages

If you already have a build/dist folder, to update the github pages:

npm run example:build
cd build/dist
git add .
git commit -m "Update gh-pages"
git push origin gh-pages

Copyright 2017 © Christophe Rosset

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. The Software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the Software.