-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |