helene
Version: 
Real-time Web Apps for Node.js
63 lines • 1.87 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogicalOperators = void 0;
const model_1 = require("./model");
const isBoolean_1 = __importDefault(require("lodash/isBoolean"));
const isFunction_1 = __importDefault(require("lodash/isFunction"));
/**
 * Match any of the subqueries
 */
exports.LogicalOperators = {
    $or: function (obj, query) {
        let i;
        if (!Array.isArray(query)) {
            throw new Error('$or operator used without an array');
        }
        for (i = 0; i < query.length; i += 1) {
            if ((0, model_1.match)(obj, query[i])) {
                return true;
            }
        }
        return false;
    },
    /**
     * Match all the subqueries
     * @param obj
     * @param query
     */
    $and: function (obj, query) {
        let i;
        if (!Array.isArray(query)) {
            throw new Error('$and operator used without an array');
        }
        for (i = 0; i < query.length; i += 1) {
            if (!(0, model_1.match)(obj, query[i])) {
                return false;
            }
        }
        return true;
    },
    /**
     * Match none of the subqueries
     */
    $not: function (obj, query) {
        return !(0, model_1.match)(obj, query);
    },
    /**
     * Match if the function returns true
     */
    $where: function (obj, fn) {
        if (!(0, isFunction_1.default)(fn)) {
            throw new Error('$where operator used without a function');
        }
        const result = fn.call(obj);
        if (!(0, isBoolean_1.default)(result)) {
            throw new Error('$where function must return boolean');
        }
        return result;
    },
};
//# sourceMappingURL=logical-operators.js.map