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

ale-url-parser

v1.0.0

Published

Fast url parser

Downloads

42

Readme

ale-url-parser Build Status

🍺 Top fermented URL parser and stringifier built with performance and small size (1.6KB) in mind.

Installation

$ npm i ale-url-parser

Usage

parse :: String -> Object

Parse url string and return url object.

const { parse } = require('ale-url-parser');

Basic example

parse('http://domain.lol/lorem/ipsum?foo=1&bar=2#baz');

{
    protocol: 'http',
    host: 'domain.lol',
    path: ['lorem', 'ipsum'],
    query: { foo: '1', bar: '2' },
    hash: 'baz'
}

Preserve protocol

parse('//domain.lol');

{
    protocol: '',
    host: 'domain.lol',
    path: [], query: {}, hash: ''
}

Http by default

parse('domain.lol');

{
    protocol: 'http',
    host: 'domain.lol',
    path: [], query: {}, hash: ''
}

Multi-valued query parameters

parse('domain.lol?foo=1&foo=2&bar=3');

{
    protocol: 'http',
    host: 'domain.lol',
    path: [],
    query: { foo: ['1', '2'], bar: '3' },
    hash: ''
}

Parsing relative urls

parse('?foo=1');

{
    protocol: 'http',
    host: '',
    path: [],
    query: { foo: '1' },
    hash: ''
}

stringify :: Object -> String

Stringify url object to url string.

const { stringify } = require('ale-url-parser');

Basic example

stringify({
    protocol: 'https',
    host: 'domain.lol',
    path: ['lorem', 'ipsum'],
    query: { foo: '1', bar: '2' },
    hash: 'baz'
});

"https://domain.lol/lorem/ipsum?foo=1&bar=2#baz"

Preserve protocol

stringify({
    protocol: '',
    host: 'domain.lol'
});

"//domain.lol"

Multi-valued query parameters

stringify({
    protocol: 'https',
    host: 'domain.lol',
    query: { foo: ['1', '2'], bar: '/baz' }
});

"https://domain.lol?foo=1&foo=2&bar=%2Fbaz"

Build relative urls

stringify({
    path: ['lorem', 'ipsum'],
    query: { foo: '1', bar: '2' }
});

"/lorem/ipsum?foo=1&bar=2"

Sort query params with custom compareFunction

Sorting query params is disabled by default. You can define your own sorting method by passing compareFunction:

const order = ['first', 'second', 'third', 'fourth'];
stringify({
    host: 'domain.lol',
    query: { third: '3', first: '1', fourth: '4', second: '2' }
}, {
    compareFunction: (a, b) => order.indexOf(a) > order.indexOf(b)
});

"http://domain.lol?first=1&second=2&third=3&fourth=4"

Caveats

ale-url-parser is limited to be used with http and https protocols though context-aware protocol guess is supported by passing an empty string to stringify function, i.e. protocol: ''.

Benchmarks

$ npm t && npm run prepare && node ./tests/benchmark-parse.js

[simple] ale-url-parser x 124,203 ops/sec ±0.67% (91 runs sampled)
[simple] url x 75,006 ops/sec ±1.03% (89 runs sampled)
[simple] query-string x 47,283 ops/sec ±0.77% (86 runs sampled)
[simple] fast-url-parser x 237,420 ops/sec ±0.66% (91 runs sampled)
[simple] Fastest is fast-url-parser
[complex] ale-url-parser x 16,846 ops/sec ±0.58% (89 runs sampled)
[complex] url x 8,104 ops/sec ±0.71% (86 runs sampled)
[complex] query-string x 5,884 ops/sec ±0.80% (87 runs sampled)
[complex] fast-url-parser x 15,430 ops/sec ±0.92% (87 runs sampled)

https://jsperf.com/ale-url-parser-vs-new-url

TypeScript definitions

Type definitions for ale-url-parser are declared in DefinitelyTyped repository. We recommend installing @types/ale-url-parser for a better experience

$ npm i @types/ale-url-parser -D

License

MIT