diff --git a/readme.md b/readme.md index 90ac932..a517379 100644 --- a/readme.md +++ b/readme.md @@ -198,6 +198,7 @@ is.boundFunction(function () {}); ##### .set(value) ##### .weakMap(value) ##### .weakSet(value) +##### .weakRef(value) #### Typed arrays diff --git a/source/index.ts b/source/index.ts index 6457799..4a3df85 100644 --- a/source/index.ts +++ b/source/index.ts @@ -1,5 +1,5 @@ import type {Buffer} from 'node:buffer'; -import type {Class, Falsy, TypedArray, ObservableLike, Primitive} from './types.js'; +import type {Class, Falsy, TypedArray, ObservableLike, Primitive, WeakRef} from './types.js'; const typedArrayTypeNames = [ 'Int8Array', @@ -40,6 +40,7 @@ const objectTypeNames = [ 'Set', 'WeakMap', 'WeakSet', + 'WeakRef', 'ArrayBuffer', 'SharedArrayBuffer', 'DataView', @@ -230,6 +231,8 @@ is.weakMap = (value: unknown): val is.weakSet = (value: unknown): value is WeakSet => isObjectOfType>('WeakSet')(value); // eslint-disable-line @typescript-eslint/ban-types +is.weakRef = (value: unknown): value is WeakRef => isObjectOfType>('WeakRef')(value); // eslint-disable-line @typescript-eslint/ban-types + is.int8Array = isObjectOfType('Int8Array'); is.uint8Array = isObjectOfType('Uint8Array'); is.uint8ClampedArray = isObjectOfType('Uint8ClampedArray'); @@ -524,6 +527,7 @@ interface Assert { set: (value: unknown) => asserts value is Set; weakMap: (value: unknown) => asserts value is WeakMap; // eslint-disable-line @typescript-eslint/ban-types weakSet: (value: unknown) => asserts value is WeakSet; // eslint-disable-line @typescript-eslint/ban-types + weakRef: (value: unknown) => asserts value is WeakRef; // eslint-disable-line @typescript-eslint/ban-types int8Array: (value: unknown) => asserts value is Int8Array; uint8Array: (value: unknown) => asserts value is Uint8Array; uint8ClampedArray: (value: unknown) => asserts value is Uint8ClampedArray; @@ -629,6 +633,7 @@ export const assert: Assert = { set: (value: unknown): asserts value is Set => assertType(is.set(value), 'Set', value), weakMap: (value: unknown): asserts value is WeakMap => assertType(is.weakMap(value), 'WeakMap', value), // eslint-disable-line @typescript-eslint/ban-types weakSet: (value: unknown): asserts value is WeakSet => assertType(is.weakSet(value), 'WeakSet', value), // eslint-disable-line @typescript-eslint/ban-types + weakRef: (value: unknown): asserts value is WeakRef => assertType(is.weakRef(value), 'WeakRef', value), // eslint-disable-line @typescript-eslint/ban-types int8Array: (value: unknown): asserts value is Int8Array => assertType(is.int8Array(value), 'Int8Array', value), uint8Array: (value: unknown): asserts value is Uint8Array => assertType(is.uint8Array(value), 'Uint8Array', value), uint8ClampedArray: (value: unknown): asserts value is Uint8ClampedArray => assertType(is.uint8ClampedArray(value), 'Uint8ClampedArray', value), diff --git a/source/types.ts b/source/types.ts index 3940da2..d49b282 100644 --- a/source/types.ts +++ b/source/types.ts @@ -49,3 +49,8 @@ export interface ObservableLike { // eslint-disable-next-line @typescript-eslint/ban-types export type Falsy = false | 0 | 0n | '' | null | undefined; + +export interface WeakRef { // eslint-disable-line @typescript-eslint/ban-types + readonly [Symbol.toStringTag]: 'WeakRef'; + deref(): T | undefined; +} diff --git a/test/test.ts b/test/test.ts index c74f76a..f9334ee 100644 --- a/test/test.ts +++ b/test/test.ts @@ -336,6 +336,12 @@ const types = new Map([ ], typename: 'WeakSet', }], + ['weakRef', { + is: is.weakRef, + assert: assert.weakRef, + fixtures: window.WeakRef ? [new window.WeakRef({})] : [], + typename: 'WeakRef', + }], ['weakMap', { is: is.weakMap, assert: assert.weakMap, @@ -798,6 +804,10 @@ test('is.weakSet', t => { testType(t, 'weakSet'); }); +test('is.weakRef', t => { + testType(t, 'weakRef'); +}); + test('is.int8Array', t => { testType(t, 'int8Array'); }); @@ -1223,7 +1233,7 @@ test('is.arrayLike', t => { t.false(is.arrayLike(new Map())); (function () { - t.notThrows(() => { + t.notThrows(function () { assert.arrayLike(arguments); // eslint-disable-line prefer-rest-params }); })();