feat(compat) : implement isArrayBuffer compat (#644)

* feat : add isArrayBuffer compat

* fix : typo compat bench

* Update src/predicate/isArrayBuffer.ts

---------

Co-authored-by: Sojin Park <raon0211@gmail.com>
This commit is contained in:
HyunWoo Choi 2024-10-03 17:23:25 +09:00 committed by GitHub
parent 458d0bf321
commit e8826c5a60
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 72 additions and 2 deletions

View File

@ -1,6 +1,11 @@
import { bench, describe } from 'vitest';
import { isArrayBuffer as isArrayBufferToolkit } from 'es-toolkit';
import { isArrayBuffer as isArrayBufferLodash } from 'lodash';
import { isArrayBuffer as isArrayBufferToolkit_ } from 'es-toolkit';
import { isArrayBuffer as isArrayBufferToolkitCompat_ } from 'es-toolkit/compat';
import { isArrayBuffer as isArrayBufferLodash_ } from 'lodash';
const isArrayBufferToolkit = isArrayBufferToolkit_;
const isArrayBufferToolkitCompat = isArrayBufferToolkitCompat_;
const isArrayBufferLodash = isArrayBufferLodash_;
describe('isArrayBuffer', () => {
bench('es-toolkit/isArrayBuffer', () => {
@ -11,6 +16,14 @@ describe('isArrayBuffer', () => {
isArrayBufferToolkit(new Map());
});
bench('es-toolkit/compat/isArrayBuffer', () => {
isArrayBufferToolkitCompat(new ArrayBuffer(16));
isArrayBufferToolkitCompat(null);
isArrayBufferToolkitCompat([]);
isArrayBufferToolkitCompat(new Object());
isArrayBufferToolkitCompat(new Map());
});
bench('lodash/isArrayBuffer', () => {
isArrayBufferLodash(new ArrayBuffer(16));
isArrayBufferLodash(null);

View File

@ -83,6 +83,7 @@ export { invertBy } from './object/invertBy.ts';
export { isPlainObject } from './predicate/isPlainObject.ts';
export { isArray } from './predicate/isArray.ts';
export { isArguments } from './predicate/isArguments.ts';
export { isArrayBuffer } from './predicate/isArrayBuffer.ts';
export { isArrayLike } from './predicate/isArrayLike.ts';
export { isSymbol } from './predicate/isSymbol.ts';
export { isObject } from './predicate/isObject.ts';

View File

@ -0,0 +1,33 @@
import { describe, expect, it } from 'vitest';
import { isArrayBuffer } from './isArrayBuffer';
import { args } from '../_internal/args';
import { falsey } from '../_internal/falsey';
import { slice } from '../_internal/slice';
import { stubFalse } from '../_internal/stubFalse';
import { symbol } from '../_internal/symbol';
describe('isArrayBuffer', () => {
it('should return `true` for array buffers', () => {
expect(isArrayBuffer(new ArrayBuffer(8))).toBe(true);
});
it('should return `false` for non array buffers', () => {
const expected = falsey.map(() => stubFalse());
const actual = falsey.map((value, index) => (index ? isArrayBuffer(value) : isArrayBuffer()));
expect(actual).toEqual(expected);
expect(isArrayBuffer(args)).toBe(false);
expect(isArrayBuffer([1])).toBe(false);
expect(isArrayBuffer(true)).toBe(false);
expect(isArrayBuffer(new Date())).toBe(false);
expect(isArrayBuffer(new Error())).toBe(false);
expect(isArrayBuffer(slice)).toBe(false);
expect(isArrayBuffer({ a: 1 })).toBe(false);
expect(isArrayBuffer(1)).toBe(false);
expect(isArrayBuffer(/x/)).toBe(false);
expect(isArrayBuffer('a')).toBe(false);
expect(isArrayBuffer(symbol)).toBe(false);
});
});

View File

@ -0,0 +1,23 @@
import { isArrayBuffer as isArrayBufferToolkit } from '../../predicate/isArrayBuffer.ts';
/**
* 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 isArrayBufferToolkit(value);
}