Skip to content

Utils

The @typeix/utils package: type predicates, uuid, flatten and isEqual, with zero runtime dependencies.

3 min read Source

Utility package for Typeix, mainly helper functions.

Zero dependencies. Every predicate is a single typeof / instanceof check, so V8 inlines them at the call site — measured cost is the same as writing the check by hand.

Installing

npm i @typeix/utils --save

Predicates

Function True when Notes
isNull(v) v === null
isUndefined(v) typeof v === "undefined"
isDefined(v) not null and not undefined the inverse of both
isBoolean(v) typeof v === "boolean"
isString(v) typeof v === "string"
isNumber(v) number and not NaN isNumber(NaN) === false — it means valid number
isFunction(v) typeof v === "function" classes are functions
isObject(v) non-null typeof v === "object" arrays and null are excluded, arrays need isArray
isArray(v) Array.isArray(v)
isSymbol(v) symbol, including a boxed Symbol object
isDate(v) [object Date]
isRegExp(v) [object RegExp]
isClass(v) function whose source starts with class slower than the others (parses toString()), 0.4 µs
isFalsy(v) null, undefined, "", false, 0, NaN
isTruthy(v) !isFalsy(v)
inArray(arr, token) array contains the token uses indexOf

Other helpers

uuid()

import {uuid} from "@typeix/utils";
uuid(); // "05b7534c-c63b-4330-a77e-2845ffbaa825"

A v4 UUID from globalThis.crypto.randomUUID(). Used as the unique suffix of decorator metadata keys, so it must be collision free — it carries 122 bits of cryptographic entropy.

The global is used rather than import {randomUUID} from "crypto", which keeps this package dependency free and usable outside Node.

flatten(value)

flatten([[1, 2], [3, [4, 5]], 6]); // [1, 2, 3, 4, 5, 6]
flatten([[[]], 1]);                // [1]
flatten("not an array");           // []

Any depth, order preserved, nested empty arrays dropped, non-array input returns []. Iterative implementation: one visit per element, so cost is linear in the number of elements.

isEqual(a, b)

Deep comparison of numbers (with NaN equal to NaN), dates (by ISO string), regexps (by source), arrays and plain objects. Symbols are never equal. It is a structural comparison, not a cycle-safe one.

isEqual({a: {b: [1, 2]}}, {a: {b: [1, 2]}}); // true
isEqual(NaN, NaN);                          // true

Performance notes

Measured on Node 26:

cost per call
isString 0.0007 µs
isArray, isObject, isDefined, isFunction, isNumber, isUndefined, isNull, isFalsy, isTruthy ~0.004 µs
uuid() 0.04 µs
flatten (100 elements) 0.32 µs
isEqual (3 level object) 0.17 µs
isClass 0.42 µs

The predicates are at the floor: isObject(v) costs the same as the inline v !== null && typeof v === "object". Do not wrap them expecting a speedup.