Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api: URL Pattern API implementation #92

Closed
wants to merge 1 commit into from
Closed

Api: URL Pattern API implementation #92

wants to merge 1 commit into from

Conversation

erivas-ligo
Copy link
Contributor

@erivas-ligo erivas-ligo commented Nov 1, 2023

Description

Related issue: 🚧 API: URLPattern #100

Dependencies: urlpattern

This PR uses urlpattern crate (which was created by Deno developers) to implement URLPattern.

The underlying crate, and thus URLPattern as defined here (and also Deno) has difference with the URL Pattern specified: it does not support the ignoreCase option. There is an open PR in urlpattern to support it, but it is already quite old, and not sure if it will be supported.

A single NativeClass is introduced (for URLPattern). For the other components (URLPatternInit, URLPatternInput, URLPatternComponentResult, URLPatternResult) this PR does not introduce native classes, and instead uses needed TryFromJs and IntoJs implementations.

Manual testing

nix develop
cargo run --bin jstz -- repl
>> (function () {
  const pattern = new URLPattern("https://deno.land/foo/:bar");
  console.log(pattern.protocol == "https");
  console.log(pattern.protocol == "https");
  console.log(pattern.hostname == "deno.land");
  console.log(pattern.pathname == "/foo/:bar");
  console.log(pattern.test("https://deno.land/foo/x"));
  console.log(!pattern.test("https://deno.com/foo/x"));
  match = pattern.exec("https://deno.land/foo/x");
  console.log(match);
  console.log(match.pathname.input == "/foo/x");
  // false, but also false in Deno/Chrome
  console.log(match.pathname.groups == { bar: "x" });
})();
[🪵] true
[🪵] true
[🪵] true
[🪵] true
[🪵] true
[🪵] true
[🪵] [object Object]
[🪵] true
[🪵] false
>> (function () {
  const pattern = new URLPattern("/foo/:bar", "https://deno.land");
  console.log(pattern.protocol == "https");
  console.log(pattern.hostname == "deno.land");
  console.log(pattern.pathname == "/foo/:bar");
  console.log(pattern.test("https://deno.land/foo/x"));
  console.log(!pattern.test("https://deno.com/foo/x"));
  const match = pattern.exec("https://deno.land/foo/x");
  console.log(match);
  console.log(match.pathname.input == "/foo/x");
  // false, but also false in Deno/Chrome
  console.log(match.pathname.groups == { bar: "x" });
})();
[🪵] true
[🪵] true
[🪵] true
[🪵] true
[🪵] true
[🪵] [object Object]
[🪵] true
[🪵] false
>> (function () {
  const pattern = new URLPattern({
    pathname: "/foo/:bar",
  });
  console.log(pattern.protocol == "*");
  console.log(pattern.hostname == "*");
  console.log(pattern.pathname == "/foo/:bar");
  console.log(pattern.test("https://deno.land/foo/x"));
  console.log(pattern.test("https://deno.com/foo/x"));
  console.log(!pattern.test("https://deno.com/bar/x"));
  console.log(pattern.test({ pathname: "/foo/x" }));
})();

[🪵] true
[🪵] true
[🪵] true
[🪵] true
[🪵] true
[🪵] true
[🪵] true
>>

Testing

Checklist

  • Changes follow the existing code style (use make fmt-check to check)
  • Tests for changes have been added
  • Internal documentation has been added (if appropriate)
  • Testing instructions have been added to PR

@erivas-ligo erivas-ligo closed this Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant