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

@zensen/router

v1.1.6

Published

A declarative router for native web components

Downloads

13

Readme

zensen-router

A declarative router for native web components.

Features

  • Listens to any changes in the route
  • Gives the ability to cancel route changes
  • Easy way of parsing route and querystring params
  • Conditionally invoke behahvior based on the current route

Install

Using npm:

$ npm install @zensen/router

Using yarn:

$ yarn add @zensen/router

API

Configuring routing settings (hash-based routing is enabled by default)

configure({ useHash: false })

Getting routes with the following window URL:

http://www.my-domain.com/sub-route/#/users/123/photos/456

import { getRoute } from '@zensen/router'

// hash-routing enabled
const route = getRoute() //  /users/123/photos/456

// hash-routing disabled
const route = getRoute() //  /sub-route/#/users/123/photos/456

Navigating to a different route

import { navigate } from '@zensen/router'

// hash-routing enabled
navigate('/users/123') // http://www.my-domain.com/#/users/123

// hash-routing disabled
navigate('/users/123') // http://www.my-domain.com/users/123

Redirecting to a different route

Note: This is just like navigate(), except it doesn't push to the browser's history.

import { redirect } from '@zensen/router'

// hash-routing enabled
redirect('/users/123') // http://www.my-domain.com/#/users/123

// hash-routing disabled
redirect('/users/123') // http://www.my-domain.com/users/123

Getting route params

import { getParams } from '@zensen/router'

// Example window URL:
// http://www.my-domain.com/#/users/123/photos/456
const { userId, photoId } = getParams('/users/:userId/photos/:photoId')
// Example when explicity providing route
const { userId, photoId } = getParams(
  '/users/:userId/photos/:photoId',
  '/users/123/photos/456'
)

Getting querystring params

import { getQuerystring } from '@zensen/router'

// Example window URL:
// http://www.my-domain.com?search=asdf&sort=asc
const { search, sort } = getQuerystring()
// Example when explicity providing route
const { search, sort } = getQuerystring(
  'http://www.my-domain.com?search=asdf&sort=asc'
)

Detecting route changes

import { EVENT_ROUTE_CHANGE } from '@zensen/router'

window.addEventListener(EVENT_ROUTE_CHANGE, e =>
  console.info('changing route:', e.detail)
)

Canceling route changes

import { EVENT_ROUTE_SHOULD_CHANGE } from '@zensen/router'

window.addEventListener(EVENT_ROUTE_SHOULD_CHANGE, e => {
  // block all route changes to /users/
  if (e.detail === '/users/') {
    e.preventDefault()
  }
})

Detecting when route changes are canceled

import { EVENT_ROUTE_CANCEL } from '@zensen/router'

window.addEventListener(EVENT_ROUTE_CANCEL, e =>
  console.info('route change canceled:', e.detail)
)

Matching against a route

import { matchRoute } from '@zensen/router'

// Example window URL:
// http://www.my-domain.com/#/users/123/photos/456?filter=upload-date&sort=asc
const result = matchRoute(
  '/users/:id/',
  (tail, { querystring, params }) => `
    <p>Tail URL: ${tail}</p>
    <p>User: ${params.id}</p>
    <p>Filter: ${querystring.filter}</p>
    <p>Sort: ${querystring.sort}</p>
  `,
  '', // do not explicity provide a route, so that it uses window.location
  false // set to false to allow routes to be longer than paths it's matched against
)

/*
  Result:
    <p>User: 123</p>
    <p>Tail URL: /photos/456</p>
    <p>Filter: upload</p>
    <p>Sort: asc</p>
 */
import { matchRoute } from '@zensen/router'

const result = matchRoute(
  '/photos/:id/',
  (tail, { id }) =>
    `
    <p>Photo: ${id}</p>
    <p>Tail URL: ${tail}</p>
  `,
  '/photos/456' // providing the tail route from the previous example
)

/*
  Result:
    <p>Photo: 456</p>
    <p>Tail URL: /</p>
 */

Matching against multiple possible routes

const items = [
  {
    path: '/users/',
    resolver: tail => `
      <p>Tail Route: ${tail}</p>
    `
  },
  {
    path: '/photos/',
    resolver: tail => `
      <p>Tail Route: ${tail}</p>
    `
  },
  {
    path: '/',
    resolver: () => `<p>404 - Not Found</p>`
  }
]
import { matchRouteSwitch } from '@zensen/router'

// Example window URL:
// http://www.my-domain.com/#/users/123
const result = matchRouteSwitch(items)

/*
  Result:
    <p>Tail Route: /123</p>
 */
import { matchRouteSwitch } from '@zensen/router'

// Example window URL:
// http://www.my-domain.com/#/photos/456
const result = matchRouteSwitch(items)

/*
  Result:
    <p>Tail Route: /456</p>
 */