UNPKG

predictype

Version:

PredicType is a library of pre-built and tested predicates for TypeScript, covering various data types and operations.

47 lines (46 loc) 2.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.functionArity = functionArity; const functions_js_1 = require("../../enums/functions.js"); /** * Checks the arity (number of parameters) of a function using the specified operation. * * @param source The function to check. * @param oper The arity operation to perform (e.g. 'arity_equals', 'arity_greater_than'). * @param arity The arity (number of parameters) to compare against. * @returns True if the arity check is valid according to the operator, otherwise false. * * @throws {Error} If the operation is not recognized. * * @example * const fn1 = function(a, b) {}; * const fn2 = function(a, b, c) {}; * const arity2 = 2; * * functionArity(fn1, 'arity_equals', arity2); // true * functionArity(fn2, 'arity_greater_than', arity2); // true * * @remarks * Supported Operators: * - **EQUALS**: Function arity equals the given value * - **NOT_EQUALS**: Function arity does not equal the value * - **GREATER_THAN**: Function arity is greater than the value * - **GREATER_OR_EQUAL**: Function arity is greater or equal to value * - **LESS_THAN**: Function arity is less than the value * - **LESS_OR_EQUAL**: Function arity is less or equal to value */ function functionArity(source, oper, arity) { const operators = { [functions_js_1.FunctionArityEnum.EQUALS]: (a, b) => a.length === b, [functions_js_1.FunctionArityEnum.NOT_EQUALS]: (a, b) => a.length !== b, [functions_js_1.FunctionArityEnum.GREATER_THAN]: (a, b) => a.length > b, [functions_js_1.FunctionArityEnum.GREATER_OR_EQUAL]: (a, b) => a.length >= b, [functions_js_1.FunctionArityEnum.LESS_THAN]: (a, b) => a.length < b, [functions_js_1.FunctionArityEnum.LESS_OR_EQUAL]: (a, b) => a.length <= b, }; const enumOper = typeof oper === 'string' ? oper : oper; const fn = operators[enumOper]; if (!fn) throw new Error(`Unknown FunctionArity operation: ${oper}`); return fn(source, arity); }