predictype
Version:
PredicType is a library of pre-built and tested predicates for TypeScript, covering various data types and operations.
37 lines (36 loc) • 1.41 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.functionNamePattern = functionNamePattern;
const functions_js_1 = require("../../enums/functions.js");
/**
* Checks if the function name matches a given regular expression pattern using the specified operation.
*
* @param source The function to check.
* @param oper The pattern operation to perform (e.g. 'matches').
* @param pattern The regular expression to test against the function name.
* @returns True if the pattern check is valid according to the operator, otherwise false.
*
* @throws {Error} If the operation is not recognized.
*
* @example
* const fn = function fooBar() {};
* const pattern = /^foo/;
*
* functionPattern(fn, 'matches', pattern); // true
*
* @remarks
* Supported Operators:
* - **MATCHES**: Function name matches the RegExp pattern
* - **NOT_MATCHES**: Function name does not match the pattern
*/
function functionNamePattern(source, oper, pattern) {
const operators = {
[functions_js_1.FunctionNamePatternEnum.MATCHES]: (a, b) => b.test(a.name),
[functions_js_1.FunctionNamePatternEnum.NOT_MATCHES]: (a, b) => !b.test(a.name),
};
const enumOper = typeof oper === 'string' ? oper : oper;
const fn = operators[enumOper];
if (!fn)
throw new Error(`Unknown FunctionNamePattern operation: ${oper}`);
return fn(source, pattern);
}
;