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

pathparser

v1.0.0

Published

Tiny, flexible JavaScript URL parser / router with no dependencies

Downloads

9

Readme

About

pathparser.js is a tiny, simple-to-use JavaScript URL parser/router with no dependencies. It supports optional named parameters, query string parameter parsing, and automatic parameter assignment:

var params = { format: 'html' };
var router = new PathParser(params);
router.add('users/:userID', function () {
    params.controller = 'users';
});
router.add('groups/:groupID', function () {
    params.controller = 'groups';
});

router.run('users/12345?format=json');
// params.controller == 'users'
// params.userID == 12345
// params.format == 'json'

Usage

Fixed paths

var router = new PathParser;
router.add('users', function () {
    document.title = 'Users';
});
router.add('users/new', function () {
    document.title = 'New Users';
});
router.add('groups', function () {
    document.title = 'Groups';
});

router.run('users');
// document.title is now 'Users'

Optional named parameters

Named parameters in the matching route are accessible from within the this object in the handler function:

var router = new PathParser;
router.add('users/:userID', function () {
    showUser(this.userID);
});

router.run('users/2714');
// showUser(2714)

Routes still match even if there are unmatched named parameters at the end:

var router = new PathParser;
router.add('collections/:collectionID/items/:subset', function () {
    showItemsInCollection(this.collectionID, this.subset);
});

router.run('collections/3424/items');
// showItemsInCollection(3424, undefined);

Routes are matched in the order they're added, so to use a more general route when a named parameter is omitted, simply add it first:

var router = new PathParser;
router.add('users', function () {
    showUsers();
});
router.add('users/:userID', function () {
    showUser(this.userID);
});

router.run('users');
// showUsers();

(This behavior differs from some other routers that use more complex rules to determine which route to choose when there are multiple options.)

Query string parameter parsing

Query string parameters are parsed automatically:

var router = new PathParser;
router.add('users', function () {
    showUsers(this.limit);
});

router.run('users?limit=10');
// showUsers(10)

If a named parameter already exists with the same name as a query parameter, the query parameter will be ignored:

var router = new PathParser;
router.add('users/:userID', function () {
    showUser(this.userID);
});

router.run('users/1?userID=4');
// showUser(1)

This holds true even if a trailing named parameter is omitted, in which case the named parameter will be undefined.

Automatic parameter assignment

You can pass an object to the constructor to have parameters from the matching route automatically assigned to that object, which can be used, among other things, to override a set of default parameters:

var params = {
    format: 'html',
    limit: 25
};
var router = new PathParser(params);
router.add('users/:userID', function () {
    params.controller = 'users';
});
router.add('groups/:groupID', function () {
    params.controller = 'groups';
});

router.run('users/12345?format=json');
// params:
// {
//     format: 'json',
//     limit: 25,
//     controller: 'users',
//     userID: 12345
// }

Handler functions are run after automatic parameter assignment, so properties set in a handler will always override parameters from the request:

var params = {};
var router = new PathParser(params);
router.add('users/:userID', function () {
    params.controller = 'users';
});
router.add('groups/:groupID', function () {
    params.controller = 'groups';
});

router.run('users/12345?controller=groups');
// params:
// {
//     controller: 'users',
//     userID: 12345
// }

In some cases, handler functions may not even be required when automatic parameter assignment is used:

var params = {};
var router = new PathParser(params);
router.add('items/:itemID');
router.add('collections/:collectionID/items/:itemID');

router.run('collections/1/items/2?limit=10');
// params:
// {
//     collectionID: 1
//     itemID: 2,
//     limit: 10
// }

Installation

Node

npm install pathparser
var PathParser = require('pathparser');
var router = new PathParser;

Browser

<script src="pathparser.min.js"></script>
<script>
var router = new PathParser;
</script>

Mozilla JSM

Components.utils.import("resource://myext/pathparser.js", MyExtension);
var router = new MyExtension.PathParser;