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:
Dayong Lee 2024-09-03 23:33:38 +09:00 committed by GitHub
parent 959b5d08c6
commit 158435c1e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 200 additions and 4 deletions

View 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);
});
});

View File

@ -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' },
],
},
{

View File

@ -211,6 +211,7 @@ function sidebar(): DefaultTheme.Sidebar {
text: 'isUndefined',
link: '/ko/reference/predicate/isUndefined',
},
{ text: 'isWeakMap', link: '/ko/reference/predicate/isWeakMap' },
],
},
{

View File

@ -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) | ❌ |

View File

@ -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) | ❌ |

View File

@ -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) | ❌ |

View 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
```

View 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
```

View File

@ -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) | ❌ |

View File

@ -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';

View 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);
});
});

View 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);
}

View File

@ -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';

View 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);
});
});

View 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;
}