mingo
Version:
MongoDB query language for in-memory objects
183 lines (182 loc) • 4.47 kB
JavaScript
import { ComputeOptions, computeValue } from "../core/_internal";
import { Query } from "../query";
import {
compare as mingoCmp,
ensureArray,
flatten,
intersection,
isArray,
isBoolean,
isDate,
isEmpty,
isEqual,
isNil,
isNumber,
isObject,
isOperator,
isRegExp,
isString,
resolve,
truthy,
typeOf
} from "../util/_internal";
function processQuery(selector, value, options, predicate) {
const opts = { unwrapArray: true };
const depth = Math.max(1, selector.split(".").length - 1);
const copts = ComputeOptions.init(options).update({ depth });
return (o) => {
const lhs = resolve(o, selector, opts);
return predicate(lhs, value, copts);
};
}
function processExpression(obj, expr, options, predicate) {
const [lhs, rhs] = computeValue(obj, expr, null, options);
return predicate(lhs, rhs, options);
}
function $eq(a, b, options) {
if (isEqual(a, b)) return true;
if (isNil(a) && isNil(b)) return true;
if (isArray(a)) {
const depth = (options instanceof ComputeOptions ? options?.local?.depth : 1) ?? 1;
return a.some((v) => isEqual(v, b)) || flatten(a, depth).some((v) => isEqual(v, b));
}
return false;
}
function $ne(a, b, options) {
return !$eq(a, b, options);
}
function $in(a, b, options) {
if (isNil(a)) return b.some((v) => v === null);
return intersection([ensureArray(a), b]).length > 0;
}
function $nin(a, b, options) {
return !$in(a, b, options);
}
function $lt(a, b, _options) {
return compare(a, b, (x, y) => mingoCmp(x, y) < 0);
}
function $lte(a, b, _options) {
return compare(a, b, (x, y) => mingoCmp(x, y) <= 0);
}
function $gt(a, b, _options) {
return compare(a, b, (x, y) => mingoCmp(x, y) > 0);
}
function $gte(a, b, _options) {
return compare(a, b, (x, y) => mingoCmp(x, y) >= 0);
}
function $mod(a, b, _options) {
return ensureArray(a).some(
((x) => b.length === 2 && x % b[0] === b[1])
);
}
function $regex(a, b, options) {
const lhs = ensureArray(a);
const match = (x) => isString(x) && truthy(b.exec(x), options?.useStrictMode);
return lhs.some(match) || flatten(lhs, 1).some(match);
}
function $all(values, queries, options) {
if (!isArray(values) || !isArray(queries) || !values.length || !queries.length) {
return false;
}
let matched = true;
for (const query of queries) {
if (!matched) break;
if (isObject(query) && Object.keys(query).includes("$elemMatch")) {
matched = $elemMatch(values, query["$elemMatch"], options);
} else if (isRegExp(query)) {
matched = values.some((s) => typeof s === "string" && query.test(s));
} else {
matched = values.some((v) => isEqual(query, v));
}
}
return matched;
}
function $size(a, b, _options) {
return Array.isArray(a) && a.length === b;
}
function isNonBooleanOperator(name) {
return isOperator(name) && ["$and", "$or", "$nor"].indexOf(name) === -1;
}
function $elemMatch(a, b, options) {
if (isArray(a) && !isEmpty(a)) {
let format = (x) => x;
let criteria = b;
if (Object.keys(b).every(isNonBooleanOperator)) {
criteria = { temp: b };
format = (x) => ({ temp: x });
}
const query = new Query(criteria, options);
for (let i = 0, len = a.length; i < len; i++) {
if (query.test(format(a[i]))) {
return true;
}
}
}
return false;
}
const isNull = (a) => a === null;
const compareFuncs = {
array: isArray,
boolean: isBoolean,
bool: isBoolean,
date: isDate,
number: isNumber,
int: isNumber,
long: isNumber,
double: isNumber,
decimal: isNumber,
null: isNull,
object: isObject,
regexp: isRegExp,
regex: isRegExp,
string: isString,
// added for completeness
undefined: isNil,
// deprecated
// Mongo identifiers
1: isNumber,
//double
2: isString,
3: isObject,
4: isArray,
6: isNil,
// deprecated
8: isBoolean,
9: isDate,
10: isNull,
11: isRegExp,
16: isNumber,
//int
18: isNumber,
//long
19: isNumber
//decimal
};
function compareType(a, b, _) {
const f = compareFuncs[b];
return f ? f(a) : false;
}
function $type(a, b, options) {
return isArray(b) ? b.findIndex((t) => compareType(a, t, options)) >= 0 : compareType(a, b, options);
}
function compare(a, b, f) {
return ensureArray(a).some((x) => typeOf(x) === typeOf(b) && f(x, b));
}
export {
$all,
$elemMatch,
$eq,
$gt,
$gte,
$in,
$lt,
$lte,
$mod,
$ne,
$nin,
$regex,
$size,
$type,
processExpression,
processQuery
};