dark
dark copied to clipboard
Add support for URL parsing to standard library functions (or add String::indexOf)
Hey! First off, thanks for building Darklang! It's a really exciting project, and I'm looking forward to seeing where things go 👍
Problem
Given a URL as a String, I'd like to parse the URL into its component parts.
For example, I'd like to be able to turn https://example.com/foo/bar?arg1=some_val&arg2=other_val#somehash
into:
-
domain
: example.com -
path
: /foo/bar -
url_parameters
:{"arg1": "some_val", "arg2": "other_val"}
-
hash
: #somehash
Many languages (e.g. JavaScript) have standard library functions that make this easy.
What I've Tried
I reviewed the full Darklang API docs, but did not find any functions under a URL::
namespace.
Then, I decided to try to hack something together using language primitives, e.g. direct string manipulation. Unfortunately there is no RegularExpression
or Regex
namespace, so I reviewed String
and List
.
Interestingly, because String::contains(lookingIn, searchingFor
) returns a Boolean
and not an Int
corresponding to the first location of searchingFor
, and there is no String::indexOf(stringToSearch, characterOrString)
, it does not appear possible to manually extract what I want from a String URL. (Unfortunately String::first
and String::last
do not appear to help here either)
I then thought perhaps I can convert the String to a List with String::split url ""
, however, List::findFirst
returns the first match, not the index of the match.
HTTP handlers can already parse URLs, so I thought maybe I could create an HTTP handler that I pass in the target URL, and in the route I could destructure the URL and return that to the caller. However, the URL would be a parameter not the URL matched by the route, so I don't think that can work.
Potential Solutions
Add URL::
to Standard Library
This may be more work, but I think it's overall the best UX: writing custom code to parse URLs is annoying, error prone, and full of edge cases.
HTTP handlers already do URL parsing, so hopefully there's already internal code that does this, and you could just expose the URL parsing code as standard library functions.
As Darklang aims to give users a first class web development experience, it seems likely that other users will build server-side functionality that requires parsing URLs, so this seems (to me) reasonable to include in the standard library at some point.
Add String.indexOf
(and maybe List.indexOf
)
Rather than adding a handful of URL helper functions, you could add these and let users code URL parsing logic themselves.
Further, at least in my opinion, these API functions seem like core language functionality that users will need at some point regardless, so probably worth adding when time permits.