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

simple-url

v1.1.8

Published

simple utilities of url

Downloads

163

Readme

simple-url

simple-url is a lib of utilities for url which can be used in both browser and node.js.

Installing

Use via npm:

npm install simple-url
var url = require('simple-url');

// Use es6 import
import url from 'simple-url';

Use in browser:

Scripts for browser is under build directory, use url.js for development (contains inline source maps) or use url.min.js for production. The reference in browser is window.simpleUrl.

The structure of a url

The following illustration comes from nodejs docs.

┌─────────────────────────────────────────────────────────────────────────────┐
│                                    href                                     │
├──────────┬┬───────────┬─────────────────┬───────────────────────────┬───────┤
│ protocol ││   auth    │      host       │           path            │ hash  │
│          ││           ├──────────┬──────┼──────────┬────────────────┤       │
│          ││           │ hostname │ port │ pathname │     search     │       │
│          ││           │          │      │          ├─┬──────────────┤       │
│          ││           │          │      │          │ │    query     │       │
"  http:   // user:pass @ host.com : 8080   /p/a/t/h  ?  query=string   #hash "
│          ││           │          │      │          │ │              │       │
└──────────┴┴───────────┴──────────┴──────┴──────────┴─┴──────────────┴───────┘
(all spaces in the "" line should be ignored -- they are purely for formatting)

Examples

Parse a url:

var simpleUrl = require('simple-url');

var parsedUrl = simpleUrl.parse('http://foo.com/pathname/?foo=bar', true);
console.log(parsedUrl);
/**
* The output is:
* {
*   protocol: 'http',
*   auth: '',
*   host: 'foo.com',
*   pathname: '/pathname/',
*   query: {foo: 'bar'},
*   hash: ''
* }
*/

Trim origin:

var simpleUrl = require('simple-url');

console.log(simpleUrl.trimOrigin('http://foo.com/pathname/?foo=bar'));
// Output: /pathname/?foo=bar

console.log(simpleUrl.trimOrigin(/pathname/?foo=bar));
// Output: /pathanem/?foo=bar

Create a url:

var simpleUrl = require('simple-url');

var url = simpleUrl.create({
    protocol: 'https',
    host: 'github.com',
    query: {colors: ['red', 'green', 'blue']}
  });
console.log(url);
/**
* The output is:
* https://github.com/?colors%5B0%5D=red&colors%5B1%5D=green&colors%5B2%5D=blue
*/

Create a path:

var path1 = simpleUrl.createPath(
    '/foo/bar',
    {colors: ['red', 'green', 'blue']}
  );
console.log(path);
/*
* The output is:
* /foo/bar?colors%5B0%5D=red&colors%5B1%5D=green&colors%5B2%5D=blue
*/

var path2 = simpleUrl.createPath({
    pathname: '/foo/bar',
    query: {colors: ['red', 'green', 'blue']}
  });
console.log(path1 === path2)
/**
* The output is:
* true
*/

methods

url.parse(url, parseQuery)

| Params | Type | Description | | --- | --- | --- | | url | String | Url to parse. | | parseQuery | Boolean | QueryString will be parsed if it's true, default false |

This method parses the given url and returns null(invalid url) or object like:

{
  protocol: 'http', // '' if mismatch
  auth: 'user:pass', // '' if mismatch
  host: 'host.com:8080', // '' if mismatch
  pathname: '/p/a/t/h', // '' if mismatch
  query: {foo: 'bar'}, // {} if mismatch
  hash: 'hash' // '' if mismatch
}

Note: url for url.parse is not necessary to be a complete url, it can be //host.com : 8080/path?query=string#hash, /path?query=string, etc.

url.create(options)

This method creates a url with the given options.

| Options | Type | Default | | --- | --- | --- | | protocal | String | 'http' | | auth | String | '' | | host | String | 'localhost' | | pathname | String | '/' | | query | String or Obj | '' | | hash | String | '' |

None of these options is required, it will produce a "http://localhost" if you call url.create() only.

url.createPath(pathname, query, hash) or url.createPath(options)

This method simply crates a path and it's params have the same defaults with url.create's. It leaves param hash there for convenience though it is not a part of path.

url.trimOrigin(url)

This method will trim origin of the given url, and it will not decode any uri component or query.

url.qs

This is exactly a reference to qs, which is the only dependency of simple-url. It is exposed just for convenience.

License

MIT