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

urldict

v1.0.0

Published

URL parameters parser for Node.js and browsers.

Downloads

2

Readme

UrlDict

Micro lib (less than 2K!) to handle URL params like dictionaries.

Support GET params and Hash (if you want to use Fragment identifier to store a dictionary of values). Reliably build back URLs.

Install

You can install via npm:

npm install urldict

Or bower:

bower install urldict

Or simply download the js files from /dist/ in this repo.

Usage

Lets start with a simple example that covers most of it:

// create a new urlDict object
var urlDict = new UrlDict("www.example.com");

// set url GET param
urlDict.GET.set("a", 123);
urlDict.GET.set("b", 456);

// build url. output will be "www.example.com?a=123&b=456"
alert(urlDict.toUrl());

// now set hash param
urlDict.HASH.set("foo", "bar");

// and now url output will be "www.example.com?a=123&b=456#foo=bar"
alert(urlDict.toUrl());

Or if you want to get params from existing url:

// build new url dict from url with GET and HASH params
var urlDict = new UrlDict("www.example.com?a=123&b=456#foo=bar");

// will output "123":
alert(urlDict.GET.get("a");

// will output "bar"
alert(urlDict.HASH.get("foo");

Note that UrlDict try to parse params using the JSON parser, which means that values like a=true will be returned as a boolean, and b=123 as a number. However, there's a special exception that if the value is not a valid JSON string it will just treat it as string.

Full API

To create a new UrlDict object:

// if 'url' is not provided, will use location.href instead.
var urlDict = new UrlDict(url);

To build the full URL from UrlDict, use toUrl():

var url = urlDict.toUrl();

To get or set just the base URL, without the GET or Hash params, use getBaseUrl() and setBaseUrl():

// set the base url, eg the part before the GET and hash params:
urlDict.setBaseUrl(newBaseUrl);

// get the base url, eg the part before the GET and hash params:
var baseUrl = urlDict.getBaseUrl();

To get a dictionary containing both GET and HASH params (objects are mutable and changing them will affect the urlDict), use asDict():

// get params as dictionary
var params = urlDict.asDict();

// show GET params
console.log(params.GET);

// show HASH params
console.log(params.HASH);

// to add a new GET param to url
params.GET["a"] = 123;

GET and HASH

The main API to set / get URL params is via the GET and HASH objects. They share the same API:

// set url GET param
urlDict.GET.set("a", 123);

// get url GET param
var a = urlDict.GET.get("a");

// remove url GET param
urlDict.GET.remove("a");

// clear all url GET params
urlDict.GET.clear();

The API above is the same for HASH object.

Or you can just get all params as a mutable dictionary (changing this dictionary will change the urlDict object):

// get all url GET params as a dictionary:
var getParams = urlDict.GET.asDict();

// change a url GET param
getParams["a"] = 123;

Tests & Browser support

urldict is fully testes on Node.js, IE7, Edge, Chrome, FireFox and Opera, and have 100% code coverage.

BrowsersSupport

Test in browsers

To test UrlDict in browsers just open the file /tests/test.html with the browser of your choice.

Test in Node.js

To test in nodejs first install qunit-cli:

npm install qunit-cli

Then you can run the following command from the project root:

qunit-cli tests/test.js

License

UrlDict is distributed under the MIT license and is free to use for any personal or commercial purpose.