All Projects → treeform → urlly

treeform / urlly

Licence: MIT license
URL and URI parsing for Nim for C/JS backends. Similar api to browsers's window.location.

Programming Languages

nim
578 projects

Urlly (Pronounced "you-really?")

nimble install urlly

Github Actions

API reference

This library has no dependencies other than the Nim standard library.

About

URL and URI parsing for Nim for C/JS backends. Similar API to the browsers's window.location.

This module works everywhere and parses everything! Including providing an easy way to work with the query key-value pairs.

  foo://admin:[email protected]:8042/over/there?name=ferret#nose
  \_/   \___/ \_____/ \_________/ \__/\_________/ \_________/ \__/
   |      |       |       |        |       |          |         |
scheme username password hostname port   path       query fragment

This library is being actively developed and we'd be happy for you to use it.

Using the parseUrl().

let test = "foo://admin:[email protected]:8042/over/there?name=ferret#nose"
let url = parseUrl(test)
url.scheme == "foo"
url.username == "admin"
url.password == "hunter1"
url.hostname == "example.com"
url.port == "8042"
url.authority == "admin:[email protected]:8042"
url.paths == @["over", "there"]
url.path == "/over/there"
url.search == "name=ferret"
url.query["name"] == "ferret"
url.fragment == "nose"
$url == test

Using the $().

You can always turn a Url into a string with the $ function.

var url = Url()
url.hostname = "example.com"
url.query["q"] = "foo"
url.fragment = "heading1"
assert $url == "example.com?q=foo#heading1"

Using the query parameter.

The Url.query is just a sequence of key value pairs (seq[(string, string)]). This preserves their order and allows 100% exact reconstruction of the url string. This also allows you to walk through each pair looking for things you need:

let url = parseUrl("?name=ferret&age=12&leg=1&leg=2&leg=3&leg=4")

for (k, v) in url.query:
  if k == "leg":
    echo v

But for most use cases a special [] is provided. This is the most common use of the query.

url.query["name"] == "ferret"

If the key repeats multiple times only the first one is returned when using the [] method. Use the for loop method if you need to support multiple keys or preserve special ordering.

url.query["leg"] == "1"

Missing keys are just empty strings, no need to check if its there or handle exceptions:

url.query["missing"] == ""

You can also modify the query string with []= method:

url.query["missing"] = "no more!"
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].