UNPKG

guardz

Version:

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

41 lines (40 loc) 1.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isBoolean = void 0; const generateTypeGuardError_1 = require("./generateTypeGuardError"); /** * Checks if a value is a boolean. * * @param value - The value to check * @param config - Optional configuration for error handling * @returns True if the value is a boolean, false otherwise * * @example * ```typescript * import { isBoolean } from 'guardz'; * * console.log(isBoolean(true)); // true * console.log(isBoolean(false)); // true * console.log(isBoolean(1)); // false * console.log(isBoolean(0)); // false * console.log(isBoolean("true")); // false * console.log(isBoolean(null)); // false * * // With type narrowing * const data: unknown = getUserInput(); * if (isBoolean(data)) { * // data is now typed as boolean * console.log(data ? "yes" : "no"); * } * ``` */ const isBoolean = function (value, config) { if (typeof value !== 'boolean') { if (config) { config.callbackOnError((0, generateTypeGuardError_1.generateTypeGuardError)(value, config.identifier, 'boolean')); } return false; } return true; }; exports.isBoolean = isBoolean;