@warren-bank/url

3.2.1 • Public • Published

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:password@www.example.org:80/demo/example.cgi?lang=de&foo=bar#position')
          }
        </script>
      </head>
      </html>

Limitations

The following methods are not implemented:


Requirements:

Legal

Package Sidebar

Install

npm i @warren-bank/url

Weekly Downloads

17

Version

3.2.1

License

GPL-2.0

Unpacked Size

68.1 kB

Total Files

7

Last publish

Collaborators

  • warren-r-bank