should.js Utility Functions
should-util is a foundational utility library within the should.js assertion ecosystem. It provides a collection of core helper functions, primarily type-checking utilities (e.g., `isString`, `isArray`, `isFunction`) and a `deepEqual` comparison function. The package itself is highly stable, having been at version 1.0.1 since its initial releases over a decade ago, with the last code commit occurring 7 years prior. As such, it follows a 'maintenance' release cadence, receiving no active development or new features. Its primary differentiator is its integral role in the should.js assertion library, offering common, reusable functionalities that are robust and well-tested, though not independently maintained for general-purpose use. It is predominantly a CommonJS module, reflecting its age.
Common errors
-
TypeError: (0 , should_util__WEBPACK_IMPORTED_MODULE_0__.isString) is not a function
cause Attempting to use ES module named imports in a bundler that struggles with CJS interop, or directly in an environment that expects explicit ES exports. This usually means the bundler didn't correctly resolve `module.exports` to named imports.fixTry importing the entire module as a default import and then accessing properties: `import util from 'should-util'; console.log(util.isString('test'))`. Alternatively, configure your bundler (e.g., Webpack, Rollup) to better handle CommonJS modules, or ensure `module.exports` is correctly identified as the source of named exports. -
Error [ERR_REQUIRE_ESM]: require() of ES Module ... Not supported by CommonJS 'require'.
cause This error would typically occur if `should-util` were an ESM package being `require()`d. However, `should-util` is CJS. If you encounter this, it likely means another dependency or the consuming project is configured as ESM-only, and the tooling is failing to correctly load `should-util` as CJS.fixEnsure your project's module resolution (`tsconfig.json#moduleResolution`, `package.json#type`) is compatible with CommonJS modules. If you are in a pure ESM environment, you may need to use a dynamic `import()` or find an ESM-native alternative for the desired functionality.
Warnings
- gotcha The `should-util` package is primarily a CommonJS module. While modern bundlers can often convert `import` statements, it does not explicitly define ES module entry points using the `package.json#exports` field. This may lead to compatibility issues in pure ESM environments or older tooling.
- gotcha This package is in maintenance mode with no active development. The last commit was 7 years ago, and there have been no new releases in a decade. While stable, it lacks modern features, TypeScript definitions, or updates for newer JavaScript language constructs or environments.
- gotcha Many of the basic type-checking utilities (e.g., `isString`, `isArray`) provided by `should-util` have native JavaScript equivalents (e.g., `typeof ''`, `Array.isArray()`) or are readily available in more modern, actively maintained utility libraries (e.g., Lodash, Ramda). Over-reliance on this specific package for basic tasks might introduce an unnecessary, unmaintained dependency.
Install
-
npm install should-util -
yarn add should-util -
pnpm add should-util
Imports
- isString
const { isString } = require('should-util')import { isString } from 'should-util' - deepEqual
import deepEqual from 'should-util/lib/deepEqual'
import { deepEqual } from 'should-util' - isArray
const isArray = require('should-util').isArrayimport { isArray } from 'should-util'
Quickstart
import { isString, isArray, deepEqual } from 'should-util';
// Example 1: Type checking a string
const myString = 'Hello, world!';
console.log(`'${myString}' is a string: ${isString(myString)}`);
// Expected: 'Hello, world!' is a string: true
// Example 2: Type checking an array
const myArray = [1, 2, 3];
console.log(`${JSON.stringify(myArray)} is an array: ${isArray(myArray)}`);
// Expected: [1,2,3] is an array: true
// Example 3: Deep equality comparison
const objA = { a: 1, b: { c: 2 } };
const objB = { a: 1, b: { c: 2 } };
const objC = { a: 1, b: { c: 3 } };
console.log(`objA deepEqual objB: ${deepEqual(objA, objB)}`);
// Expected: objA deepEqual objB: true
console.log(`objA deepEqual objC: ${deepEqual(objA, objC)}`);
// Expected: objA deepEqual objC: false
// Example 4: Demonstrating other exports
import util from 'should-util'; // For older bundlers or direct CommonJS require
console.log(`Is null undefined? ${util.isUndefined(null)}`);
console.log(`Is undefined undefined? ${util.isUndefined(undefined)}`);