Launched: URL pack for for composing, parsing, normalizing, and manipulating URLs

The URL pack enables better manipulation of URL strings using well-documented formulas powered by tried-and-true libraries, rather than manual formatting, which can be error-prone especially when dealing with complex URLs or user input.

My use case was to compare various user inputted URLs to check whether they were functionally the same, and I needed something slightly stronger than simply Trim()ming whitespace and Lower()-casing the URL strings. Manually removing the query params and adjusting the protocol with regexes felt clumsy, so I made this pack to bring existing well-established URL manipulation techniques into the Coda Formula Language. This can now easily be accomplished with this pack by normalizing both URLs with the same set of options, and then literally comparing the output to check for equality. Normalizing with the NormalizeURL() formula can also be useful to display, store, sort, compare, etc, URLs.

Arbitrarily complex URLs can be parsed into a representation of the various components (protocol, hostname, path, etc.) using the ParseURL() formula. Conversely, URLs can be composed from any number of URL components using the BuildURL() formula. The components of existing URLs can be modified (i.e. overwritten) by passing any number of components as arguments to the ModifyURL() formula, called with the URL to modify as its first argument.

Lastly, the search params (e.g. ‘?q=myQuery’ that is found in the URL of webpages that display the results of a search) can be read and manipulated using the ...URLSearchParam() set of formulas, where the formula name prefix, one of Append, Delete, Get, GetAll, Has, List, Set, and Sort, indicates its function. These formulas should be called on an already-parsed query string as their first argument, which can be extracted from a full URL by accessing the Search property on the result of the ParseURL() formula. For example, ModifyURL(myUrl, search: ParseURL(myUrl).search.AppendURLSearchParam(name: 'foo' , value: 'bar')) would append the search param name-value pair ‘foo=bar’ to the URL myUrl.

Any feedback, functionality requests to augment existing formulas or add new ones, and bug reports appreciated!

As usual, all code is made open source (feel free to open issues or contribute!):

6 Likes

Well done @loucadufault - glad to see you making more Packs!

4 Likes

Hi Louca, Thank you for this pack. I wondering if I could get some help using it. I want to have a column show the hostname for each url, but so far I’ve only been able to get the host name when I hover over a column with the urlparse formula in use. Is there a different formula that I should use or any optional parameter to just get the host name? Thank you, Adam

1 Like

Something like this?

Let me know if you are unable to view the formulas used for the columns.

Essentially once you call the ParseURL formula for the pack on a URL, the formula will return an object with a bunch of properties (including hostname) on it that can be accessed with dot notation. It would look like ParseURL("https://community.coda.io/t/launched-url-pack-for-for-composing-parsing-normalizing-and-manipulating-urls").Hostname. You can do this directly, or you can store the result of the ParseURL formula in a column, and then use dot notation on that column, like [My column with a formula using ParseURL].Hostname

1 Like

Yes! that does it! Thank you!

1 Like