diff --git a/benchmarks/performance/isBuffer.bench.ts b/benchmarks/performance/isBuffer.bench.ts new file mode 100644 index 000000000..7aa73c7aa --- /dev/null +++ b/benchmarks/performance/isBuffer.bench.ts @@ -0,0 +1,22 @@ +import { bench, describe } from 'vitest'; +import { isBuffer as isBufferToolkit_ } from 'es-toolkit'; +import { isBuffer as isBufferToolkitCompat_ } from 'es-toolkit/compat'; +import { isBuffer as isBufferLodash_ } from 'lodash'; + +const isBufferToolkit = isBufferToolkit_; +const isBufferToolkitCompat = isBufferToolkitCompat_; +const isBufferLodash = isBufferLodash_; + +describe('isBuffer', () => { + bench('es-toolkit/isBuffer', () => { + isBufferToolkit(Buffer.from('test')); + }); + + bench('es-toolkit/compat/isBuffer', () => { + isBufferToolkitCompat(Buffer.from('test')); + }); + + bench('lodash/isBuffer', () => { + isBufferLodash(Buffer.from('test')); + }); +}); diff --git a/src/compat/index.ts b/src/compat/index.ts index fa3bb833e..7d9a98d0c 100644 --- a/src/compat/index.ts +++ b/src/compat/index.ts @@ -143,6 +143,7 @@ export { isArrayBuffer } from './predicate/isArrayBuffer.ts'; export { isArrayLike } from './predicate/isArrayLike.ts'; export { isArrayLikeObject } from './predicate/isArrayLikeObject.ts'; export { isBoolean } from './predicate/isBoolean.ts'; +export { isBuffer } from './predicate/isBuffer.ts'; export { isDate } from './predicate/isDate.ts'; export { isElement } from './predicate/isElement.ts'; export { isEmpty } from './predicate/isEmpty.ts'; diff --git a/src/compat/predicate/isBuffer.spec.ts b/src/compat/predicate/isBuffer.spec.ts new file mode 100644 index 000000000..391b9cdbb --- /dev/null +++ b/src/compat/predicate/isBuffer.spec.ts @@ -0,0 +1,47 @@ +import { describe, expect, it } from 'vitest'; +import { isBuffer } from './isBuffer'; +import { args } from '../_internal/args'; +import { falsey } from '../_internal/falsey'; +import { symbol } from '../_internal/symbol'; +import { map } from '../array/map'; +import { slice } from '../array/slice'; +import { stubFalse } from '../util/stubFalse'; + +describe('isBuffer', () => { + it('should return `true` for buffers', () => { + if (Buffer) { + expect(isBuffer(Buffer.alloc(2))).toBe(true); + } + }); + + it('should return `false` for non-buffers', () => { + const expected = map(falsey, stubFalse); + + const actual = map(falsey, (value, index) => (index ? isBuffer(value) : isBuffer())); + + expect(actual).toEqual(expected); + + expect(isBuffer(args)).toBe(false); + expect(isBuffer([1])).toBe(false); + expect(isBuffer(true)).toBe(false); + expect(isBuffer(new Date())).toBe(false); + expect(isBuffer(new Error())).toBe(false); + expect(isBuffer(slice)).toBe(false); + expect(isBuffer({ a: 1 })).toBe(false); + expect(isBuffer(1)).toBe(false); + expect(isBuffer(/x/)).toBe(false); + expect(isBuffer('a')).toBe(false); + expect(isBuffer(symbol)).toBe(false); + }); + + it('should return `false` if `Buffer` is not defined', () => { + const originalBuffer = global.Buffer; + // eslint-disable-next-line + // @ts-ignore + delete global.Buffer; + + expect(isBuffer(new Uint8Array())).toBe(false); + + global.Buffer = originalBuffer; // Restore Buffer + }); +}); diff --git a/src/compat/predicate/isBuffer.ts b/src/compat/predicate/isBuffer.ts new file mode 100644 index 000000000..246921c7c --- /dev/null +++ b/src/compat/predicate/isBuffer.ts @@ -0,0 +1,23 @@ +import { isBuffer as isBufferToolkit } from '../../predicate/isBuffer.ts'; + +/** + * Checks if the given value is a Buffer instance. + * + * This function tests whether the provided value is an instance of Buffer. + * It returns `true` if the value is a Buffer, and `false` otherwise. + * + * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `Buffer`. + * + * @param {unknown} x - The value to check if it is a Buffer. + * @returns {boolean} Returns `true` if `x` is a Buffer, else `false`. + * + * @example + * const buffer = Buffer.from("test"); + * console.log(isBuffer(buffer)); // true + * + * const notBuffer = "not a buffer"; + * console.log(isBuffer(notBuffer)); // false + */ +export function isBuffer(x?: unknown): boolean { + return isBufferToolkit(x); +}