feat(isArrayBuffer) : add isArrayBuffer function (#624)

* feat : add isArrayBuffer function

* feat : isArrayBuffer bench

* docs : isArrayBuffer
This commit is contained in:
HyunWoo Choi 2024-10-01 13:59:23 +09:00 committed by GitHub
parent 20426dfdcc
commit 8f7e0960cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,21 @@
import { bench, describe } from 'vitest';
import { isArrayBuffer as isArrayBufferToolkit } from 'es-toolkit';
import { isArrayBuffer as isArrayBufferLodash } from 'lodash';
describe('isArrayBuffer', () => {
bench('es-toolkit/isArrayBuffer', () => {
isArrayBufferToolkit(new ArrayBuffer(16));
isArrayBufferToolkit(null);
isArrayBufferToolkit([]);
isArrayBufferToolkit(new Object());
isArrayBufferToolkit(new Map());
});
bench('lodash/isArrayBuffer', () => {
isArrayBufferLodash(new ArrayBuffer(16));
isArrayBufferLodash(null);
isArrayBufferLodash([]);
isArrayBufferLodash(new Object());
isArrayBufferLodash(new Map());
});
});

View File

@ -0,0 +1,33 @@
# isArrayBuffer
주어진 값이 `ArrayBuffer`의 인스턴스인지 확인해요.
값이 `ArrayBuffer`이면 `true`, 아니면 `false`를 반환해요.
TypeScript의 타입 가드로 주로 사용되는데요, 파라미터로 주어진 값을 `ArrayBuffer`인 타입으로 좁힐 수 있어요.
## 인터페이스
```typescript
function isArrayBuffer(value: unknown): value is ArrayBuffer;
```
### 파라미터
- `value` (`unknown`): `ArrayBuffer`인지 확인할 값.
### 반환 값
(`value is ArrayBuffer`): 값이 `ArrayBuffer`이면 `true`, 아니면 `false`.
## 예시
```typescript
const value1 = new ArrayBuffer();
const value2 = new Array();
const value3 = new Map();
console.log(isArrayBuffer(value1)); // true
console.log(isArrayBuffer(value2)); // false
console.log(isArrayBuffer(value3)); // false
```

View File

@ -0,0 +1,34 @@
# isArrayBuffer
Checks if the given value is a `ArrayBuffer`.
This function tests whether the provided value is an instance of `ArrayBuffer`.
It returns `true` if the value is a `ArrayBuffer`, and `false` otherwise.
This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `ArrayBuffer`.
## Signature
```typescript
function isArrayBuffer(value: unknown): value is ArrayBuffer;
```
### Parameters
- `value` (`unknown`): The value to check if it is a `ArrayBuffer`.
### Returns
(`value is ArrayBuffer`): true if the value is a `ArrayBuffer`, false otherwise.
## Examples
```typescript
const value1 = new ArrayBuffer();
const value2 = new Array();
const value3 = new Map();
console.log(isArrayBuffer(value1)); // true
console.log(isArrayBuffer(value2)); // false
console.log(isArrayBuffer(value3)); // false
```

View File

@ -1,3 +1,4 @@
export { isArrayBuffer } from './isArrayBuffer.ts';
export { isDate } from './isDate.ts'; export { isDate } from './isDate.ts';
export { isEqual } from './isEqual.ts'; export { isEqual } from './isEqual.ts';
export { isError } from './isError.ts'; export { isError } from './isError.ts';

View File

@ -0,0 +1,17 @@
import { describe, expect, it } from 'vitest';
import { isArrayBuffer } from './isArrayBuffer';
describe('isArrayBuffer', () => {
it('returns true if the value is a ArrayBuffer', () => {
expect(isArrayBuffer(new ArrayBuffer(8))).toBe(true);
});
it('returns false if the value is not a ArrayBuffer', () => {
expect(isArrayBuffer(null)).toBe(false);
expect(isArrayBuffer('')).toBe(false);
expect(isArrayBuffer(123)).toBe(false);
expect(isArrayBuffer({})).toBe(false);
expect(isArrayBuffer([])).toBe(false);
expect(isArrayBuffer(new Map())).toBe(false);
});
});

View File

@ -0,0 +1,21 @@
/**
* Checks if a given value is `ArrayBuffer`.
*
* This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `ArrayBuffer`.
*
* @param {unknown} value The value to check if it is a `ArrayBuffer`.
* @returns {value is ArrayBuffer} Returns `true` if `value` is a `ArrayBuffer`, else `false`.
*
* @example
* const value1 = new ArrayBuffer();
* const value2 = new Array();
* const value3 = new Map();
*
* console.log(isArrayBuffer(value1)); // true
* console.log(isArrayBuffer(value2)); // false
* console.log(isArrayBuffer(value3)); // false
*/
export function isArrayBuffer(value: unknown): value is ArrayBuffer {
return value instanceof ArrayBuffer;
}