guardz
Version:
A simple and lightweight TypeScript type guard library for runtime type validation.
43 lines (42 loc) • 1.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isNilOr = isNilOr;
const typeGuardMeta_1 = require("../utils/typeGuardMeta");
/**
* 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.
*
* @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) {
function isNilOrGuard(value, config) {
if (value === null || value === undefined) {
return true;
}
return typeGuardFn(value, config);
}
return (0, typeGuardMeta_1.attachTypeGuardMeta)(isNilOrGuard, {
innerGuard: typeGuardFn,
wrapperKind: 'nilOr',
});
}