predictype
Version:
PredicType is a library of pre-built and tested predicates for TypeScript, covering various data types and operations.
61 lines (60 loc) • 2.19 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.setState = setState;
const sets_js_1 = require("../../enums/sets.js");
/**
* Checks the state of a set using the specified operation.
*
* @param source The set to check.
* @param oper The state operation to perform (e.g. 'is_empty', 'has_primitives').
* @returns True if the state check is valid according to the operator, otherwise false.
*
* @throws {Error} If the operation is not recognized.
*
* @example
* const a = new Set();
* const b = new Set([1]);
* const c = new Set([1, 'hello', { id: 1 }]);
*
* setState(a, 'is_empty'); // true
* setState(b, 'is_not_empty'); // true
* setState(c, 'has_primitives'); // true
* setState(c, 'has_objects'); // true
*
* @remarks
* Supported Operators:
* - **IS_EMPTY**: Set is empty
* - **IS_NOT_EMPTY**: Set is not empty
* - **HAS_PRIMITIVES**: Set contains primitive values (string, number, boolean, null, undefined, symbol, bigint)
* - **HAS_OBJECTS**: Set contains object values (objects, arrays, functions)
*/
function setState(source, oper) {
const operators = {
[sets_js_1.SetStateEnum.IS_EMPTY]: (a) => a.size === 0,
[sets_js_1.SetStateEnum.IS_NOT_EMPTY]: (a) => a.size > 0,
[sets_js_1.SetStateEnum.HAS_PRIMITIVES]: (a) => {
for (const value of a) {
const type = typeof value;
if (['string', 'number', 'boolean', 'symbol', 'bigint'].includes(type))
return true;
if (value === null || value === undefined)
return true;
}
return false;
},
[sets_js_1.SetStateEnum.HAS_OBJECTS]: (a) => {
for (const value of a) {
const type = typeof value;
if ((type === 'object' && value !== null) || type === 'function') {
return true;
}
}
return false;
},
};
const enumOper = typeof oper === 'string' ? oper : oper;
const fn = operators[enumOper];
if (!fn)
throw new Error(`Unknown SetState operation: ${oper}`);
return fn(source);
}
;