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.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.mapState = mapState;
const maps_js_1 = require("../../enums/maps.js");
/**
* Checks the state of a Map (empty or not) using the specified operation.
*
* @param source The Map to check.
* @param oper The state operation to perform (e.g. 'is_empty', 'is_not_empty').
* @returns True if the state check is valid according to the operator, otherwise false.
*
* @throws {Error} If the operation is not recognized.
*
* @example
* const m1 = new Map();
* const m2 = new Map([[1, 'a']]);
*
* mapState(m1, 'is_empty'); // true
* mapState(m2, 'is_not_empty'); // true
*
* @remarks
* Supported Operators:
* - **IS_EMPTY**: Map is empty
* - **IS_NOT_EMPTY**: Map is not empty
*/
function mapState(source, oper) {
const operators = {
[maps_js_1.MapStateEnum.IS_EMPTY]: (a) => a.size === 0,
[maps_js_1.MapStateEnum.IS_NOT_EMPTY]: (a) => a.size > 0,
};
const enumOper = typeof oper === 'string' ? oper : oper;
const fn = operators[enumOper];
if (!fn)
throw new Error(`Unknown MapState operation: ${oper}`);
return fn(source);
}