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

@axtk/router

v2.0.1

Published

A lightweight browser history router

Downloads

25

Readme

npm GitHub browser TypeScript

@axtk/router

Core ideas:

  • The route navigation interface might be similar to window.location (route.href, route.assign(), route.replace()).
  • Routes might be handled as arbitrary plain strings, with their nestedness being irrelevant.
  • Regular expressions might be sufficient to express pattern-wise route matching (especially with the advent of the named capturing groups) instead of pattern strings, reserving fixed strings for exact route matching.

Usage

Initialization

// route.js
import {Route} from '@axtk/router';
export const route = new Route();

Subscription to URL changes

Adding a handler of an exact URL path:

import {route} from './route';

let routeListener = route.addListener('/home', ({href}) => {
    console.log(href);
});

of a specific URL path pattern:

route.addListener(/^\/section\/(?<id>\d+)\/?$/, ({href, params}) => {
    console.log(href, params.id);
});

and removing a previously created route listener:

routeListener.remove();

Tracking all route changes:

let unsubscribe = route.onChange(({href}) => {
    console.log(href);
});

and unsubscribing from them:

unsubscribe();

Matching

Checking a route pattern (or an array thereof) if it matches the current path:

// Provided that the current location is '/section/42':
route.match('/home'); // null
route.match('/section/42'); // {}
route.match(/^\/section\/(?<id>\d+)\/?$/); // {0: '42', id: '42'}

Navigation

Getting the current location:

console.log(route.href);

Changing the current location:

// With the current location saved in the browser history
route.assign('/home');
// Without saving the current location in the browser history
route.replace('/home');

Reloading the current location (by re-dispatching the current location event to the subscribers of route):

route.reload();

Jumping to browser history entries:

route.go(-2); // to go 2 entries back in the browser history
route.back(); // = route.go(-1);
route.forward(); // = route.go(+1);

Modifying the behavior

The interaction of a Route instance with window.history or window.location is isolated in a couple of methods that can be overriden in descendant classes to apply custom routing behavior. These methods are: init, transition, go, calcHref.

For example: by default, a Route instance takes into account changes in the pathname, search, and hash portions of the URL combined. To make a Route instance disregard the URL search and hash, the Route class can be extended to redefine the calcHref method:

import {Route, getPath} from '@axtk/router';

export class PathRoute extends Route {
    calcHref(location) {
        return getPath(location, {search: false, hash: false});
    }
}

Also