From ee56483410a1b8bedb31940dd8683435aa03eab1 Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Mon, 29 Jan 2024 18:02:02 +0000 Subject: [PATCH 1/2] Remove a whole bunch of polyfills BREAKING CHANGE: This removes a lot of polyfills, and pushes our minimum base support up --- docs/index.html | 470 ++++++++++-------------------------- src/abortsignal-abort.ts | 27 --- src/abortsignal-timeout.ts | 8 +- src/array-findlast.ts | 26 -- src/array-findlastindex.ts | 31 --- src/arraylike-at.ts | 40 --- src/crypto-randomuuid.ts | 33 --- src/event-abortsignal.ts | 48 ---- src/form-requestsubmit.ts | 32 --- src/index.ts | 47 +--- src/object-hasown.ts | 33 --- test/abortsignal-abort.js | 24 -- test/array-findlast.js | 30 --- test/array-findlastindex.js | 30 --- test/arraylike-at.js | 17 -- test/crypto-randomuuid.js | 18 -- test/event-abortsignal.js | 25 -- test/form-requestsubmit.js | 73 ------ test/object-hasown.js | 16 -- 19 files changed, 147 insertions(+), 881 deletions(-) delete mode 100644 src/abortsignal-abort.ts delete mode 100644 src/array-findlast.ts delete mode 100644 src/array-findlastindex.ts delete mode 100644 src/arraylike-at.ts delete mode 100644 src/crypto-randomuuid.ts delete mode 100644 src/event-abortsignal.ts delete mode 100644 src/form-requestsubmit.ts delete mode 100644 src/object-hasown.ts delete mode 100644 test/abortsignal-abort.js delete mode 100644 test/array-findlast.js delete mode 100644 test/array-findlastindex.js delete mode 100644 test/arraylike-at.js delete mode 100644 test/crypto-randomuuid.js delete mode 100644 test/event-abortsignal.js delete mode 100644 test/form-requestsubmit.js delete mode 100644 test/object-hasown.js diff --git a/docs/index.html b/docs/index.html index 42a52f4..2a8edbc 100644 --- a/docs/index.html +++ b/docs/index.html @@ -193,104 +193,6 @@

GitHub Feature Support Table

Base Objects & Functions

- - - - Blob Constructor - - -
!
-
5+
-
12+
-
4+
-
6+
-
11+
-
1.0+
- - - - - PerformanceObserver Constructor - - -
!
-
52+
-
79+
-
57+
-
11+
-
39+
-
6.0+
- - - - - Intl Constructor - - -
!
-
24+
-
12+
-
29+
-
10+
-
15+
-
1.5+
- - - - - MutationObserver Constructor - - -
!
-
26+
-
12+
-
14+
-
7+
-
15+
-
1.5+
- - - - - URLSearchParams Constructor - - -
!
-
49+
-
17+
-
29+
-
10.1+
-
36+
-
5.0+
- - - - - WebSocket Constructor - - -
!
-
4+
-
12+
-
11+
-
5+
-
12.1+
-
1.0+
- - - - - IntersectionObserver Constructor - - -
!
-
51+
-
15+
-
55+
-
12.1+
-
38+
-
5.0+
- @@ -307,87 +209,17 @@

GitHub Feature Support Table

-
- TextEncoder Constructor - - -
!
-
38+
-
79+
-
18+
-
10.1+
-
25+
-
3.0+
- - - - - TextDecoder Constructor + + HTMLDialogElement Constructor -
!
-
38+
-
79+
-
19+
-
10.1+
-
25+
-
3.0+
- - - - - customElements - - -
!
-
54+
-
79+
-
63+
-
10.1+
-
41+
-
6.0+
- - - - - HTMLDetailsElement Constructor - - -
!
-
10+
+
!
+
37+
79+
-
49+
-
6+
-
15+
-
1.0+
- - - - - AbortController Constructor - - -
!
-
66+
-
16+
-
57+
-
12.1+
-
53+
-
9.0+
- - - - - AbortSignal Constructor - - -
!
-
66+
-
16+
-
57+
-
11.1+
-
53+
-
9.0+
+
98+
+
24+
+
15.4+
+
4.0+
@@ -403,34 +235,6 @@

GitHub Feature Support Table

58+
10.0+
- - - - FormData.entries - - -
!
-
50+
-
18+
-
44+
-
11.1+
-
37+
-
5.0+
- - - - - Element.toggleAttribute - - -
!
-
69+
-
18+
-
63+
-
12+
-
56+
-
10.0+
- GitHub Feature Support Table String.replaceAll -
*
+
!
85+
85+
77+
@@ -507,34 +311,6 @@

GitHub Feature Support Table

71+
14.0+
- - - - AggregateError - - -
*
-
85+
-
85+
-
79+
-
14+
-
71+
-
14.0+
- - - - - Element.replaceChildren - - -
*
-
86+
-
86+
-
78+
-
14+
-
72+
-
14.0+
- GitHub Feature Support Table Promise.allSettled -
*
+
!
76+
79+
71+
@@ -557,7 +333,7 @@

GitHub Feature Support Table

Promise.any -
*
+
!
85+
85+
79+
@@ -565,9 +341,51 @@

GitHub Feature Support Table

71+
14+
- - -

Polyfilled Features

+ + + + String.prototype.at + + +
!
+
92+
+
92+
+
90+
+
15.4+
+
65+
+
16.0+
+ + + + + Array.prototype.at + + +
!
+
92+
+
92+
+
90+
+
15.4+
+
65+
+
16.0+
+ + + + + Object.hasOwn + + +
!
+
93+
+
93+
+
92+
+
15.4+
+
79+
+
17.0+
@@ -575,7 +393,7 @@

GitHub Feature Support Table

AbortSignal.abort -
*
+
!
93+
93+
88+
@@ -585,32 +403,92 @@

GitHub Feature Support Table

- - AbortSignal.timeout + + AggregateError -
*
-
103+
-
103+
-
100+
-
16+
-
89+
-
16+
+
!
+
85+
+
85+
+
79+
+
14+
+
71+
+
14.0+
- - Array.at + + BroadcastChannel -
*
+
!
+
54+
+
79+
+
38+
+
15.4+
+
41+
+
6.0+
+ + + + + Crypto.randomUUID + + +
!
92+
92+
-
90+
+
95+
15.4+
78+
16.0+
+ + + + Element.replaceChildren + + +
!
+
86+
+
86+
+
78+
+
14+
+
72+
+
14.0+
+ + + + + HTMLFormElement.requestSubmit + + +
!
+
76+
+
79+
+
75+
+
16+
+
63+
+
12.0+
+ + + +

Polyfilled Features

+ + + + + AbortSignal.timeout + + +
*
+
103+
+
103+
+
100+
+
16+
+
89+
+
16+
+ @@ -625,20 +503,6 @@

GitHub Feature Support Table

53+ †
9.0+ †
- - -
- Crypto.randomUUID - - -
*
-
92+
-
92+
-
95+
-
15.4+
-
78+
-
16.0+
- @@ -653,22 +517,6 @@

GitHub Feature Support Table

91+
20.0+
- - -
- EventTarget.addEventListener signal - - -
*
-
90+
-
90+
-
86+
-
15+
-
76+
-
15.0+
- @@ -683,34 +531,6 @@

GitHub Feature Support Table

63+ †
12.0+ †
- - -
- HTMLFormElement.requestSubmit - - -
*
-
76+
-
79+
-
75+
-
16+
-
63+
-
12.0+
- - - - - Object.hasOwn - - -
*
-
93+
-
93+
-
92+
-
15.4+
-
79+
-
17.0+
- @@ -725,36 +545,6 @@

GitHub Feature Support Table

34+
5.0+
- - -
- Array.findLast - - -
*
-
97+
-
97+
-
104+
-
15.4+
-
83+
-
18.0+
- - - - - Array.findLastIndex - - -
*
-
97+
-
97+
-
104+
-
15.4+
-
83+
-
18.0+
-

Native Syntax

diff --git a/src/abortsignal-abort.ts b/src/abortsignal-abort.ts deleted file mode 100644 index fb8f8ab..0000000 --- a/src/abortsignal-abort.ts +++ /dev/null @@ -1,27 +0,0 @@ -export function abortSignalAbort(reason: unknown) { - const controller = new AbortController() - controller.abort(reason) - return controller.signal -} - -declare global { - interface AbortController { - abort(reason: unknown): void - } -} - -/*#__PURE__*/ -export function isSupported(): boolean { - return 'abort' in AbortSignal && typeof AbortSignal.abort === 'function' -} - -/*#__PURE__*/ -export function isPolyfilled(): boolean { - return AbortSignal.abort === abortSignalAbort -} - -export function apply(): void { - if (!isSupported()) { - AbortSignal.abort = abortSignalAbort - } -} diff --git a/src/abortsignal-timeout.ts b/src/abortsignal-timeout.ts index e9f6fac..dbdd085 100644 --- a/src/abortsignal-timeout.ts +++ b/src/abortsignal-timeout.ts @@ -12,16 +12,22 @@ declare global { /*#__PURE__*/ export function isSupported(): boolean { - return 'abort' in AbortSignal && typeof AbortSignal.timeout === 'function' + return ( + 'abort' in AbortSignal && + // @ts-expect-error `.timeout` + typeof AbortSignal.timeout === 'function' + ) } /*#__PURE__*/ export function isPolyfilled(): boolean { + // @ts-expect-error `.timeout` return AbortSignal.timeout === abortSignalTimeout } export function apply(): void { if (!isSupported()) { + // @ts-expect-error `.timeout` AbortSignal.timeout = abortSignalTimeout } } diff --git a/src/array-findlast.ts b/src/array-findlast.ts deleted file mode 100644 index 516235e..0000000 --- a/src/array-findlast.ts +++ /dev/null @@ -1,26 +0,0 @@ -export function arrayFindLast( - this: T[], - pred: (this: T[], value: T, i: number, array: T[]) => boolean, - recv = this, -): T | void { - for (let i = this.length - 1; i >= 0; i -= 1) { - if (pred.call(recv, this[i], i, this)) return this[i] - } -} - -/*#__PURE__*/ -export function isSupported(): boolean { - return 'findLast' in Array.prototype && typeof Array.prototype.findLast === 'function' -} - -/*#__PURE__*/ -export function isPolyfilled(): boolean { - return Array.prototype.findLast === arrayFindLast -} - -export function apply(): void { - if (!isSupported()) { - const defn = {value: arrayFindLast, writable: true, configurable: true} - Object.defineProperty(Array.prototype, 'findLast', defn) - } -} diff --git a/src/array-findlastindex.ts b/src/array-findlastindex.ts deleted file mode 100644 index 092e350..0000000 --- a/src/array-findlastindex.ts +++ /dev/null @@ -1,31 +0,0 @@ -export function arrayFindLastIndex( - this: T[], - pred: (this: T[], value: T, i: number, array: T[]) => boolean, - recv = this, -): number { - for (let i = this.length - 1; i >= 0; i -= 1) { - if (pred.call(recv, this[i], i, this)) return i - } - return -1 -} - -/*#__PURE__*/ -export function isSupported(): boolean { - return 'findLastIndex' in Array.prototype && typeof Array.prototype.findLastIndex === 'function' -} - -/*#__PURE__*/ -export function isPolyfilled(): boolean { - return Array.prototype.findLastIndex === arrayFindLastIndex -} - -export function apply(): void { - if (!isSupported()) { - const defn = { - value: arrayFindLastIndex, - writable: true, - configurable: true, - } - Object.defineProperty(Array.prototype, 'findLastIndex', defn) - } -} diff --git a/src/arraylike-at.ts b/src/arraylike-at.ts deleted file mode 100644 index 256dbb5..0000000 --- a/src/arraylike-at.ts +++ /dev/null @@ -1,40 +0,0 @@ -const TypedArray = Reflect.getPrototypeOf(Int8Array) as Int8ArrayConstructor | null - -export function arrayLikeAt(this: ArrayLike, i: number): T | void { - const l = this.length - i = Math.trunc(i) || 0 - if (i < 0) i += l - return i < 0 || i >= l ? undefined : this[i] -} - -/*#__PURE__*/ -export function isSupported(): boolean { - return ( - 'at' in Array.prototype && - typeof Array.prototype.at === 'function' && - 'at' in String.prototype && - typeof String.prototype.at === 'function' && - typeof TypedArray === 'function' && - 'at' in TypedArray.prototype && - typeof TypedArray.prototype.at === 'function' - ) -} - -/*#__PURE__*/ -export function isPolyfilled(): boolean { - return ( - Array.prototype.at === arrayLikeAt && - String.prototype.at === arrayLikeAt && - typeof TypedArray === 'function' && - TypedArray.prototype.at === arrayLikeAt - ) -} - -export function apply(): void { - if (!isSupported()) { - const defn = {value: arrayLikeAt, writable: true, configurable: true} - Object.defineProperty(Array.prototype, 'at', defn) - Object.defineProperty(String.prototype, 'at', defn) - Object.defineProperty(TypedArray, 'at', defn) - } -} diff --git a/src/crypto-randomuuid.ts b/src/crypto-randomuuid.ts deleted file mode 100644 index 6fc83cd..0000000 --- a/src/crypto-randomuuid.ts +++ /dev/null @@ -1,33 +0,0 @@ -export function randomUUID(): `${string}-${string}-${string}-${string}-${string}` { - const buf = new Uint32Array(4) - crypto.getRandomValues(buf) - let idx = -1 - return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { - idx++ - const r = (buf[idx >> 3] >> ((idx % 8) * 4)) & 15 - const v = c === 'x' ? r : (r & 0x3) | 0x8 - return v.toString(16) - }) as `${string}-${string}-${string}-${string}-${string}` -} - -declare global { - interface Crypto { - randomUUID(): string - } -} - -/*#__PURE__*/ -export function isSupported(): boolean { - return typeof crypto === 'object' && 'randomUUID' in crypto && typeof crypto.randomUUID === 'function' -} - -/*#__PURE__*/ -export function isPolyfilled(): boolean { - return isSupported() && crypto.randomUUID === randomUUID -} - -export function apply(): void { - if (!isSupported()) { - crypto.randomUUID = randomUUID - } -} diff --git a/src/event-abortsignal.ts b/src/event-abortsignal.ts deleted file mode 100644 index b355519..0000000 --- a/src/event-abortsignal.ts +++ /dev/null @@ -1,48 +0,0 @@ -const originalAddEventListener = EventTarget.prototype.addEventListener -export function addEventListenerWithAbortSignal( - this: EventTarget, - type: string, - callback: EventListenerOrEventListenerObject | null, - options?: AddEventListenerOptions | boolean, -): void { - if (typeof options === 'object' && 'signal' in options && options.signal instanceof AbortSignal) { - if (options.signal.aborted) return - originalAddEventListener.call(options.signal, 'abort', () => { - this.removeEventListener(type, callback, options) - }) - } - return originalAddEventListener.call(this, type, callback, options) -} - -declare global { - interface AddEventListenerOptions { - signal?: AbortSignal - } -} - -export function isSupported(): boolean { - let signalSupported = false - const setSignalSupported = () => (signalSupported = true) - - function noop() {} - const options = Object.create({}, {signal: {get: setSignalSupported}}) - try { - const target = new EventTarget() - target.addEventListener('test', noop, options) - target.removeEventListener('test', noop, options) - return signalSupported - } catch { - return signalSupported - } -} - -/*#__PURE__*/ -export function isPolyfilled(): boolean { - return EventTarget.prototype.addEventListener === addEventListenerWithAbortSignal -} - -export function apply(): void { - if (typeof AbortSignal === 'function' && !isSupported()) { - EventTarget.prototype.addEventListener = addEventListenerWithAbortSignal - } -} diff --git a/src/form-requestsubmit.ts b/src/form-requestsubmit.ts deleted file mode 100644 index 20e1bcc..0000000 --- a/src/form-requestsubmit.ts +++ /dev/null @@ -1,32 +0,0 @@ -export function requestSubmit( - this: HTMLFormElement, - submitter: HTMLButtonElement | HTMLInputElement | null = null, -): void { - const event = new SubmitEvent('submit', {bubbles: true, cancelable: true, submitter}) - let input - if (submitter && submitter.name) { - input = Object.assign(document.createElement('input'), { - type: 'hidden', - hidden: true, - name: submitter.name, - value: submitter.value, - }) - this.append(input) - } - this.checkValidity() && !this.dispatchEvent(event) && this.submit() - input?.remove() -} - -export function isSupported(): boolean { - return 'requestSubmit' in HTMLFormElement.prototype && typeof HTMLFormElement.prototype.requestSubmit === 'function' -} - -export function isPolyfilled(): boolean { - return HTMLFormElement.prototype.requestSubmit === requestSubmit -} - -export function apply(): void { - if (!isSupported()) { - HTMLFormElement.prototype.requestSubmit = requestSubmit - } -} diff --git a/src/index.ts b/src/index.ts index 1581473..52624c4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,35 +1,11 @@ -import * as abortSignalAbort from './abortsignal-abort.js' import * as abortSignalTimeout from './abortsignal-timeout.js' -import * as arrayAt from './arraylike-at.js' import * as clipboardItem from './clipboarditem.js' -import * as cryptoRandomUUID from './crypto-randomuuid.js' import * as elementCheckVisibility from './element-checkvisibility.js' -import * as eventAbortSignal from './event-abortsignal.js' import * as navigatorClipboard from './navigator-clipboard.js' -import * as formRequestSubmit from './form-requestsubmit.js' -import * as objectHasOwn from './object-hasown.js' import * as requestIdleCallback from './requestidlecallback.js' -import * as arrayFindLast from './array-findlast.js' -import * as arrayFindLastIndex from './array-findlastindex.js' export const baseSupport = - typeof Blob === 'function' && - typeof PerformanceObserver === 'function' && - typeof Intl === 'object' && - typeof MutationObserver === 'function' && - typeof URLSearchParams === 'function' && - typeof WebSocket === 'function' && - typeof IntersectionObserver === 'function' && - typeof queueMicrotask === 'function' && - typeof TextEncoder === 'function' && - typeof TextDecoder === 'function' && - typeof customElements === 'object' && - typeof HTMLDetailsElement === 'function' && - typeof AbortController === 'function' && - typeof AbortSignal === 'function' && typeof globalThis === 'object' && - 'entries' in FormData.prototype && - 'toggleAttribute' in Element.prototype && // ES2019 'fromEntries' in Object && 'flatMap' in Array.prototype && @@ -41,32 +17,29 @@ export const baseSupport = 'replaceAll' in String.prototype && 'any' in Promise && // ES2022 - // 'at' in String.prototype && // Polyfilled - // 'at' in Array.prototype && // Polyfilled - // 'hasOwn' in Object && // Polyfilled + 'at' in String.prototype && + 'at' in Array.prototype && + 'hasOwn' in Object && // ESNext - // 'abort' in AbortSignal && // Polyfilled + 'abort' in AbortSignal && // 'timeout' in AbortSignal && // Polyfilled + // DOM / HTML and other specs + typeof queueMicrotask === 'function' && + typeof HTMLDialogElement === 'function' && typeof AggregateError === 'function' && - // 'randomUUID' in crypto && // Polyfilled + typeof BroadcastChannel === 'function' && + 'randomUUID' in crypto && 'replaceChildren' in Element.prototype && + 'requestSubmit' in HTMLFormElement.prototype && // 'requestIdleCallback' in window && // Polyfilled true export const polyfills = { - abortSignalAbort, abortSignalTimeout, - arrayAt, clipboardItem, - cryptoRandomUUID, elementCheckVisibility, - eventAbortSignal, navigatorClipboard, - formRequestSubmit, - objectHasOwn, requestIdleCallback, - arrayFindLast, - arrayFindLastIndex, } export function isSupported() { diff --git a/src/object-hasown.ts b/src/object-hasown.ts deleted file mode 100644 index 574d42b..0000000 --- a/src/object-hasown.ts +++ /dev/null @@ -1,33 +0,0 @@ -const phasOwn = Object.prototype.hasOwnProperty -export function objectHasOwn(object: unknown, property: PropertyKey) { - if (object == null) { - throw new TypeError('Cannot convert undefined or null to object') - } - return phasOwn.call(Object(object), property) -} - -declare global { - interface Object { - hasOwn: (object: unknown, property: Key) => object is unknown & Record - } -} - -/*#__PURE__*/ -export function isSupported(): boolean { - return 'hasOwn' in Object && typeof Object.hasOwn === 'function' -} - -/*#__PURE__*/ -export function isPolyfilled(): boolean { - return Object.hasOwn === objectHasOwn -} - -export function apply(): void { - if (!isSupported()) { - Object.defineProperty(Object, 'hasOwn', { - value: objectHasOwn, - configurable: true, - writable: true, - }) - } -} diff --git a/test/abortsignal-abort.js b/test/abortsignal-abort.js deleted file mode 100644 index 707cb61..0000000 --- a/test/abortsignal-abort.js +++ /dev/null @@ -1,24 +0,0 @@ -import {abortSignalAbort, apply, isPolyfilled, isSupported} from '../lib/abortsignal-abort.js' - -describe('abortSignalAbort', () => { - it('has standard isSupported, isPolyfilled, apply API', () => { - expect(isSupported).to.be.a('function') - expect(isPolyfilled).to.be.a('function') - expect(apply).to.be.a('function') - expect(isSupported()).to.be.a('boolean') - expect(isPolyfilled()).to.equal(false) - }) - - it('returns already aborted signal', () => { - expect(abortSignalAbort()).to.be.instanceOf(AbortSignal) - expect(abortSignalAbort().aborted).to.equal(true) - }) - - it('creates a new signal each time', () => { - expect(abortSignalAbort()).to.not.equal(abortSignalAbort()) - }) - - it('forwards reason property', () => { - expect(abortSignalAbort('Foo')).to.have.property('reason', 'Foo') - }) -}) diff --git a/test/array-findlast.js b/test/array-findlast.js deleted file mode 100644 index 2933f12..0000000 --- a/test/array-findlast.js +++ /dev/null @@ -1,30 +0,0 @@ -import {apply, arrayFindLast, isPolyfilled, isSupported} from '../lib/array-findlast.js' - -describe('arrayFindLast', () => { - it('has standard isSupported, isPolyfilled, apply API', () => { - expect(isSupported).to.be.a('function') - expect(isPolyfilled).to.be.a('function') - expect(apply).to.be.a('function') - expect(isSupported()).to.be.a('boolean') - expect(isPolyfilled()).to.equal(false) - }) - - it('returns value that passes truthy', () => { - expect(arrayFindLast.call([1, 2, 3], v => v === 3)).to.equal(3) - expect(arrayFindLast.call([1, 2, 3], v => v === 1)).to.equal(1) - const arr = [1, 2, 3] - const recv = {} - expect( - arrayFindLast.call( - arr, - function (v, i, _arr) { - // eslint-disable-next-line @typescript-eslint/no-invalid-this - expect(this).to.equal(recv) - expect(_arr).to.equal(arr) - expect(v).to.equal(arr[i]) - }, - recv, - ), - ) - }) -}) diff --git a/test/array-findlastindex.js b/test/array-findlastindex.js deleted file mode 100644 index eecdfd1..0000000 --- a/test/array-findlastindex.js +++ /dev/null @@ -1,30 +0,0 @@ -import {apply, arrayFindLastIndex, isPolyfilled, isSupported} from '../lib/array-findlastindex.js' - -describe('arrayFindLastIndex', () => { - it('has standard isSupported, isPolyfilled, apply API', () => { - expect(isSupported).to.be.a('function') - expect(isPolyfilled).to.be.a('function') - expect(apply).to.be.a('function') - expect(isSupported()).to.be.a('boolean') - expect(isPolyfilled()).to.equal(false) - }) - - it('returns value that passes truthy', () => { - expect(arrayFindLastIndex.call([1, 2, 3], v => v === 3)).to.equal(2) - expect(arrayFindLastIndex.call([1, 2, 3], v => v === 1)).to.equal(0) - const arr = [1, 2, 3] - const recv = {} - expect( - arrayFindLastIndex.call( - arr, - function (v, i, _arr) { - // eslint-disable-next-line @typescript-eslint/no-invalid-this - expect(this).to.equal(recv) - expect(_arr).to.equal(arr) - expect(v).to.equal(arr[i]) - }, - recv, - ), - ) - }) -}) diff --git a/test/arraylike-at.js b/test/arraylike-at.js deleted file mode 100644 index 8c75106..0000000 --- a/test/arraylike-at.js +++ /dev/null @@ -1,17 +0,0 @@ -import {apply, arrayLikeAt, isPolyfilled, isSupported} from '../lib/arraylike-at.js' - -describe('arrayLikeAt', () => { - it('has standard isSupported, isPolyfilled, apply API', () => { - expect(isSupported).to.be.a('function') - expect(isPolyfilled).to.be.a('function') - expect(apply).to.be.a('function') - expect(isSupported()).to.be.a('boolean') - expect(isPolyfilled()).to.equal(false) - }) - - it('returns value at given index', () => { - expect(arrayLikeAt.call([1, 2, 3], -1)).to.equal(3) - expect(arrayLikeAt.call('bar', -2)).to.equal('a') - expect(arrayLikeAt.call('bar', 1)).to.equal('a') - }) -}) diff --git a/test/crypto-randomuuid.js b/test/crypto-randomuuid.js deleted file mode 100644 index 67ce2f5..0000000 --- a/test/crypto-randomuuid.js +++ /dev/null @@ -1,18 +0,0 @@ -import {apply, isPolyfilled, isSupported, randomUUID} from '../lib/crypto-randomuuid.js' - -describe('randomUUID', () => { - it('has standard isSupported, isPolyfilled, apply API', () => { - expect(isSupported).to.be.a('function') - expect(isPolyfilled).to.be.a('function') - expect(apply).to.be.a('function') - expect(isSupported()).to.be.a('boolean') - expect(isPolyfilled()).to.equal(false) - }) - - it('returns a UUID string', async () => { - expect(randomUUID()).to.match( - /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/, - ) - expect(randomUUID()).to.not.equal(randomUUID()) - }) -}) diff --git a/test/event-abortsignal.js b/test/event-abortsignal.js deleted file mode 100644 index 2e2edcd..0000000 --- a/test/event-abortsignal.js +++ /dev/null @@ -1,25 +0,0 @@ -import {addEventListenerWithAbortSignal, apply, isPolyfilled, isSupported} from '../lib/event-abortsignal.js' - -describe('addEventListenerWithAbortSignal', () => { - it('has standard isSupported, isPolyfilled, apply API', () => { - expect(isSupported).to.be.a('function') - expect(isPolyfilled).to.be.a('function') - expect(apply).to.be.a('function') - expect(isSupported()).to.be.a('boolean') - expect(isPolyfilled()).to.equal(false) - }) - - it('adds event listener, with abortable signal', async () => { - const et = new EventTarget() - const ac = new AbortController() - let i = 0 - const incr = () => (i += 1) - addEventListenerWithAbortSignal.call(et, 'test', incr, {signal: ac.signal}) - et.dispatchEvent(new Event('test')) - et.dispatchEvent(new Event('test')) - expect(i).to.equal(2) - ac.abort() - et.dispatchEvent(new Event('test')) - expect(i).to.equal(2) - }) -}) diff --git a/test/form-requestsubmit.js b/test/form-requestsubmit.js deleted file mode 100644 index 577c9af..0000000 --- a/test/form-requestsubmit.js +++ /dev/null @@ -1,73 +0,0 @@ -import {requestSubmit, apply, isPolyfilled, isSupported} from '../lib/form-requestsubmit.js' - -describe('requestSubmit', () => { - let form - beforeEach(() => { - form = document.createElement('form') - document.body.append(form) - }) - afterEach(() => { - form.remove() - }) - - it('has standard isSupported, isPolyfilled, apply API', () => { - expect(isSupported).to.be.a('function') - expect(isPolyfilled).to.be.a('function') - expect(apply).to.be.a('function') - expect(isSupported()).to.be.a('boolean') - expect(isPolyfilled()).to.equal(false) - }) - - it('does not dispatch or submit for invalid forms', () => { - const input = document.createElement('input') - input.required = true - form.append(input) - let called = false - form.addEventListener('submit', () => { - called = true - }) - requestSubmit.call(form) - expect(called).to.equal(false) - }) - - it('dispatches submit event', () => { - const input = document.createElement('input') - form.append(input) - let called = false - form.addEventListener('submit', event => { - called = true - event.stopPropagation() - }) - requestSubmit.call(form) - expect(called).to.equal(true) - }) - - it('passes submitter in event', () => { - const input = document.createElement('input') - input.type = 'button' - form.append(input) - let submitter = null - form.addEventListener('submit', event => { - submitter = event.submitter - event.stopPropagation() - }) - requestSubmit.call(form, input) - expect(submitter).to.equal(input) - }) - - it('includes the input value in FormData', () => { - const input = document.createElement('input') - input.type = 'button' - input.name = 'foo' - input.value = '1' - form.append(input) - let formdata = null - form.addEventListener('submit', event => { - formdata = new FormData(form) - event.stopPropagation() - }) - requestSubmit.call(form, input) - expect(formdata.get('foo')).to.equal('1') - expect(Array.from(form.querySelectorAll('input')).length).to.equal(1) - }) -}) diff --git a/test/object-hasown.js b/test/object-hasown.js deleted file mode 100644 index 22b89d2..0000000 --- a/test/object-hasown.js +++ /dev/null @@ -1,16 +0,0 @@ -import {apply, isPolyfilled, isSupported, objectHasOwn} from '../lib/object-hasown.js' - -describe('objectHasOwn', () => { - it('has standard isSupported, isPolyfilled, apply API', () => { - expect(isSupported).to.be.a('function') - expect(isPolyfilled).to.be.a('function') - expect(apply).to.be.a('function') - expect(isSupported()).to.be.a('boolean') - expect(isPolyfilled()).to.equal(false) - }) - - it('returns boolean based on own properties', () => { - expect(objectHasOwn({a: 1}, 'a')).to.equal(true) - expect(objectHasOwn({a: 1}, 'b')).to.equal(false) - }) -}) From bc9b81652f10b5723cdad2f8aa2f9c6f2acf733a Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Mon, 29 Jan 2024 18:24:21 +0000 Subject: [PATCH 2/2] remove no longer needed @ts-expect-error comments --- src/abortsignal-timeout.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/abortsignal-timeout.ts b/src/abortsignal-timeout.ts index dbdd085..e9f6fac 100644 --- a/src/abortsignal-timeout.ts +++ b/src/abortsignal-timeout.ts @@ -12,22 +12,16 @@ declare global { /*#__PURE__*/ export function isSupported(): boolean { - return ( - 'abort' in AbortSignal && - // @ts-expect-error `.timeout` - typeof AbortSignal.timeout === 'function' - ) + return 'abort' in AbortSignal && typeof AbortSignal.timeout === 'function' } /*#__PURE__*/ export function isPolyfilled(): boolean { - // @ts-expect-error `.timeout` return AbortSignal.timeout === abortSignalTimeout } export function apply(): void { if (!isSupported()) { - // @ts-expect-error `.timeout` AbortSignal.timeout = abortSignalTimeout } }