or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

Core Util Is

Core Util Is provides the util.is* functions introduced in Node.js v0.12, offering consistent type checking utilities across different Node.js versions. It implements reliable type detection that avoids the security vulnerabilities and fragility associated with instanceof checks.

Package Information

  • Package Name: core-util-is
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install core-util-is

Core Imports

const {
  isArray,
  isBoolean,
  isNull,
  isNullOrUndefined,
  isNumber,
  isString,
  isSymbol,
  isUndefined,
  isRegExp,
  isObject,
  isDate,
  isError,
  isFunction,
  isPrimitive,
  isBuffer
} = require('core-util-is');

Note: This is a CommonJS package and does not support ES module imports.

Basic Usage

const { isArray, isString, isNumber, isPrimitive } = require('core-util-is');

// Check basic types
console.log(isArray([])); // true
console.log(isString('hello')); // true
console.log(isNumber(42)); // true

// Check for primitive values
console.log(isPrimitive(null)); // true
console.log(isPrimitive('text')); // true
console.log(isPrimitive({})); // false

// Robust object type checking
console.log(isArray({ [Symbol.toStringTag]: 'Array' })); // true (handles Symbol.toStringTag)
console.log(isDate(new Date())); // true
console.log(isRegExp(/pattern/)); // true

Capabilities

Array Type Checking

Checks if a value is an array, using Array.isArray() when available or falling back to Object.prototype.toString.call().

/**
 * Checks if argument is an array
 * @param {any} arg - Value to check
 * @returns {boolean} - True if the value is an array
 */
function isArray(arg);

Boolean Type Checking

Checks if a value is a boolean primitive.

/**
 * Checks if argument is a boolean
 * @param {any} arg - Value to check
 * @returns {boolean} - True if the value is a boolean
 */
function isBoolean(arg);

Null Type Checking

Checks if a value is strictly null.

/**
 * Checks if argument is null
 * @param {any} arg - Value to check
 * @returns {boolean} - True if the value is null
 */
function isNull(arg);

Null or Undefined Type Checking

Checks if a value is null or undefined using loose equality.

/**
 * Checks if argument is null or undefined
 * @param {any} arg - Value to check
 * @returns {boolean} - True if the value is null or undefined
 */
function isNullOrUndefined(arg);

Number Type Checking

Checks if a value is a number primitive.

/**
 * Checks if argument is a number
 * @param {any} arg - Value to check
 * @returns {boolean} - True if the value is a number
 */
function isNumber(arg);

String Type Checking

Checks if a value is a string primitive.

/**
 * Checks if argument is a string
 * @param {any} arg - Value to check
 * @returns {boolean} - True if the value is a string
 */
function isString(arg);

Symbol Type Checking

Checks if a value is a symbol (ES6).

/**
 * Checks if argument is a symbol
 * @param {any} arg - Value to check
 * @returns {boolean} - True if the value is a symbol
 */
function isSymbol(arg);

Undefined Type Checking

Checks if a value is undefined.

/**
 * Checks if argument is undefined
 * @param {any} arg - Value to check
 * @returns {boolean} - True if the value is undefined
 */
function isUndefined(arg);

RegExp Type Checking

Checks if a value is a regular expression object using Object.prototype.toString.call().

/**
 * Checks if argument is a regular expression
 * @param {any} re - Value to check
 * @returns {boolean} - True if the value is a RegExp
 */
function isRegExp(re);

Object Type Checking

Checks if a value is an object (excluding null).

/**
 * Checks if argument is an object (excluding null)
 * @param {any} arg - Value to check
 * @returns {boolean} - True if the value is an object and not null
 */
function isObject(arg);

Date Type Checking

Checks if a value is a Date object using Object.prototype.toString.call().

/**
 * Checks if argument is a Date object
 * @param {any} d - Value to check
 * @returns {boolean} - True if the value is a Date
 */
function isDate(d);

Error Type Checking

Checks if a value is an Error object using Object.prototype.toString.call() or instanceof Error.

/**
 * Checks if argument is an Error object
 * @param {any} e - Value to check
 * @returns {boolean} - True if the value is an Error
 */
function isError(e);

Function Type Checking

Checks if a value is a function.

/**
 * Checks if argument is a function
 * @param {any} arg - Value to check
 * @returns {boolean} - True if the value is a function
 */
function isFunction(arg);

Primitive Type Checking

Checks if a value is a primitive (null, boolean, number, string, symbol, or undefined).

/**
 * Checks if argument is a primitive value
 * @param {any} arg - Value to check
 * @returns {boolean} - True if the value is a primitive type
 */
function isPrimitive(arg);

Buffer Type Checking

Checks if a value is a Buffer object. This function is re-exported from require('buffer').Buffer.isBuffer.

/**
 * Checks if argument is a Buffer object
 * @param {any} obj - Value to check
 * @returns {boolean} - True if the value is a Buffer
 */
function isBuffer(obj);

Usage Examples

Basic type validation:

const { isString, isNumber, isArray } = require('core-util-is');

function validateUser(user) {
  if (!isString(user.name)) {
    throw new Error('Name must be a string');
  }
  if (!isNumber(user.age)) {
    throw new Error('Age must be a number');
  }
  if (!isArray(user.hobbies)) {
    throw new Error('Hobbies must be an array');
  }
}

Filtering arrays by type:

const { isPrimitive, isObject } = require('core-util-is');

const mixed = [1, 'hello', {}, null, true, [], new Date()];
const primitives = mixed.filter(isPrimitive); // [1, 'hello', null, true]
const objects = mixed.filter(isObject); // [{}, [], Date object]

Safe property access:

const { isObject, isFunction } = require('core-util-is');

function safeCall(obj, methodName, ...args) {
  if (isObject(obj) && isFunction(obj[methodName])) {
    return obj[methodName](...args);
  }
  return undefined;
}