Skip to content

pvoisin/URL.js

Repository files navigation

NPM Version Build Status Coverage Status

URL.js

Simple URL implementation of RFC #3986 for both NodeJS & browsers.

Installation

NPM

npm install unified-resource-locator

Bower

bower install unified-resource-locator

Usage

In Short...

var locator = new URL("http://localhost:1234/a/b/c/resource?X=1&Y&Z=3#page=1");

locator.protocol // "http"
locator.host // "localhost"
locator.port // 1234
locator.path // "/a/b/c/resource"
locator.query// {X: "1", Y: "", Z: "3"}
locator.fragment // "page=1"

String(new URL(locator, {query: {p: "San Francisco"}}))
// "https://search.yahoo.com/search?p=San%20Francisco"

String(new URL(locator, {path: "/search/images"}, true))
// "https://search.yahoo.com/search/images?p=New%20York"

String(new URL(locator, {query: null}, true))
// "https://search.yahoo.com/search"

API

Constructor (URL)

URL objects can be constructed from raw locators, like:

new URL("http://foo.bar-baz.com:80/p/a/t/h?q=u&e&r=y#fragment");
new URL("/p/a/t/h?q=u&e&r=y#fragment");
new URL("https://engine/search/?q=Earth");
new URL("http://fr.wikipedia.org/wiki/Sp%C3%A9cial:Page_au_hasard");
new URL("//en.wikipedia.org/wiki/URI_scheme#Generic_syntax");
new URL("redis://undefined:password@localhost:6379/0");
new URL("http://[email protected]/#/house");

Also, you can complement the locators with specific parts if missing or completely overwrite some of them:

new URL("https://engine/search", {query: "q=Earth"});
new URL("https://engine/search", {query: {q: "Solar System"}});
new URL("https://engine/search/?q=Earth", {query: {q: "Moon"}}, true);

Finally, full control of URL parts is possible by providing them to the constructor without any text locator.

new URL({protocol: "http", "host": "server"});
new URL({host: "server"});
new URL({path: "/from/root"});
new URL({query: "A=1"});

Pattern (URL.pattern)

Pattern based on RFC 3986.

Supported Parts (URL.parts)

  • protocol
  • user
  • password
  • host
  • port
  • path
  • query
  • fragment

URL#parseQueryString (string)

Guess what!..

URL#makeQueryString (object)

Yes, you can!

More?

Have a look at the test suite! ;)

Workflow

  • npm test for running tests;
  • test/coverage.sh for measuring code coverage.

About

Simple URL implementation for NodeJS & Bower.

Resources

License

Stars

Watchers

Forks

Packages

No packages published