Skip to content

Commit

Permalink
feat: add isWeakSet function (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jul 9, 2024
1 parent 73e70c1 commit aacd5be
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
9 changes: 9 additions & 0 deletions benchmarks/typed/isWeakSet.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as _ from 'radashi'
import { bench } from 'vitest'

describe('isWeakSet', () => {
bench('with no arguments', () => {
_.isWeakSet()
})
})

16 changes: 16 additions & 0 deletions docs/typed/isWeakSet.mdx
Original file line number Diff line number Diff line change
@@ -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
```
1 change: 1 addition & 0 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
7 changes: 7 additions & 0 deletions src/typed/isWeakSet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { isTagged } from 'radashi'

export function isWeakSet<T extends WeakKey = WeakKey>(
value: unknown,
): value is WeakSet<T> {
return isTagged(value, '[object WeakSet]')
}
23 changes: 23 additions & 0 deletions tests/typed/isWeakSet.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})

0 comments on commit aacd5be

Please sign in to comment.