CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-is-buffer

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

93

1.02x
Quality

Pending

Does it follow best practices?

Impact

93%

1.02x

Average score across 9 eval scenarios

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

task.mdevals/scenario-4/

Binary Data Validator

Build a data validation utility that safely checks whether incoming data is binary buffer data across different JavaScript runtime environments. The validator should handle various input types gracefully without throwing errors, even when given unexpected or invalid inputs.

Requirements

Your implementation should:

  1. Accept any type of input value and determine if it represents binary buffer data
  2. Return a boolean result (true for buffer data, false for anything else)
  3. Handle edge cases safely:
    • Null and undefined values should return false without errors
    • Plain objects, arrays, strings, numbers, and functions should return false
    • Objects that have buffer-like properties but aren't actual buffers should return false
  4. Work correctly in both Node.js and browser environments (when bundled)
  5. Provide TypeScript type definitions for type-safe usage

Implementation

Create a file src/validator.js (or src/validator.ts for TypeScript) that exports a single function isBinaryData(value) which returns a boolean.

Test Cases { .test-cases }

Create test file src/validator.test.js with the following test cases:

Test: Valid buffer types @test

const result1 = isBinaryData(Buffer.alloc(10));
const result2 = isBinaryData(Buffer.from([1, 2, 3, 4]));
const result3 = isBinaryData(Buffer.allocUnsafe(20));

console.log(result1); // true
console.log(result2); // true
console.log(result3); // true

Test: Invalid types @test

const result1 = isBinaryData(null);
const result2 = isBinaryData(undefined);
const result3 = isBinaryData("string data");
const result4 = isBinaryData(12345);
const result5 = isBinaryData([1, 2, 3]);
const result6 = isBinaryData({ data: "object" });

console.log(result1); // false
console.log(result2); // false
console.log(result3); // false
console.log(result4); // false
console.log(result5); // false
console.log(result6); // false

Test: Edge cases @test

const result1 = isBinaryData({ constructor: null });
const result2 = isBinaryData({ isBuffer: null });

console.log(result1); // false
console.log(result2); // false

Dependencies { .dependencies }

is-buffer { .dependency }

A utility for detecting Buffer objects across different JavaScript environments.

tile.json