Skip to content

Commit

Permalink
feat(isSet): add isSet (#596)
Browse files Browse the repository at this point in the history
* feat : add isSet to predicate

* feat : add isSet bench

* feat : add isSet test

* docs : isSet
  • Loading branch information
chhw130 authored Sep 26, 2024
1 parent 7016baf commit 3cf0992
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 0 deletions.
21 changes: 21 additions & 0 deletions benchmarks/performance/isSet.bench.ts
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);
});
});
33 changes: 33 additions & 0 deletions docs/ko/reference/predicate/isSet.md
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
```
34 changes: 34 additions & 0 deletions docs/reference/predicate/isSet.md
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
```
1 change: 1 addition & 0 deletions src/predicate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export { isRegExp } from './isRegExp.ts';
export { isBoolean } from './isBoolean.ts';
export { isSymbol } from './isSymbol.ts';
export { isString } from './isString.ts';
export { isSet } from './isSet.ts';
export { isWeakMap } from './isWeakMap.ts';
export { isWeakSet } from './isWeakSet.ts';
18 changes: 18 additions & 0 deletions src/predicate/isSet.spec.ts
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);
});
});
21 changes: 21 additions & 0 deletions src/predicate/isSet.ts
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;
}

0 comments on commit 3cf0992

Please sign in to comment.