X-Git-Url: https://git.ayoreis.com/relax.git/blobdiff_plain/d7baaea4f90491d77b416fbf53935e090bbf7f7f..83524f5609ef938630538f11371a2d0a4b88035f:/relax/url-pattern-list.ts?ds=inline diff --git a/relax/url-pattern-list.ts b/relax/url-pattern-list.ts index e69de29..802d02a 100644 --- a/relax/url-pattern-list.ts +++ b/relax/url-pattern-list.ts @@ -0,0 +1,44 @@ +import compare_component from "./url-pattern-compare-component.ts"; + +export interface URLPatternListResult extends URLPatternResult { + pattern: URLPattern; +} + +export const COMPONENTS = [ + "hash", + "search", + "pathname", + "port", + "hostname", + "password", + "username", + "protocol", +] as const; + +/** https://github.com/whatwg/urlpattern/issues/30 */ +export default class { + readonly #patterns; + + constructor(patterns: Iterable) { + this.#patterns = Array.from(patterns).sort((left, right) => { + for (const component of COMPONENTS) { + const order = compare_component(component, left, right); + if (order !== 0) return order; + } + + return 0; + }); + } + + test(input: URLPatternInput, baseURL?: string) { + return !this.exec(input, baseURL).next().done; + } + + *exec(input: URLPatternInput, baseURL?: string) { + for (const pattern of this.#patterns) { + const result = pattern.exec(input, baseURL); + if (result === null) continue; + yield { pattern, ...result } satisfies URLPatternListResult; + } + } +}