-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisBuffer.ts
More file actions
25 lines (22 loc) · 810 Bytes
/
isBuffer.ts
File metadata and controls
25 lines (22 loc) · 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { root } from "./.internal/root.js";
/** NodeJS `Buffer.isBuffer`, if available. */
// eslint-disable-next-line @typescript-eslint/unbound-method
const nativeIsBuffer = root.Buffer?.isBuffer;
/** no-op if `Buffer.isBuffer` is not available. */
const noOpIsBuffer = () => false as const;
/**
* Checks if `value` is a buffer.
*
* > In non-node environments, this function is a no-op and will always return `false`.
*
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a buffer, else `false`.
* @example
* ```ts
* isBuffer(Buffer.alloc(2)) // => true
* isBuffer(new Uint8Array(2)) // => false
* ```
*/
export const isBuffer = (typeof nativeIsBuffer === "function"
? nativeIsBuffer
: noOpIsBuffer) as unknown as (value?: unknown) => value is Buffer;