WHATWG URL Standard parser implementation in MoonBit
Dependencies
moon add tonyfettes/url// Parse a URL
let url = @url.Url::parse("https://user:pass@example.com:8080/path?query=value#fragment")
match url {
Some(url) => {
println(url.protocol()) // "https:"
println(url.hostname()) // "example.com"
println(url.pathname()) // "/path"
println(url.search()) // "?query=value"
println(url.hash()) // "#fragment"
println(url.to_string()) // full URL
}
None => println("Invalid URL")
}
// Parse relative URLs with a base
let base = @url.Url::parse("https://example.com/a/b/c").unwrap()
let relative = @url.Url::parse("../d", base~)
// Result: "https://example.com/a/d"
// Modify URL components
let url = @url.Url::parse("http://example.com/path").unwrap()
url.set_protocol("https:")
url.set_port("8080")
url.set_pathname("/new/path")
url.set_search("?foo=bar")
url.set_hash("#section")| Method | Description |
|---|---|
| href() | Full serialized URL |
| protocol() | Scheme with trailing colon (e.g., "https:") |
| get_username() | Username component |
| get_password() | Password component |
| get_host() | Host with port (e.g., "example.com:8080") |
| hostname() | Host without port |
| get_port() | Port as string (empty if default/none) |
| pathname() | Path component |
| search() | Query string with leading ? |
| hash() | Fragment with leading # |
| origin() | Origin (scheme + host + port) |
| Method | Description |
|---|---|
| set_protocol(protocol: String) | Set scheme |
| set_username(username: String) | Set username |
| set_password(password: String) | Set password |
| set_host(host: String) | Set host (with optional port) |
| set_hostname(hostname: String) | Set hostname only |
| set_port(port: String) | Set port |
| set_pathname(pathname: String) | Set path |
| set_search(search: String) | Set query string |
| set_hash(hash: String) | Set fragment |
moon check # Type check and lint
moon build # Build the project
moon test # Run all testsimpl Show for ValidationErrortype Path#as_free_fn
fn Url::try_parse(input : StringView, base? : Url, validation_errors? : Array[ValidationError]) -> Url?impl Show for UrlSearchParamsimpl ToJson for UrlSearchParamsWHATWG URL Standard parser implementation in MoonBit
Dependencies