UNPKG

guardz

Version:

A simple and lightweight TypeScript type guard library for runtime type validation.

36 lines (35 loc) 1.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isNilOr = isNilOr; const isNullOr_1 = require("./isNullOr"); const isUndefinedOr_1 = require("./isUndefinedOr"); /** * A function that takes a type guard function of type T, * and returns a new function that checks if a value is either of type T, null, or undefined. * This is equivalent to `isUndefinedOr(isNullOr(typeGuardFn))`. * * @param {function} typeGuardFn - the callback function that checks if a value is of type T * @returns {function} - a function that returns true if the value is of type T, null, or undefined, false otherwise * * @example * ```typescript * import { isNilOr, isString } from 'guardz'; * * const isStringOrNil = isNilOr(isString); * * console.log(isStringOrNil("hello")); // true * console.log(isStringOrNil(null)); // true * console.log(isStringOrNil(undefined)); // true * console.log(isStringOrNil(123)); // false * * // Usage with type narrowing * const data: unknown = getDataFromSomewhere(); * if (isStringOrNil(data)) { * // data is now typed as string | null | undefined * console.log(data?.toUpperCase()); // TypeScript knows this is safe * } * ``` */ function isNilOr(typeGuardFn) { return (0, isUndefinedOr_1.isUndefinedOr)((0, isNullOr_1.isNullOr)(typeGuardFn)); }