@warren-bank/url
v3.2.1
Published
URL class library that does not perform any encoding or decoding
Downloads
139
Maintainers
Readme
jsURL
Javascript drop-in replacement URL class library for Node.js and the browser.
The important difference is that this library does not perform any encoding or decoding. Further reading: 1
Class Import
in Node.js:
npm install --save @warren-bank/url
const {URL, parse} = require('@warren-bank/url') { const gh = new URL('https://github.com/warren-bank') const qs = gh.searchParams gh.username = 'user' gh.password = 'pass' gh.port = 443 gh.hash = '#language-options' qs.set('tab', 'repositories') console.log('URL.href = ' + gh.href) console.log('parse(URL.href) = ' + JSON.stringify(parse(gh.href), null, 2)) console.log('parse(URL) = ' + JSON.stringify(parse(gh), null, 2)) console.log('parse(URL, true) = ' + JSON.stringify(parse(gh, true), null, 2)) }
in the browser:
<html> <head> <script src="https://cdn.jsdelivr.net/gh/warren-bank/js-url/es5-browser/jsURL.js"></script> <script> { // temporarily override global URL class with external implementation within scope of block const {URL, parse} = window.jsURL const gh = new URL('https://github.com/warren-bank') const qs = gh.searchParams gh.username = 'user' gh.password = 'pass' gh.port = 443 gh.hash = '#language-options' qs.set('tab', 'repositories') window.alert('URL.href = ' + gh.href) window.alert('parse(URL.href) = ' + JSON.stringify(parse(gh.href), null, 2)) window.alert('parse(URL) = ' + JSON.stringify(parse(gh), null, 2)) window.alert('parse(URL, true) = ' + JSON.stringify(parse(gh, true), null, 2)) } { const {parse, format} = window.jsURL const run_test_1 = () => { const url_1 = format({ protocol: 'https:', hostname: 'example.com', port: 80, pathname: '/some/path', search: 'page=1&format=json' }) const url_2 = format({ protocol: 'https', host: 'example.com:80', pathname: 'some/path', query: { page: 1, format: 'json' } }) const is_equal = (url_1 === url_2) window.alert( JSON.stringify({url_1, url_2, is_equal}, null, 2) ) } const run_test_2 = (url_1) => { const urlObject = parse(url_1) const url_2 = format(urlObject) const is_equal = (url_1 === url_2) window.alert( JSON.stringify({url_1, url_2, is_equal}, null, 2) ) } run_test_1() run_test_2('http://login:[email protected]:80/demo/example.cgi?lang=de&foo=bar#position') } </script> </head> </html>
Limitations
The following methods are not implemented:
Requirements:
- release version:
v2.0.0
(and lower)- Node.js version: v6.4.0 (and higher)
- ES6 support
- v0.10.48+: const
- v4.09.01+: arrow functions
- v5.12.00+: spread syntax for iterable objects
- v6.04.00+: let
- v6.04.00+: class statement with static methods
- ES6 support
- Node.js version: v6.4.0 (and higher)
- release version:
v2.0.1
(and higher)- Node.js version: v12.0.0 (and higher)
- ES6 support
- v12.00.00+: private instance class fields
- ES6 support
- Node.js version: v12.0.0 (and higher)
Legal
- copyright: Warren Bank
- license: GPL-2.0