or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-is-buffer

Determine if an object is a Buffer without including the full buffer module

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/is-buffer@2.0.x

To install, run

npx @tessl/cli install tessl/npm-is-buffer@2.0.0

index.mddocs/

is-buffer

is-buffer provides a lightweight utility function for determining whether an object is a Buffer instance without requiring the full buffer module in browserify environments. It offers cross-platform compatibility between Node.js and browser environments while maintaining zero runtime dependencies and minimal bundle size.

Package Information

  • Package Name: is-buffer
  • Package Type: npm
  • Language: JavaScript (with TypeScript definitions)
  • Installation: npm install is-buffer

Core Imports

const isBuffer = require('is-buffer');

For ESM:

import isBuffer from 'is-buffer';

For TypeScript:

import isBuffer = require('is-buffer');
// or modern syntax:
import isBuffer from 'is-buffer';

Basic Usage

const isBuffer = require('is-buffer');

// Check various types
console.log(isBuffer(Buffer.from('hello')));     // true
console.log(isBuffer(Buffer.alloc(4)));          // true
console.log(isBuffer('string'));                 // false
console.log(isBuffer(null));                     // false
console.log(isBuffer({}));                       // false

Capabilities

Buffer Detection

Determines if an object is a Buffer instance by checking the object's constructor and its isBuffer method, providing reliable detection without including the full buffer module.

/**
 * Determine if an object is a Buffer
 * @param obj - The object to test
 * @returns true if the object is a Buffer, false otherwise
 */
function isBuffer(obj: any): boolean;

Parameters:

  • obj (any): The object to test for Buffer instance

Returns:

  • boolean: true if the object is a Buffer, false otherwise

Implementation Details:

  • Returns false immediately if obj is null or undefined
  • Checks that obj.constructor exists and is not null
  • Verifies that obj.constructor.isBuffer is a function
  • Calls obj.constructor.isBuffer(obj) to perform the actual Buffer check
  • Works with both Node.js Buffer and browserify Buffer implementations

Supported Buffer Types:

  • Buffer.from() - Creates Buffer from various sources
  • Buffer.alloc() - Creates zero-filled Buffer of specified size
  • Buffer.allocUnsafeSlow() - Creates uninitialized Buffer
  • Any object with a constructor implementing the isBuffer method

Non-Buffer Types (returns false):

  • Primitive types: undefined, null, string, number, boolean
  • Objects: {}, [], functions
  • Objects with malformed isBuffer properties