-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat : add isSet to predicate * feat : add isSet bench * feat : add isSet test * docs : isSet
- Loading branch information
Showing
6 changed files
with
128 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,21 @@ | ||
import { bench, describe } from 'vitest'; | ||
import { isSet as isSetToolkit } from 'es-toolkit'; | ||
import { isSet as isSetLodash } from 'lodash'; | ||
|
||
describe('isSet', () => { | ||
bench('es-toolkit/isSet', () => { | ||
isSetToolkit(new Set()); | ||
isSetToolkit(new WeakSet()); | ||
isSetToolkit([]); | ||
isSetToolkit({}); | ||
isSetToolkit(null); | ||
}); | ||
|
||
bench('lodash/isSet', () => { | ||
isSetLodash(new Set()); | ||
isSetLodash(new WeakSet()); | ||
isSetLodash([]); | ||
isSetLodash({}); | ||
isSetLodash(null); | ||
}); | ||
}); |
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,33 @@ | ||
# isSet | ||
|
||
주어진 값이 `Set`의 인스턴스인지 확인해요. | ||
|
||
값이 `Set`이면 `true`, 아니면 `false`를 반환해요. | ||
|
||
TypeScript의 타입 가드로 주로 사용되는데요, 파라미터로 주어진 값을 `Set`인 타입으로 좁힐 수 있어요. | ||
|
||
## 인터페이스 | ||
|
||
```typescript | ||
function isSet(value: unknown): value is Set<any>; | ||
``` | ||
|
||
### 파라미터 | ||
|
||
- `value` (`unknown`): `Set`인지 확인할 값. | ||
|
||
### 반환 값 | ||
|
||
(`value is Set<any>`): 값이 `Set`이면 `true`, 아니면 `false`. | ||
|
||
## 예시 | ||
|
||
```typescript | ||
const value1 = new Set(); | ||
const value2 = new Map(); | ||
const value3 = new WeakSet(); | ||
|
||
console.log(isSet(value1)); // true | ||
console.log(isSet(value2)); // false | ||
console.log(isSet(value3)); // false | ||
``` |
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,34 @@ | ||
# isSet | ||
|
||
Checks if the given value is a `Set`. | ||
|
||
This function tests whether the provided value is an instance of `Set`. | ||
It returns `true` if the value is a `Set`, and `false` otherwise. | ||
|
||
This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `Set`. | ||
|
||
## Signature | ||
|
||
```typescript | ||
function isSet(value: unknown): value is Set<any>; | ||
``` | ||
|
||
### Parameters | ||
|
||
- `value` (`unknown`): The value to check if it is a `Set`. | ||
|
||
### Returns | ||
|
||
(`value is Set<any>`): true if the value is a `Set`, false otherwise. | ||
|
||
## Examples | ||
|
||
```typescript | ||
const value1 = new Set(); | ||
const value2 = new Map(); | ||
const value3 = new WeakSet(); | ||
|
||
console.log(isSet(value1)); // true | ||
console.log(isSet(value2)); // false | ||
console.log(isSet(value3)); // false | ||
``` |
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,18 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { isSet } from './isSet'; | ||
|
||
describe('isSet', () => { | ||
it('returns true if the value is a Set', () => { | ||
expect(isSet(new Set())).toBe(true); | ||
}); | ||
|
||
it('returns false if the value is not a Set', () => { | ||
expect(isSet(null)).toBe(false); | ||
expect(isSet('')).toBe(false); | ||
expect(isSet(123)).toBe(false); | ||
expect(isSet({})).toBe(false); | ||
expect(isSet([])).toBe(false); | ||
expect(isSet(new Map())).toBe(false); | ||
expect(isSet(new WeakSet())).toBe(false); | ||
}); | ||
}); |
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,21 @@ | ||
/** | ||
* Checks if a given value is `Set`. | ||
* | ||
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `string`. | ||
* | ||
* @param {unknown} value The value to check if it is a `Set`. | ||
* @returns {value is Set<any>} Returns `true` if `value` is a `Set`, else `false`. | ||
* | ||
* @example | ||
* const value1 = new Set(); | ||
* const value2 = new Map(); | ||
* const value3 = new WeakSet(); | ||
* | ||
* console.log(isSet(value1)); // true | ||
* console.log(isSet(value2)); // false | ||
* console.log(isSet(value3)); // false | ||
*/ | ||
|
||
export function isSet(value: unknown): value is Set<any> { | ||
return value instanceof Set; | ||
} |