Simple URL implementation of RFC #3986 for both NodeJS & browsers.
npm install unified-resource-locator
bower install unified-resource-locator
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"
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"});
protocol
user
password
host
port
path
query
fragment
Guess what!..
Yes, you can!
Have a look at the test suite! ;)
npm test
for running tests;test/coverage.sh
for measuring code coverage.