@node-dlc/messaging
Version:
DLC Messaging Protocol
42 lines • 1.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.bigIntToNumber = exports.toBigInt = exports.sleep = void 0;
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
exports.sleep = sleep;
/**
* Safely converts various input types to BigInt
* @param value Value to convert to BigInt
* @returns BigInt representation of the value, or BigInt(0) for null/undefined
*/
function toBigInt(value) {
if (value === null || value === undefined)
return BigInt(0);
if (typeof value === 'bigint')
return value;
if (typeof value === 'string')
return BigInt(value);
if (typeof value === 'number')
return BigInt(value);
return BigInt(0);
}
exports.toBigInt = toBigInt;
/**
* Safely converts BigInt to number, preserving precision for safe integers
* @param value BigInt value to convert
* @returns Number if within safe range, otherwise returns the BigInt as-is for JSON serialization
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function bigIntToNumber(value) {
// For values within safe integer range, convert to number
if (value <= BigInt(Number.MAX_SAFE_INTEGER) &&
value >= BigInt(Number.MIN_SAFE_INTEGER)) {
return Number(value);
}
// For larger values, preserve as BigInt (json-bigint will handle serialization)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return value;
}
exports.bigIntToNumber = bigIntToNumber;
//# sourceMappingURL=util.js.map