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

lite-url

v1.0.5

Published

A small cross-browser JS lib for parsing a URL into its component parts (generally follows Chrome's `new URL(parseThisUrl)` API).

Downloads

206

Readme

NPM

dependencies

Sauce Test Status

lite-url

A small cross-browser JS lib for parsing a URL into its component parts.

Broadly provides the same interface as the native URL function, but in a cross browser way (taken from Chrome 35):

new URL('http://user:[email protected]:8080/directory/file.ext?query=1#anchor'); //results in...

{
  "hash": "#anchor",
  "search": "?query=1",
  "pathname": "/directory/file.ext",
  "port": "8080",
  "hostname": "example.com",
  "host": "example.com:8080",
  "password": "pass",
  "username": "user",
  "protocol": "http:",
  "origin": "http://example.com:8080",
  "href": "http://user:[email protected]:8080/directory/file.ext?query=1#anchor"
}

install

manual

grab the minified version from dist/

bower

bower install --save lite-url

npm

npm install --save lite-url

(Since node.js already has built-in parsing functionality, this is only really useful if you are using browserify and want to keep the size down).

2 variations from chrome

1. more permissive

The following will parse in this lib but not in chrome (this is intentional):

  1. new URL('//user:[email protected]:8080/directory/file.ext?query=1#anchor'); //results in empty protocol
  2. new URL('?foo=bar&bingobang=&[email protected]#foobar/bing/bo@ng?b#ang'); //populates only href, hash, search, params

Both of the above would throw an error in chrome's native URL() parser.

2. query parser

Technically, there shouldn't be a parsed version of the query in the result (since the Chrome URL parser doesn't do this).

If you don't like the behaviour you can change it by calling changeQueryParser with a function. That function will be given the deconstructed url and expects the query params back.

E.g. If you want duplicate keys to be turned into an array, you could do this:

liteURL.changeQueryParser(function (uri) {
    var params = {};

    //strip the question mark from search
    var query = uri.search ? uri.search.substring(uri.search.indexOf('?') + 1) : '';
    query.replace(/(?:^|&)([^&=]*)=?([^&]*)/g, function ($0, $1, $2) {
        //query isn't actually modified, .replace() is used as an iterator to populate params
        if ($1) {
            if (params[$1]) {
                if (params[$1] instanceof Array) {
                    params[$1].push($2);
                } else {
                    params[$1] = [params[$1], $2];
                }
            } else {
                params[$1] = $2;
            }
        }
    });
    return params;
});

(The default behaviour will only ever return a string for a key, and it will be the last string it finds for that key.)

usage

<script src="lite-url.min.js"></script>
<script>
    var url = 'http://user:[email protected]:8080/directory/file.ext?query=1#anchor';
    var parsed = new liteURL(url);
    console.log(parsed);
</script>

notes

The URL object in Chrome etc doesn't quite fit with other interpretations of the spec (http://en.wikipedia.org/wiki/URI_scheme#Examples).

alternatives

  • https://github.com/medialize/URI.js
    • good if size isn't an issue
  • http://stevenlevithan.com/demo/parseuri/js/
    • good test examples but has a bug or two ('@' symbol in path or query breaks it)
  • var x = document.createElement('a'); x.href = '/relative/url'; console.log(x.hostname)
    • requires a document and creating an element (just not tidy)
    • isn't compatible with lt IE10
    • can't get authentication info from URL
  • https://developer.mozilla.org/en-US/docs/Web/API/URL
    • not cross browser

developing

testing

npm install -g gulp
npm install && bower install
gulp

crossbrowser testing with saucelabs

using config file:

cp saucelabs.example.json saucelabs.json

add your saucelabs username/secret key, and run:

npm test

you can do the same on cmdline with:

export SAUCE_USERNAME='your username' && export SAUCE_ACCESS_KEY='your key' && npm test

building

for npm (assuming setup correctly with npm)

npm publish

for bower, just tag correct semver and push to github.