diff --git a/benchmarks/typed/isWeakSet.bench.ts b/benchmarks/typed/isWeakSet.bench.ts new file mode 100644 index 00000000..7a32c8d5 --- /dev/null +++ b/benchmarks/typed/isWeakSet.bench.ts @@ -0,0 +1,9 @@ +import * as _ from 'radashi' +import { bench } from 'vitest' + +describe('isWeakSet', () => { + bench('with no arguments', () => { + _.isWeakSet() + }) +}) + diff --git a/docs/typed/isWeakSet.mdx b/docs/typed/isWeakSet.mdx new file mode 100644 index 00000000..9ca3089d --- /dev/null +++ b/docs/typed/isWeakSet.mdx @@ -0,0 +1,16 @@ +--- +title: isWeakSet +description: Returns true for WeakSet instances +--- + +## Basic usage + +Returns true for `WeakSet` instances, even if they are subclass instances or from +other [realms](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof#instanceof_and_multiple_realms). + +```ts +import * as _ from 'radashi' + +_.isWeakSet(new WeakSet()) // true +_.isWeakSet(new (class extends WeakSet {})()) // true +``` diff --git a/src/mod.ts b/src/mod.ts index 1307a1dc..b2d87fd7 100644 --- a/src/mod.ts +++ b/src/mod.ts @@ -114,5 +114,6 @@ export * from './typed/isSet.ts' export * from './typed/isString.ts' export * from './typed/isSymbol.ts' export * from './typed/isTagged.ts' +export * from './typed/isWeakSet.ts' export * from './types' diff --git a/src/typed/isWeakSet.ts b/src/typed/isWeakSet.ts new file mode 100644 index 00000000..a8e8034a --- /dev/null +++ b/src/typed/isWeakSet.ts @@ -0,0 +1,7 @@ +import { isTagged } from 'radashi' + +export function isWeakSet( + value: unknown, +): value is WeakSet { + return isTagged(value, '[object WeakSet]') +} diff --git a/tests/typed/isWeakSet.test.ts b/tests/typed/isWeakSet.test.ts new file mode 100644 index 00000000..45ead106 --- /dev/null +++ b/tests/typed/isWeakSet.test.ts @@ -0,0 +1,23 @@ +import * as vm from 'node:vm' +import * as _ from 'radashi' + +describe('isWeakSet', () => { + test('returns true for WeakSet instances', () => { + expect(_.isWeakSet(new WeakSet())).toBe(true) + }) + test('returns true for WeakSet subclass instances', () => { + expect(_.isWeakSet(new (class extends WeakSet {})())).toBe(true) + }) + test('returns true for WeakSet instances from other realms', () => { + expect(_.isWeakSet(vm.runInNewContext('new WeakSet()'))).toBe(true) + }) + test('returns false for undefined', () => { + expect(_.isWeakSet(undefined)).toBe(false) + }) + test('returns false for null', () => { + expect(_.isWeakSet(null)).toBe(false) + }) + test('returns false for non-WeakSet objects', () => { + expect(_.isWeakSet({})).toBe(false) + }) +})