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

@arve.knudsen/sheet-router

v4.2.5

Published

Fast, modular client router

Downloads

6

Readme

sheet-router stability

npm version build status test coverage downloads js-standard-style

sheet-router is a fast, modular client-side router. It enables view composition and is tuned for performance by statically declaring routes in a radix-trie. Weighs 1.5KB minified and gzipped.

Installation

$ npm install sheet-router

Features

  • View composition through functions
  • Tuned for performance by generating a radix-trie
  • Not bound to any framework
  • Minimal dependencies and tiny code size
  • HTML5 history support
  • Catch and handle <a href=""> links

Usage

sheet-router tries to make routing understandable and pleasant to work with. It does so by using a lisp-like structure which is internally compiled to an efficient data structure. Here each route takes either an array of children or a callback, which are then translated to paths that take callbacks

const sheetRouter = require('sheet-router')
const html = require('bel')

// default to `/404` if no path matches
const router = sheetRouter({ default: '/404' }, [
  ['/', (params) => html`<div>Welcome to router land!</div>`],
  ['/:username', (params) => html`<div>${params.username}</div>`, [
    ['/orgs', (params) => html`<div>${params.username}'s orgs!</div>`]
  ]],
  ['/404', (params) => html`<div>Oh no, path not found!</div>`],
])

router('/hughsk/orgs')

history

Interacting with the browser history is a common action, sheet-router supports this out of the box. When the forwards or backwards buttons in the browser are clicked, or history.back / history.go are called sheet-router will update accordingly.

const history = require('sheet-router/history')
history(function (href) {
  router(href)
  console.log('history changed: ' + href)
})

hash

Interacting with hash changes is often a common fallback scenario for those who don't have support for browser history. Whenever a hashchange event is triggered, sheet-router will trigger an update as seen below. However in order to match hash prefixed routes, the hash-match module can be used to normalize routes (ex: #/foo becomes /foo).

const hash = require('sheet-router/hash')
const match = require('hash-match')
hash(function (href) {
  router(match(href))
  console.log('hash location changed: ' + href)
})

href

In HTML links are represented with <a href=""> style tags. Sheet-router can be smart about these and handle them globally. This way there's no need to attach specific listeners to each link and static HTML templates can be upgraded seemlessly to include single-page routing.

const href = require('sheet-router/href')
href(function (href) {
  router(href)
  console.log('link was clicked: ' + href)
})

You can ignore specific links that you do not want to process through routing by adding the data-no-routing attribute.

<a href="/my-external-link" data-no-routing>Non routed link</a>
<a href="/another-external-link" data-no-routing="true">Not routed either</a>

Also, if you pass an optional root node reference as a second argument to href, it will never intercept clicks outside that node. This is useful when your app is confined to a widget in a larger document.

href(function (href) {
  router(href)
  console.log('link was clicked: ' + href)
}, document.getElementById("app-root"))

qs

Sometimes query strings must be decoded. In order to do this, the ./qs.js file is included.

const qs = require('./qs')
qs('https://www.npmjs.com/search?q=query+string')
// => { q: 'query+string' }

walk

Sometimes it's necessary to walk the trie to apply transformations. In order to access the raw callback and prevent unnecessary function calls we need to disable the default thunking mechanism by passing { thunk: false }:

const sheetRouter = require('sheet-router')
const walk = require('sheet-router/walk')

const router = sheetRouter({ thunk: false }, [
  ['/multiply', (x, y) => x * y],
  ['/divide', (x, y) => x / y]
])

walk(router, (route, cb) => {
  const y = 2
  return function (params, x) {
    return cb(x, y)
  }
})

router('/multiply', 4)
// => 8
router('/divide', 8)
// => 4

We could change our transformed function to be thunked by changing walk to return a function, and setting { thunk: 'match' } so only the match function thunks. This is pretty advanced stuff, so don't sweat it too much - but it's super useful to create performant frameworks!

const router = sheetRouter({ thunk: 'match' }, [
  ['/foo', (x, y) => x * y],
  ['/bar', (x, y) => x / y]
])

walk(router, (route, cb) => {
  const y = 2
  return function (params) {
    return function (x) {
      return cb(x, y)
    }
  }
})

router('/multiply', 4)
// => 8
router('/multiply', 4)
// => 8 (but this time around this is computed faster)
router('/divide', 8)
// => 4

create-location

Sometimes you want to mirror the browser location API inside an object to use inside a framework. The hard part is to compute the new href from a set of changes. create-location provides an API to do just that:

const createLocation = require('sheet-router/create-location')

document.location = '/foo/bar#hey?beep=boop'
var location = createLocation()
// => {
//    pathname: '/',
//    hash: '#hey',
//    search: { beep: 'boop' },
//    href: '/foo/bar#hey?beep=boop'
//  }

const hashPatch = { hash: '#oh-no' }
var location = createLocation(location, hashPatch)
// => {
//    pathname: '/',
//    hash: '#oh-no',
//    search: { beep: 'boop' },
//    href: '/foo/bar#oh-no?beep=boop'
//  }

const uriPatch = '/hey/hello'
var location = createLocation(location, uriPatch)
// => {
//    pathname: '/hey/hello',
//    hash: '',
//    search: { },
//    href: '/hey/hello'
//  }

virtual-dom example

const render = require('virtual-dom/create-element')
const sheetRouter = require('sheet-router')
const h = require('virtual-dom/h')
const hyperx = require('hyperx')

const html = hyperx(h)

const router = sheetRouter([
  ['/foo/bar', (params, h, state) => html`<div>hello world!</div>`]
])

const node = render(router('/foo/bar', h, { name: 'Jane' }))
document.body.appendChild(node)
<body>
  <div>hello world</div>
</body>

react example

const sheetRouter = require('sheet-router')
const render = require('react-dom')
const hyperx = require('hyperx')
const react = require('react')

const html = hyperx(react.createElement)

const router = sheetRouter([
  ['/foo/bar', (params, h, state) => html`<div>hello world!</div>`]
])

render(router('/foo', react.createElement, { name: 'Jane' }), document.body)
<body>
  <div>hello world</div>
</body>

API

router = sheetRouter(opts?, [routes])

Create a new router from a nested array. Takes an optional options object as the first argument. Options are:

  • opts.default: defaults to '/404', default path to use if no paths match
  • opts.thunk: defaults to true. Toggle if callbacks should be thunked or not. Can be set to 'match' to only have the returned router.match() function expect thunks to exist. Useful to write a custom walk function that creates a different signature

router(path, [,...])

Match a route on the router. Takes a path and an arbitrary list of arguments that are then passed to the matched routes. Cleans urls to only match the pathname.

history(cb(href))

Call a callback to handle html5 pushsState history.

href(cb(href))

Call a callback to handle <a href="#"> clicks.

See Also

License

MIT