mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-24 03:32:58 +03:00
feat(isWeakMap): add isWeakMap with compatibility (#458)
* Add isWeakMap * Add compat * Add bench * Add docs * Update return type * Update docs/ko/reference/predicate/isWeakMap.md * Update docs/ko/reference/predicate/isWeakMap.md * Update src/compat/predicate/isWeakMap.ts --------- Co-authored-by: Sojin Park <raon0211@gmail.com>
This commit is contained in:
parent
959b5d08c6
commit
158435c1e6
26
benchmarks/performance/isWeakMap.bench.ts
Normal file
26
benchmarks/performance/isWeakMap.bench.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { bench, describe } from 'vitest';
|
||||
import { isWeakMap as isWeakMapToolkit } from 'es-toolkit';
|
||||
import { isWeakMap as isWeakMapToolkitCompat } from 'es-toolkit/compat';
|
||||
import { isWeakMap as isWeakMapLodash } from 'lodash';
|
||||
|
||||
describe('isWeakMap', () => {
|
||||
bench('es-toolkit/isWeakMap', () => {
|
||||
isWeakMapToolkit(new WeakMap());
|
||||
isWeakMapToolkit(new Map());
|
||||
isWeakMapToolkit('');
|
||||
isWeakMapToolkit(123);
|
||||
});
|
||||
bench('es-toolkit/compat/isWeakMap', () => {
|
||||
isWeakMapToolkitCompat(new WeakMap());
|
||||
isWeakMapToolkitCompat(new Map());
|
||||
isWeakMapToolkitCompat('');
|
||||
isWeakMapToolkitCompat(123);
|
||||
});
|
||||
|
||||
bench('lodash/isWeakMap', () => {
|
||||
isWeakMapLodash(new WeakMap());
|
||||
isWeakMapLodash(new Map());
|
||||
isWeakMapLodash('');
|
||||
isWeakMapLodash(123);
|
||||
});
|
||||
});
|
@ -198,6 +198,7 @@ function sidebar(): DefaultTheme.Sidebar {
|
||||
{ text: 'isObjectLike (compat)', link: '/reference/compat/predicate/isObjectLike' },
|
||||
{ text: 'isTypedArray', link: '/reference/predicate/isTypedArray' },
|
||||
{ text: 'isUndefined', link: '/reference/predicate/isUndefined' },
|
||||
{ text: 'isWeakMap', link: '/reference/predicate/isWeakMap' },
|
||||
],
|
||||
},
|
||||
{
|
||||
|
@ -211,6 +211,7 @@ function sidebar(): DefaultTheme.Sidebar {
|
||||
text: 'isUndefined',
|
||||
link: '/ko/reference/predicate/isUndefined',
|
||||
},
|
||||
{ text: 'isWeakMap', link: '/ko/reference/predicate/isWeakMap' },
|
||||
],
|
||||
},
|
||||
{
|
||||
|
@ -229,7 +229,7 @@ Even if a feature is marked "in review," it might already be under review to ens
|
||||
| [isSymbol](https://lodash.com/docs/4.17.15#isSymbol) | ✅ |
|
||||
| [isTypedArray](https://lodash.com/docs/4.17.15#isTypedArray) | ✅ |
|
||||
| [isUndefined](https://lodash.com/docs/4.17.15#isUndefined) | ✅ |
|
||||
| [isWeakMap](https://lodash.com/docs/4.17.15#isWeakMap) | ❌ |
|
||||
| [isWeakMap](https://lodash.com/docs/4.17.15#isWeakMap) | ✅ |
|
||||
| [isWeakSet](https://lodash.com/docs/4.17.15#isWeakSet) | ❌ |
|
||||
| [lt](https://lodash.com/docs/4.17.15#lt) | ❌ |
|
||||
| [lte](https://lodash.com/docs/4.17.15#lte) | ❌ |
|
||||
|
@ -230,7 +230,7 @@ chunk([1, 2, 3, 4], 0);
|
||||
| [isSymbol](https://lodash.com/docs/4.17.15#isSymbol) | ✅ |
|
||||
| [isTypedArray](https://lodash.com/docs/4.17.15#isTypedArray) | ✅ |
|
||||
| [isUndefined](https://lodash.com/docs/4.17.15#isUndefined) | ✅ |
|
||||
| [isWeakMap](https://lodash.com/docs/4.17.15#isWeakMap) | ❌ |
|
||||
| [isWeakMap](https://lodash.com/docs/4.17.15#isWeakMap) | ✅ |
|
||||
| [isWeakSet](https://lodash.com/docs/4.17.15#isWeakSet) | ❌ |
|
||||
| [lt](https://lodash.com/docs/4.17.15#lt) | ❌ |
|
||||
| [lte](https://lodash.com/docs/4.17.15#lte) | ❌ |
|
||||
|
@ -230,7 +230,7 @@ chunk([1, 2, 3, 4], 0);
|
||||
| [isSymbol](https://lodash.com/docs/4.17.15#isSymbol) | ✅ |
|
||||
| [isTypedArray](https://lodash.com/docs/4.17.15#isTypedArray) | ✅ |
|
||||
| [isUndefined](https://lodash.com/docs/4.17.15#isUndefined) | ✅ |
|
||||
| [isWeakMap](https://lodash.com/docs/4.17.15#isWeakMap) | ❌ |
|
||||
| [isWeakMap](https://lodash.com/docs/4.17.15#isWeakMap) | ✅ |
|
||||
| [isWeakSet](https://lodash.com/docs/4.17.15#isWeakSet) | ❌ |
|
||||
| [lt](https://lodash.com/docs/4.17.15#lt) | ❌ |
|
||||
| [lte](https://lodash.com/docs/4.17.15#lte) | ❌ |
|
||||
|
32
docs/ko/reference/predicate/isWeakMap.md
Normal file
32
docs/ko/reference/predicate/isWeakMap.md
Normal file
@ -0,0 +1,32 @@
|
||||
# isWeakMap
|
||||
|
||||
이 함수는 주어진 값이 `WeakMap`의 인스턴스인지 확인해요.
|
||||
값이 `WeakMap`이면 `true`, 아니면 `false`를 반환해요.
|
||||
|
||||
TypeScript의 타입 가드로 주로 사용되는데요, 파라미터로 주어진 값을 `WeakMap`인 타입으로 좁힐 수 있어요.
|
||||
|
||||
## 인터페이스
|
||||
|
||||
```typescript
|
||||
function isWeakMap(value: unknown): value is WeakMap<WeakKey, any>;
|
||||
```
|
||||
|
||||
### 파라미터
|
||||
|
||||
- `value` (`unknown`): `WeakMap`인지 확인할 값.
|
||||
|
||||
### 반환 값
|
||||
|
||||
(`value is WeakMap<WeakKey, any>`): 값이 `WeakMap`이면 `true`, 아니면 `false`.
|
||||
|
||||
## 예시
|
||||
|
||||
```typescript
|
||||
const value1 = new WeakMap();
|
||||
const value2 = new Map();
|
||||
const value3 = new Set();
|
||||
|
||||
console.log(isWeakMap(value1)); // true
|
||||
console.log(isWeakMap(value2)); // false
|
||||
console.log(isWeakMap(value3)); // false
|
||||
```
|
34
docs/reference/predicate/isWeakMap.md
Normal file
34
docs/reference/predicate/isWeakMap.md
Normal file
@ -0,0 +1,34 @@
|
||||
# isWeakMap
|
||||
|
||||
Checks if the given value is a `WeakMap`.
|
||||
|
||||
This function tests whether the provided value is an instance of `WeakMap`.
|
||||
It returns `true` if the value is a `WeakMap`, and `false` otherwise.
|
||||
|
||||
This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `WeakMap`.
|
||||
|
||||
## Signature
|
||||
|
||||
```typescript
|
||||
function isWeakMap(value: unknown): value is WeakMap<WeakKey, any>;
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
`value` (`unknown`): The value to test if it is a `WeakMap`.
|
||||
|
||||
### Returns
|
||||
|
||||
(`value is WeakMap<WeakKey, any>`): true if the value is a `WeakMap`, false otherwise.
|
||||
|
||||
## Examples
|
||||
|
||||
```typescript
|
||||
const value1 = new WeakMap();
|
||||
const value2 = new Map();
|
||||
const value3 = new Set();
|
||||
|
||||
console.log(isWeakMap(value1)); // true
|
||||
console.log(isWeakMap(value2)); // false
|
||||
console.log(isWeakMap(value3)); // false
|
||||
```
|
@ -229,7 +229,7 @@ chunk([1, 2, 3, 4], 0);
|
||||
| [isSymbol](https://lodash.com/docs/4.17.15#isSymbol) | ✅ |
|
||||
| [isTypedArray](https://lodash.com/docs/4.17.15#isTypedArray) | ✅ |
|
||||
| [isUndefined](https://lodash.com/docs/4.17.15#isUndefined) | ✅ |
|
||||
| [isWeakMap](https://lodash.com/docs/4.17.15#isWeakMap) | ❌ |
|
||||
| [isWeakMap](https://lodash.com/docs/4.17.15#isWeakMap) | ✅ |
|
||||
| [isWeakSet](https://lodash.com/docs/4.17.15#isWeakSet) | ❌ |
|
||||
| [lt](https://lodash.com/docs/4.17.15#lt) | ❌ |
|
||||
| [lte](https://lodash.com/docs/4.17.15#lte) | ❌ |
|
||||
|
@ -66,6 +66,7 @@ export { isRegExp } from './predicate/isRegExp.ts';
|
||||
export { isString } from './predicate/isString.ts';
|
||||
export { matches } from './predicate/matches.ts';
|
||||
export { matchesProperty } from './predicate/matchesProperty.ts';
|
||||
export { isWeakMap } from './predicate/isWeakMap.ts';
|
||||
|
||||
export { startsWith } from './string/startsWith.ts';
|
||||
export { endsWith } from './string/endsWith.ts';
|
||||
|
35
src/compat/predicate/isWeakMap.spec.ts
Normal file
35
src/compat/predicate/isWeakMap.spec.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { isWeakMap } from './isWeakMap';
|
||||
import { falsey } from '../_internal/falsey';
|
||||
import { args } from '../_internal/args';
|
||||
import { slice } from '../_internal/slice';
|
||||
import { symbol } from '../_internal/symbol';
|
||||
|
||||
describe('isWeakMap', () => {
|
||||
it('should return `true` for weak maps', () => {
|
||||
if (WeakMap) {
|
||||
expect(isWeakMap(new WeakMap())).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it('should return `false` for non weak maps', () => {
|
||||
expect(falsey.map((value, index) => (index ? isWeakMap(value) : isWeakMap()))).toEqual(falsey.map(() => false));
|
||||
expect(isWeakMap(args)).toBe(false);
|
||||
expect(isWeakMap([1, 2, 3])).toBe(false);
|
||||
expect(isWeakMap(true)).toBe(false);
|
||||
expect(isWeakMap(new Date())).toBe(false);
|
||||
expect(isWeakMap(new Error())).toBe(false);
|
||||
expect(isWeakMap(slice)).toBe(false);
|
||||
expect(isWeakMap({ a: 1 })).toBe(false);
|
||||
expect(isWeakMap(Array.prototype.map)).toBe(false);
|
||||
expect(isWeakMap(1)).toBe(false);
|
||||
expect(isWeakMap(/x/)).toBe(false);
|
||||
expect(isWeakMap('a')).toBe(false);
|
||||
expect(isWeakMap(symbol)).toBe(false);
|
||||
});
|
||||
|
||||
it('should work for objects with a non-function `constructor` (test in IE 11)', () => {
|
||||
expect(isWeakMap({ constructor: false })).toBe(false);
|
||||
expect(isWeakMap({ constructor: true })).toBe(false);
|
||||
});
|
||||
});
|
25
src/compat/predicate/isWeakMap.ts
Normal file
25
src/compat/predicate/isWeakMap.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { isWeakMap as isWeakMapToolkit } from '../../predicate/isWeakMap';
|
||||
|
||||
/**
|
||||
* Checks if the given value is a `WeakMap`.
|
||||
*
|
||||
* This function tests whether the provided value is an instance of `WeakMap`.
|
||||
* It returns `true` if the value is a `WeakMap`, and `false` otherwise.
|
||||
*
|
||||
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `WeakMap`.
|
||||
*
|
||||
* @param {unknown} value - The value to test if it is a `WeakMap`.
|
||||
* @returns {value is WeakMap<WeakKey, any>} true if the value is a `WeakMap`, false otherwise.
|
||||
*
|
||||
* @example
|
||||
* const value1 = new WeakMap();
|
||||
* const value2 = new Map();
|
||||
* const value3 = new Set();
|
||||
*
|
||||
* console.log(isWeakMap(value1)); // true
|
||||
* console.log(isWeakMap(value2)); // false
|
||||
* console.log(isWeakMap(value3)); // false
|
||||
*/
|
||||
export function isWeakMap(value?: unknown): value is WeakMap<WeakKey, any> {
|
||||
return isWeakMapToolkit(value);
|
||||
}
|
@ -12,3 +12,4 @@ export { isRegExp } from './isRegExp.ts';
|
||||
export { isBoolean } from './isBoolean.ts';
|
||||
export { isSymbol } from './isSymbol.ts';
|
||||
export { isString } from './isString.ts';
|
||||
export { isWeakMap } from './isWeakMap.ts';
|
||||
|
17
src/predicate/isWeakMap.spec.ts
Normal file
17
src/predicate/isWeakMap.spec.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { isWeakMap } from './isWeakMap';
|
||||
|
||||
describe('isWeakMap', () => {
|
||||
it('returns true if the value is WeakMap', () => {
|
||||
expect(isWeakMap(new WeakMap())).toBe(true);
|
||||
});
|
||||
|
||||
it('returns false if the value is not WeakMap', () => {
|
||||
expect(isWeakMap(null)).toBe(false);
|
||||
expect(isWeakMap('')).toBe(false);
|
||||
expect(isWeakMap(123)).toBe(false);
|
||||
expect(isWeakMap({})).toBe(false);
|
||||
expect(isWeakMap([])).toBe(false);
|
||||
expect(isWeakMap(new Map())).toBe(false);
|
||||
});
|
||||
});
|
23
src/predicate/isWeakMap.ts
Normal file
23
src/predicate/isWeakMap.ts
Normal file
@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Checks if the given value is a `WeakMap`.
|
||||
*
|
||||
* This function tests whether the provided value is an instance of `WeakMap`.
|
||||
* It returns `true` if the value is a `WeakMap`, and `false` otherwise.
|
||||
*
|
||||
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `WeakMap`.
|
||||
*
|
||||
* @param {unknown} value - The value to test if it is a `WeakMap`.
|
||||
* @returns {value is WeakMap<WeakKey, any>} true if the value is a `WeakMap`, false otherwise.
|
||||
*
|
||||
* @example
|
||||
* const value1 = new WeakMap();
|
||||
* const value2 = new Map();
|
||||
* const value3 = new Set();
|
||||
*
|
||||
* console.log(isWeakMap(value1)); // true
|
||||
* console.log(isWeakMap(value2)); // false
|
||||
* console.log(isWeakMap(value3)); // false
|
||||
*/
|
||||
export function isWeakMap(value: unknown): value is WeakMap<WeakKey, any> {
|
||||
return value instanceof WeakMap;
|
||||
}
|
Loading…
Reference in New Issue
Block a user