ureal

urls are serialised data structures; this library lets you treat them that way

Usage no npm install needed!

<script type="module">
  import ureal from 'https://cdn.skypack.dev/ureal';
</script>

README

Urls are, at their heart, serialised data-structures. They're really awkward to work with, and everyone in the web-world has seen god-awful regex hacks in one project or another. We don't use string manipulation on JSON, though; so why subject ourselves to it for urls?

API

ureal.parse(url) -> object

Like JSON.parse, ureal.parse takes a string (a url), and turns it into a userful datastructure. For instance:

        ureal.parse('https://user:password@secure.my-site.com/profile?edit=email')

Returns:

{ protocol: 'https',
  user: 'user',
  password: 'password',
  domain: 'secure.my-site.com',
  path: [ 'profile' ],
  params: { edit: 'email' } }        

ureal.stringify(object) -> url

ureal.stringify takes an object in the same format as ureal.parse emits, and constructs a url out of it:

        
        ureal.stringify({ protocol: 'https',
                          user: 'user',     
                          password: 'password', 
                          domain: 'secure.my-site.com',
                          path: [ 'profile' ],         
                          params: { edit: 'email' } })
                                                                                                                                                   

Returns:

        'https://user:password@secure.my-site.com/profile?edit=email'
```g