guardz
Version:
A simple and lightweight TypeScript type guard library for runtime type validation.
41 lines (40 loc) • 1.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isNil = void 0;
const generateTypeGuardError_1 = require("./generateTypeGuardError");
/**
* Checks if a value is null or undefined (nil).
*
* @param value - The value to check
* @param config - Optional configuration for error handling
* @returns True if the value is null or undefined, false otherwise
*
* @example
* ```typescript
* import { isNil } from 'guardz';
*
* console.log(isNil(null)); // true
* console.log(isNil(undefined)); // true
* console.log(isNil("")); // false
* console.log(isNil(0)); // false
* console.log(isNil(false)); // false
* console.log(isNil({})); // false
*
* // Useful for checking if a value needs initialization
* const data: unknown = getUserInput();
* if (isNil(data)) {
* // data is now typed as null | undefined
* console.log("Value is null or undefined");
* }
* ```
*/
const isNil = function (value, config) {
if (value !== null && value !== undefined) {
if (config) {
config.callbackOnError((0, generateTypeGuardError_1.generateTypeGuardError)(value, config.identifier, 'null | undefined'));
}
return false;
}
return true;
};
exports.isNil = isNil;