yummies/type-guard
Description
Runtime type guards and narrowers for primitives, DOM nodes, NaN, and common app values. Each helper is implemented once with consistent typeof / Object.prototype.toString logic so unknown from JSON, events, or third-party scripts becomes safe typed branches without ad-hoc checks scattered through the codebase.
Usage
import { typeGuard } from "yummies/type-guard";typeGuard.isDefined()
Checks that a value is neither null nor undefined.
Examples:
isDefined(0); // trueisDefined(null); // falsetypeGuard.isTruthy()
Checks whether a value is truthy (!!value).
Examples:
isTruthy(1); // trueisTruthy(0); // falsetypeGuard.isFalsy()
Checks whether a value is falsy (!Boolean(value)).
Examples:
isFalsy(null); // trueisFalsy("x"); // falsetypeGuard.isNull
Checks whether a value is exactly null.
Examples:
isNull(null); // trueisNull(undefined); // falsetypeGuard.isUndefined
Checks whether a value is exactly undefined.
Examples:
isUndefined(undefined); // trueisUndefined('value'); // falsetypeGuard.isObject
Checks whether a value is a plain object.
Examples:
isObject({ id: 1 }); // trueisObject([]); // falsetypeGuard.isArray
Checks whether a value is an array.
Examples:
isArray([1, 2, 3]); // trueisArray({ length: 1 }); // falsetypeGuard.isString
Checks whether a value is a string object or primitive string.
Examples:
isString('hello'); // trueisString(123); // falsetypeGuard.isNumber
Checks whether a value is a finite number.
Unlike isNaN and isInfinite, this guard only matches regular numeric values.
Examples:
isNumber(123); // trueisNumber(Number.NaN); // falsetypeGuard.isBoolean
Checks whether a value is a boolean.
Examples:
isBoolean(true); // trueisBoolean('true'); // falsetypeGuard.isFunction
Checks whether a value is a synchronous or asynchronous function.
Examples:
isFunction(() => {}); // trueisFunction(async () => {}); // truetypeGuard.isRegExp
Checks whether a value is a regular expression.
Examples:
isRegExp(/foo/); // trueisRegExp('foo'); // falsetypeGuard.isElement
Checks whether a value looks like a DOM element or document node.
Examples:
isElement(document.body); // trueisElement({ nodeType: 3 }); // falsetypeGuard.isNaN
Checks whether a value is NaN.
Examples:
isNaN(Number.NaN); // trueisNaN(5); // falsetypeGuard.isInfinite
Checks whether a value is positive or negative infinity.
Examples:
isInfinite(Infinity); // trueisInfinite(10); // falsetypeGuard.isSymbol
Checks whether a value is a symbol.
Examples:
isSymbol(Symbol('id')); // trueisSymbol('id'); // false