Type checking utility functions that replicate Node.js util.is* functions for consistent type detection across versions
npx @tessl/cli install tessl/npm-core-util-is@1.0.0Core 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.
npm install core-util-isconst {
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.
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/)); // trueChecks 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);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);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);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);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);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);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);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);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);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);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);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);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);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);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);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;
}