Skip to content

Commit

Permalink
feat(compat): implement isBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Dec 15, 2024
1 parent becaac5 commit daf3a1c
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
22 changes: 22 additions & 0 deletions benchmarks/performance/isBuffer.bench.ts
Original file line number Diff line number Diff line change
@@ -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'));
});
});
1 change: 1 addition & 0 deletions src/compat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
47 changes: 47 additions & 0 deletions src/compat/predicate/isBuffer.spec.ts
Original file line number Diff line number Diff line change
@@ -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
});
});
23 changes: 23 additions & 0 deletions src/compat/predicate/isBuffer.ts
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit daf3a1c

Please sign in to comment.