predictype
Version:
PredicType is a library of pre-built and tested predicates for TypeScript, covering various data types and operations.
48 lines (47 loc) • 1.81 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.bigintComparison = bigintComparison;
const bigints_js_1 = require("../../enums/bigints.js");
/**
* Compares two bigint values using the specified operation.
*
* @param source The source bigint value.
* @param oper The comparison operation to perform (e.g. 'equals', 'greater_than').
* @param target The target bigint value to compare against the source.
* @returns True if the comparison is valid according to the operator, otherwise false.
*
* @throws {Error} If the operation is not recognized.
*
* @example
* const a = BigInt(10);
* const b = BigInt(5);
* const c = BigInt(20);
*
* bigintComparison(a, 'equals', a); // true
* bigintComparison(a, 'greater_than', b); // true
* bigintComparison(a, 'less_than', c); // true
*
* @remarks
* Supported Operators:
* - **EQUALS**: a === b
* - **NOT_EQUALS**: a !== b
* - **GREATER_THAN**: a > b
* - **GREATER_THAN_OR_EQUALS**: a >= b
* - **LESS_THAN**: a < b
* - **LESS_THAN_OR_EQUALS**: a <= b
*/
function bigintComparison(source, oper, target) {
const operators = {
[bigints_js_1.BigIntComparisonEnum.EQUALS]: (a, b) => a === b,
[bigints_js_1.BigIntComparisonEnum.NOT_EQUALS]: (a, b) => a !== b,
[bigints_js_1.BigIntComparisonEnum.GREATER_THAN]: (a, b) => a > b,
[bigints_js_1.BigIntComparisonEnum.GREATER_THAN_OR_EQUALS]: (a, b) => a >= b,
[bigints_js_1.BigIntComparisonEnum.LESS_THAN]: (a, b) => a < b,
[bigints_js_1.BigIntComparisonEnum.LESS_THAN_OR_EQUALS]: (a, b) => a <= b,
};
const enumOper = typeof oper === 'string' ? oper : oper;
const fn = operators[enumOper];
if (!fn)
throw new Error(`Unknown BigIntComparison operation: ${oper}`);
return fn(source, target);
}
;