-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aacd5be
commit f32cfd5
Showing
5 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import * as _ from 'radashi' | ||
import { bench } from 'vitest' | ||
|
||
describe('isWeakMap', () => { | ||
bench('with no arguments', () => { | ||
_.isWeakMap() | ||
}) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
title: isWeakMap | ||
description: Returns true for WeakMap instances | ||
--- | ||
|
||
## Basic usage | ||
|
||
Returns true for `WeakMap` 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' | ||
|
||
_.isWeakMap(new WeakMap()) // true | ||
_.isWeakMap(new (class extends WeakMap {})()) // true | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { isTagged } from 'radashi' | ||
|
||
export function isWeakMap<K extends WeakKey = WeakKey, V = unknown>( | ||
value: unknown, | ||
): value is WeakMap<K, V> { | ||
return isTagged(value, '[object WeakMap]') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('isWeakMap', () => { | ||
test('returns true for WeakMap instances', () => { | ||
expect(_.isWeakMap(new WeakMap())).toBe(true) | ||
}) | ||
test('returns true for WeakMap subclass instances', () => { | ||
expect(_.isWeakMap(new (class extends WeakMap {})())).toBe(true) | ||
}) | ||
test('returns true for WeakMap instances from other realms', () => { | ||
expect(_.isWeakMap(vm.runInNewContext('new WeakMap()'))).toBe(true) | ||
}) | ||
test('returns false for undefined', () => { | ||
expect(_.isWeakMap(undefined)).toBe(false) | ||
}) | ||
test('returns false for null', () => { | ||
expect(_.isWeakMap(null)).toBe(false) | ||
}) | ||
test('returns false for non-WeakMap objects', () => { | ||
expect(_.isWeakMap({})).toBe(false) | ||
}) | ||
}) |