urllib

WHATWG-compliant URL and URLPattern library for MoonBit

url
whatwg
urlpattern
moon add connect0459/urllib@0.4.3
Download zip
Version
0.4.3
License
Apache-2.0
Last updated
4 days ago
Downloads
31

Dependencies

README

#urllib-mbt

CI License: Apache-2.0 docs

A WHATWG-compliant URL parsing library for MoonBit.

Implements the URL Standard and URL Pattern Standard with full WPT (Web Platform Tests) coverage.

#Packages

PackageDescription
connect0459/urllib/urlURL parsing, getters, setters, URLSearchParams, IDNA utilities
connect0459/urllib/urlpatternURLPattern — route matching against URL components

#Installation

moon add connect0459/urllib

Then declare the packages you need in your moon.pkg:

///|
import {
"connect0459/urllib/url",
"connect0459/urllib/urlpattern",
}

#Usage

#URL Parsing

///|
test {
let url = @url.parse("https://user:pass@example.com:8080/path?q=1#frag")
assert_eq(url.href(), "https://user:pass@example.com:8080/path?q=1#frag")
assert_eq(url.hostname(), "example.com")
@debug.assert_eq(url.port(), Some(8080))
assert_eq(url.pathname(), "/path")
assert_eq(url.search(), "?q=1")
assert_eq(url.origin(), "https://example.com:8080")

// Infallible variant — returns None instead of raising
let opt = @url.parse_maybe("not a url", None)
@debug.assert_eq(opt, None)

// Resolve a relative URL
let base = @url.parse("https://example.com/a/b/c")
let resolved = base.join("../d")
assert_eq(resolved.href(), "https://example.com/a/d")
}

#URLSearchParams

///|
test {
let params = @url.UrlSearchParams::from_string("a=1&b=2&a=3")
@debug.assert_eq(params.get("a"), Some("1"))
@debug.assert_eq(params.get_all("a"), ["1", "3"])

params.append("c", "4")
params.delete("b")
params.set("a", "99")
assert_eq(params.to_string(), "a=99&c=4")
}

#URLPattern

///|
test {
let p = @urlpattern.UrlPattern::from_string(
"https://example.com/books/:id",
None,
)
assert_eq(p.test_url("https://example.com/books/42"), true)

match p.exec_url("https://example.com/books/42") {
Some(result) =>
match result.pathname() {
Some(pr) =>
match pr.groups().get("id") {
Some(Some(id)) => assert_eq(id, "42")
_ => assert_true(false)
}
None => assert_true(false)
}
None => assert_true(false)
}
}

#IDNA

///|
test {
assert_eq(@url.domain_to_ascii("münchen.example"), "xn--mnchen-3ya.example")
assert_eq(
@url.domain_to_unicode("xn--mnchen-3ya.example"),
"münchen.example",
)
}

#Compliance

Full Web Platform Tests (WPT) coverage for the URL and URLPattern standards.

#Documentation

Each public package has a README.mbt.md with a key-types overview, usage examples, and a full API reference. Start with url for the main entry point.

#Contributing

#License

Apache-2.0