From b74c14ed6f79037a329084220f037b8dd4f308bf Mon Sep 17 00:00:00 2001 From: Mateusz Russak Date: Sun, 7 Jul 2024 15:34:16 +0200 Subject: [PATCH] feat: add shim for URLPattern --- lib/shims.test.ts | 9 ++++++--- lib/shims.ts | 13 +++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/shims.test.ts b/lib/shims.test.ts index e89fa61..c105bd9 100644 --- a/lib/shims.test.ts +++ b/lib/shims.test.ts @@ -13,6 +13,7 @@ Deno.test("should get when all true", () => { crypto: true, domException: true, undici: true, + urlPattern: true, weakRef: true, webSocket: true, custom: [{ @@ -31,8 +32,8 @@ Deno.test("should get when all true", () => { }], }); - assertEquals(result.shims.length, 10); - assertEquals(result.testShims.length, 11); + assertEquals(result.shims.length, 11); + assertEquals(result.testShims.length, 12); }); Deno.test("should get when all dev", () => { @@ -44,12 +45,13 @@ Deno.test("should get when all dev", () => { crypto: "dev", domException: "dev", undici: "dev", + urlPattern: "dev", weakRef: "dev", webSocket: "dev", }); assertEquals(result.shims.length, 0); - assertEquals(result.testShims.length, 9); + assertEquals(result.testShims.length, 10); }); Deno.test("should get when all false", () => { @@ -61,6 +63,7 @@ Deno.test("should get when all false", () => { crypto: false, domException: false, undici: false, + urlPattern: false, weakRef: false, webSocket: false, }); diff --git a/lib/shims.ts b/lib/shims.ts index 3cd19ba..639b80b 100644 --- a/lib/shims.ts +++ b/lib/shims.ts @@ -37,6 +37,8 @@ export interface ShimOptions { * using the "undici" package (https://www.npmjs.com/package/undici). */ undici?: ShimValue; + /** Shim `URLPattern` using the "urlpattern-polyfill" package (https://www.npmjs.com/package/urlpattern-polyfill) */ + urlPattern?: ShimValue; /** Use a sham for the `WeakRef` global, which uses `globalThis.WeakRef` when * it exists. The sham will throw at runtime when calling `deref()` and `WeakRef` * doesn't globally exist, so this is only intended to help type check code that @@ -73,6 +75,7 @@ export function shimOptionsToTransformShims(options: ShimOptions) { add(options.timers, getTimersShim); add(options.domException, getDomExceptionShim); add(options.undici, getUndiciShim); + add(options.urlPattern, getURLPatternShim); add(options.weakRef, getWeakRefShim); add(options.webSocket, getWebSocketShim); @@ -218,6 +221,16 @@ function getUndiciShim(): Shim { }; } +function getURLPatternShim(): Shim { + return { + package: { + name: "urlpattern-polyfill", + version: "~10.0.0", + }, + globalNames: ["URLPattern"], + }; +} + function getDomExceptionShim(): Shim { return { package: {