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!):

5 Likes

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

3 Likes