Skip to content

Commit

Permalink
Add .weakRef() (#165)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
bigbigDreamer and sindresorhus authored Jun 13, 2022
1 parent 06d217f commit e503a9e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ is.boundFunction(function () {});
##### .set(value)
##### .weakMap(value)
##### .weakSet(value)
##### .weakRef(value)

#### Typed arrays

Expand Down
7 changes: 6 additions & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -40,6 +40,7 @@ const objectTypeNames = [
'Set',
'WeakMap',
'WeakSet',
'WeakRef',
'ArrayBuffer',
'SharedArrayBuffer',
'DataView',
Expand Down Expand Up @@ -230,6 +231,8 @@ is.weakMap = <Key extends object = object, Value = unknown>(value: unknown): val

is.weakSet = (value: unknown): value is WeakSet<object> => isObjectOfType<WeakSet<object>>('WeakSet')(value); // eslint-disable-line @typescript-eslint/ban-types

is.weakRef = (value: unknown): value is WeakRef<object> => isObjectOfType<WeakRef<object>>('WeakRef')(value); // eslint-disable-line @typescript-eslint/ban-types

is.int8Array = isObjectOfType<Int8Array>('Int8Array');
is.uint8Array = isObjectOfType<Uint8Array>('Uint8Array');
is.uint8ClampedArray = isObjectOfType<Uint8ClampedArray>('Uint8ClampedArray');
Expand Down Expand Up @@ -524,6 +527,7 @@ interface Assert {
set: <T = unknown>(value: unknown) => asserts value is Set<T>;
weakMap: <Key extends object = object, Value = unknown>(value: unknown) => asserts value is WeakMap<Key, Value>; // eslint-disable-line @typescript-eslint/ban-types
weakSet: <T extends object = object>(value: unknown) => asserts value is WeakSet<T>; // eslint-disable-line @typescript-eslint/ban-types
weakRef: <T extends object = object>(value: unknown) => asserts value is WeakRef<T>; // 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;
Expand Down Expand Up @@ -629,6 +633,7 @@ export const assert: Assert = {
set: <T = unknown>(value: unknown): asserts value is Set<T> => assertType(is.set(value), 'Set', value),
weakMap: <Key extends object = object, Value = unknown>(value: unknown): asserts value is WeakMap<Key, Value> => assertType(is.weakMap(value), 'WeakMap', value), // eslint-disable-line @typescript-eslint/ban-types
weakSet: <T extends object = object>(value: unknown): asserts value is WeakSet<T> => assertType(is.weakSet(value), 'WeakSet', value), // eslint-disable-line @typescript-eslint/ban-types
weakRef: <T extends object = object>(value: unknown): asserts value is WeakRef<T> => 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),
Expand Down
5 changes: 5 additions & 0 deletions source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends object> { // eslint-disable-line @typescript-eslint/ban-types
readonly [Symbol.toStringTag]: 'WeakRef';
deref(): T | undefined;
}
12 changes: 11 additions & 1 deletion test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,12 @@ const types = new Map<string, Test>([
],
typename: 'WeakSet',
}],
['weakRef', {
is: is.weakRef,
assert: assert.weakRef,
fixtures: window.WeakRef ? [new window.WeakRef({})] : [],
typename: 'WeakRef',
}],
['weakMap', {
is: is.weakMap,
assert: assert.weakMap,
Expand Down Expand Up @@ -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');
});
Expand Down Expand Up @@ -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
});
})();
Expand Down

0 comments on commit e503a9e

Please sign in to comment.