predictype
Version:
PredicType is a library of pre-built and tested predicates for TypeScript, covering various data types and operations.
39 lines (38 loc) • 1.38 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.stringMembership = stringMembership;
const strings_js_1 = require("../../enums/strings.js");
/**
* Checks if a string is (or is not) a member of a set of strings using the specified operation.
*
* @param source The string to check.
* @param oper The membership operation to perform (e.g. 'in', 'not_in').
* @param target The array of strings to check membership against.
* @returns True if the membership check is valid according to the operator, otherwise false.
*
* @throws {Error} If the operation is not recognized.
*
* @example
* const arr = ['foo', 'bar'];
* const value1 = 'foo';
* const value2 = 'baz';
*
* stringMembership(value1, 'in', arr); // true
* stringMembership(value2, 'not_in', arr); // true
*
* @remarks
* Supported Operators:
* - **IN**: String is in the array
* - **NOT_IN**: String is not in the array
*/
function stringMembership(source, oper, target) {
const operators = {
[strings_js_1.StringMembershipEnum.IN]: (a, b) => b.includes(a),
[strings_js_1.StringMembershipEnum.NOT_IN]: (a, b) => !b.includes(a),
};
const enumOper = typeof oper === 'string' ? oper : oper;
const fn = operators[enumOper];
if (!fn)
throw new Error(`Unknown StringMembership operation: ${oper}`);
return fn(source, target);
}
;