Url.js
Tiny cross-platform URL utility
Why?
Another npm package https://www.npmjs.com/package/url is fine, but just too big(~47kb including dependencies). So I wrote this, the source code is about 3kb.
Installing
npm install x-url --save
Including
This is a UMD module, so you can include it in many ways.
Webpack:
Via <script>
tag:
API
url.parse(urlString)
Parses a URL string and returns a URL object. Query string is parsed into an object of key-value pairs.
If the string begins with //
, the token after //
and before next /
will be interpreted as the host
.
It's basically the same as url.parse(urlString, true, true) of the built-in node.js url module.
The parse function is based on parseUri by Steven Levithan.
url/* ->{ "href": "https://user:pass@www.example.com:444/foo/bar.html?a=1&a=2&c#d", "protocol": "https:", "auth": "user:pass", "username": "user", "password": "pass", "host": "www.example.com:444", "hostname": "www.example.com", "port": "444", "path": "/foo/bar.html?a=1&a=2&c", "pathname": "/foo/bar.html", "search": "?a=1&a=2&c", "hash": "#d", "query": { "a": [ "1", "2" ], "c": "" }}*/ url/* ->{ "href": "//www.example.com/foo/bar.html", "protocol": "", "auth": "", "username": "", "password": "", "host": "www.example.com", "hostname": "www.example.com", "port": "", "path": "/foo/bar.html", "pathname": "/foo/bar.html", "search": "", "hash": "", "query": {}}*/ url/* ->{ "href": "/foo/bar.html", "protocol": "", "auth": "", "username": "", "password": "", "host": "", "hostname": "", "port": "", "path": "/foo/bar.html", "pathname": "/foo/bar.html", "search": "", "hash": "", "query": {}}*/ url/* ->{ "href": "https://www.example.com", "protocol": "https:", "auth": "", "username": "", "password": "", "host": "www.example.com", "hostname": "www.example.com", "port": "", "path": "/", "pathname": "/", "search": "", "hash": "", "query": {}}*/
url.format(urlObject)
Takes a urlObject(as returned by url.parse()
), returns a formatted URL string. Basically the same as url.format(urlObject) of the built-in node.js url module.
url// -> "https://user:pass@www.example.com:444/foo/bar.html?a=1&a=2&c#d" url// -> "//www.example.com/foo/bar.html" url// -> "/foo/bar.html" url// -> "https://www.example.com/"
url.resolve(from, to)
Resolves a target URL relative to a base URL in a manner similar to that of a Web browser resolving an anchor tag HREF. Basically the same as of the built-in node.js url module.
url// -> "/one/two/four" url// -> "http://example.com/two" url// -> "https://www.google.com/bar"
url.parseQuery(queryString)
Parses a URL query string into a collection of key and value pairs. Basically the same as querystring.parse(str) of the built-in node.js querystring module.
url/* ->{ "foo": "bar", "abc": [ "xyz", "123" ]}*/
url.formatQuery(queryObject)
Takes a queryObject(as returned by url.parseQuery(queryString)), returns a query string. Basically the same as querystring.stringify(obj) of the built-in node.js querystring module.
url// -> "foo=bar&baz=qux&baz=quux&corge" url// -> "w=%E4%B8%AD%E6%96%87&foo=bar"