helene
Version:
Real-time Web Apps for Node.js
94 lines • 2.75 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ComparisonFunctions = void 0;
const model_1 = require("./model");
const isRegExp_1 = __importDefault(require("lodash/isRegExp"));
exports.ComparisonFunctions = {
/**
* Arithmetic and comparison operators
*/
$lt: function (a, b) {
return (0, model_1.areComparable)(a, b) && a < b;
},
$lte: function (a, b) {
return (0, model_1.areComparable)(a, b) && a <= b;
},
$gt: function (a, b) {
return (0, model_1.areComparable)(a, b) && a > b;
},
$gte: function (a, b) {
return (0, model_1.areComparable)(a, b) && a >= b;
},
$ne: function (a, b) {
if (a === undefined) {
return true;
}
return !(0, model_1.areThingsEqual)(a, b);
},
$in: function (a, b) {
let i;
if (!Array.isArray(b)) {
throw new Error('$in operator called with a non-array');
}
for (i = 0; i < b.length; i += 1) {
if ((0, model_1.areThingsEqual)(a, b[i])) {
return true;
}
}
return false;
},
$nin: function (a, b) {
if (!Array.isArray(b)) {
throw new Error('$nin operator called with a non-array');
}
return !exports.ComparisonFunctions.$in(a, b);
},
$regex: function (a, b) {
if (!(0, isRegExp_1.default)(b)) {
throw new Error('$regex operator called with non regular expression');
}
if (typeof a !== 'string') {
return false;
}
else {
return b.test(a);
}
},
$exists: function (value, exists) {
exists = exists || exists === '';
if (value === undefined) {
return !exists;
}
else {
return exists;
}
},
$size: function (obj, value) {
if (!Array.isArray(obj)) {
return false;
}
if (value % 1 !== 0) {
throw new Error('$size operator called without an integer');
}
return obj.length == value;
},
$elemMatch: function (obj, value) {
if (!Array.isArray(obj)) {
return false;
}
let i = obj.length;
let result = false;
while (i--) {
if ((0, model_1.match)(obj[i], value)) {
// If match for array element, return true
result = true;
break;
}
}
return result;
},
};
//# sourceMappingURL=comparison-functions.js.map