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

@altrdpdgm/router

v2.0.0

Published

A JavaScript routing module.

Downloads

2

Readme

altrd-router

A JavaScript routing module.

altrd-router is a lighweight vanilla JavaScript client-side router for single page applications. It is a refinement on Joakim Carlstein's proof of concept for a 20-line JavaScript router documented here.

This module supports basic client-side routing that is drive by plain JavaScript and can work with any view library or templating language.

Modules

altrd-router consists of (2) modules: Router and RouteTable.

Router

Router houses all business logic of the router via hash-based routing. Router() takes an object with optional arguments and returns an instance of Router that exposes a start and a RouteTable method.

|function/method | description | |---|---| | validateRoute | private function, checks for presence of a given URL on Router's internal routeMap. On undefined route, returns false | |regRoute| private function, adds an argument route to Router's internal routeMap | |resolveRoute| private function, resolves any route that has a resolve method. Resolve methods are functions that request some async data and return promise. Takes a route, a params object containing query parameters in the URL being triggered and a routeTimer.| |execRoute | private function, clears routeTimer, if still in effect, initializes route controller with any data returned from resolveRoute.| |router| private function, triggered on Window's load or hashchange event, takes a URL string, starts routeTimer used to trigger a loading animation, starts route validation.| |getParams| private function, takes a URL and returns an object mapping URL query parameters to their values. | |stripParams| private function, takes URL and removes any query params, returning only the stripped URL.| |start| registers all routes, adds event listeners for the loadand hashchange events. Takes a routeTable and callback to trigger after route registration.| |RouteTable| a constructor returning and instance of RouteTable. See list of methods available on this constructor below.|

RouteTable

RouteTable stores route objects and exposes (2) methods: addRoute and routes.

|function/method | description | |---| ---| |addRoute| adds a route object to an internal routeTable array.| |routes| returns internal routeTable. An array of all registered routes. |

The Route object

Routes are plain JavaScript objects with the following fields:

|field| description| |---|---| |path| [required] string with a leading backslash and without # sign. This is the URL that will trigger the route (e.g. https://mysite.com/index#/list)| |templateURL| string file path to the template to be loaded in the view. |controller| function manages all data and events for a specified view. | |container | string a querySelector to identify the dom node to attach the rendered template to (e.g. #container.) | |resolve| function returns a Promise. Used to delay loading a view until async data is fetched. | |onTimeout| function executes after the timeout duration has been reached. Usually used to show a loading animation on routes that require data fetched from the server.| |timeout| integer a value in milliseconds to wait before calling the onTimeout method.|