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.55 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.objectInstanceRelation = objectInstanceRelation;
const objects_js_1 = require("../../enums/objects.js");
/**
* Checks instance or prototype relation between two values using the specified operation.
*
* @param source The value to check.
* @param oper The relation operation to perform (e.g. 'instance_of', 'prototype_of').
* @param target The target to compare against.
* @returns True if the relation check is valid according to the operator, otherwise false.
*
* @throws {Error} If the operation is not recognized.
*
* @remarks
* Supported Operators:
* - **INSTANCE_OF**: value instanceof target
* - **PROTOTYPE_OF**: value is prototype of target
*/
function objectInstanceRelation(source, oper, target) {
const operators = {
[objects_js_1.ObjectInstanceRelationEnum.INSTANCE_OF]: (v, t) => typeof t === 'function' && v instanceof t,
[objects_js_1.ObjectInstanceRelationEnum.PROTOTYPE_OF]: (v, t) => {
if (v == null || (typeof v !== 'object' && typeof v !== 'function'))
return false;
if (t == null || (typeof t !== 'object' && typeof t !== 'function'))
return false;
return Object.prototype.isPrototypeOf.call(v, t);
},
};
const enumOper = typeof oper === 'string' ? oper : oper;
const fn = operators[enumOper];
if (!fn)
throw new Error(`Unknown ObjectInstanceRelation operation: ${oper}`);
return fn(source, target);
}
;