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

@techexp/hash-router

v0.2.4

Published

Hash-based Web Router for SPAs

Downloads

10

Readme

HashRouter.js

A library for hash-based routing in the browser.

Introduction

In a Single Page App (SPA), it is convenient to navigate by changing the part of the URL after the hash # character, as this will not cause the browser to reload the page.

This library allows you to associate callbacks with URL paths after the hash, so when a user navigates to a given path, the associated callback is called.

The size of this library, minified and zipped is about 1KB.

Install

You can use this library as either an EcmaScript module, or the old way as a script which you include in your html file.

Install as NPM EcmaScript Module

If you plan to use this package as an NPM module:

    npm install @techexp/hash-router

Install as a Script

If you plan to use this package as a JS script library

    <script src="https://raw.githubusercontent.com/ahabra/hash-router/master/dist/HashRouter-script-min.js"></script>

Alternatively, you can download the file https://raw.githubusercontent.com/ahabra/hash-router/master/dist/HashRouter-script-min.js and use directly. Note that there is a non-minified version at the same location.

Usage

If you installed as an EcmaScript module

import * as HashRouter from '@techexp/hash-router'

If you installed as a Script, the library is available at window.HashRouter

Quick Code Demo

This example shows how to create simple hash-based route handlers:

import * as HashRouter from '@techexp/hash-router'
// or use window.HashRouter if you are using the script.

const router = HashRouter.createRouter()

router.add('a/b', ()=> {
  console.log('Navigated to #a/b')
})
window.location.href = '#a/b'

router.add('a/:id/c', (path, params) => {
  console.log('Navigated to ', path)  // path = a/42/c
  console.log(params) // params = {id: "42"}
})
window.location.href = '#a/42/c'

router.add('a/[x.*z]/c', path => {
  console.log('Navigated to ', path)  // path = a/xBARz/c
})
window.location.href = '#a/xBARz/c'

API

This library consists of a single function createRouter() that returns an object with two prototype methods, add() and go().

add(path, handler)

Add a new hash-path handler.

path

A string that consists of one or more slash / separated parts. The path may start with a hash or slash, but they have no effect. Each part can be one of:

  1. Literal string value. E.g. dept
  2. Parameter prefixed with colon :. E.g. dept/:id
  3. Regular Expression surrounded by square brackets []. E.g. dept/:id/[foo.*]
handler

A callback that takes two arguments, path and params. path is the current path that was navigated to, while params is an object that contain the path paramters. For example:

router.add('dept/:id/employee/:name', (path, params) => {
  consol.log('id', params.id)  // 3
  consol.log('name', params.name)   // bob
})

window.location.href = '#dept/3/employee/bob'

go(path)

Navigate (within the app) to the given path. The path may start with a hash or slash, but they have no effect.

path

The path to navigate to. For example:

router.go('dept/3/employee/bob')

Calling this functions is somewhat similar to setting window.location.href, however this go() function makes sure that a hash character will be used, and it updates the browser's history.