guardz
Version:
A simple and lightweight TypeScript type guard library for runtime type validation.
51 lines (50 loc) • 1.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isBigInt = void 0;
const generateTypeGuardError_1 = require("./generateTypeGuardError");
/**
* Checks if a value is a BigInt.
*
* This validates values of the BigInt type, which is used for integers
* larger than the safe integer range in JavaScript (beyond 2^53 - 1).
* Useful for validating large identifiers, financial amounts, or timestamps.
*
* @param value - The value to check
* @param config - Optional configuration for error handling
* @returns True if the value is a BigInt, false otherwise
*
* @example
* ```typescript
* import { isBigInt } from 'guardz';
*
* console.log(isBigInt(123n)); // true
* console.log(isBigInt(BigInt(123))); // true
* console.log(isBigInt(BigInt("9007199254740991"))); // true
* console.log(isBigInt(123)); // false (regular number)
* console.log(isBigInt("123n")); // false (string)
* console.log(isBigInt(null)); // false
*
* // Useful for validating large IDs or financial data
* const data: unknown = getDataFromAPI();
* if (isBigInt(data)) {
* // data is now typed as bigint
* console.log(`Large ID: ${data.toString()}`);
* }
*
* // Common use cases:
* // - Database IDs that exceed safe integer range
* // - Financial amounts in smallest currency units (e.g., satoshis)
* // - High-precision timestamps
* // - Large mathematical calculations
* ```
*/
const isBigInt = function (value, config) {
if (typeof value !== 'bigint') {
if (config) {
config.callbackOnError((0, generateTypeGuardError_1.generateTypeGuardError)(value, config.identifier, 'bigint'));
}
return false;
}
return true;
};
exports.isBigInt = isBigInt;