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

halter

v2.5.7

Published

Simple and lightweight but robust router for your web application

Downloads

33

Readme

halter Travis CI build

Simple independent JS router

Usage

First remember to install history NPM module of your choice:

npm install history

Then set up your routes:

import { createMemoryHistory } from 'history';
import { Router } from 'halter';

async function bootstrap() {
    const router = new Router(createMemoryHistory()).addRoute({
        path: '/',
        callback() {
            // it will be triggered when the browser history match "/"
        }
    });

    // very important to listen to changes on history
    await router.init();

    // Stop listening to changes on history
    router.destroy();
}

Labeled routes

Following an similar approach of Angular UI for Angular 1.x we decided to create labeled routes for convenience, so we can build our app based on labeled routes and not on routes paths it self. For example when you create your <Link/> in your React app instead of putting the path to go when the user click the component, you put a convenient label which will match to be the path you need, check the following example:

import { createBrowserHistory } from 'history';
import { Router } from 'halter';
import PostDetail from './components/PostDetail';
import PostsList from './components/PostsList';
import Link from './components/Link';

const router = new Router(createBrowserHistory()).addRoute({
    name: 'post.detail'
    path: '/posts/{id:[0-9]+}',
    callback: ({ params }) => {
        ReactDOM.render(
            <PostDetail postId={params.get('id')} />,
            document.getElementById('app'));
    }
}).addRoute({
    name: 'post.list',
    path: '/posts/list',
    callback: () => {
        ReactDOM.render(<div>
            <PostsList getContents={posts => posts.map(post => (
                <Link key={post.id} to="post.detail" params={new Map().set('id', 'post.id')}>
                    {post.title}
                </Link>
            ))}
        </div>, document.getElementById('app'));
    }
});

router.pushState('post.detail', new Map().set('id', '1040'));

The advantages I see on this approach is that you don't need to keep track of paths itself but the route labels instead. So if you need to change a route path you'll be able to do this without worrying about looking all over through your code searching for that path.

Remember that whenever you don't name your route definitions, it'll be the route path (i.e. if you have /posts/{id:[0-9]+} with no name, the name will automatically be /posts/{id:[0-9]+}).

Listening to changes

Listening to changes on history API it's pretty simple.

const router = new Router();

router.listen((name, params, query) => (
    console.log('New path is %s, params are %o and query is %o', name, params, query);
));

Integration with React

Integration with React it's pretty straightforward. You use <RouterView routes={x} router={y} /> from react-halter NPM module and it'll automatically create necessary routes according to the nested structure provided through routes property. See the steps below:

  1. Install react-halter module:
npm install --save react-halter
  1. Set up routes:
const routes = [{
    path: '/',
    name: 'app',
    component: HomeWrapper,
    childRoutes: [{
        name: 'post',
        path: 'posts/{id:[A-z0-9]+}',
        component: Post
    }, {
        path: 'login',
        name: 'login',
        component: Login
    }]
}, {
    name: 'dashboard',
    path: '/dashboard',
    component: Dashboard,
    onBefore: rules.isGuest
}];
ReactDOM.render(
    <RouterView
        router={router}
        routes={router} />,
    document.getElementById('app')
);