mingo
Version:
MongoDB query language for in-memory objects
20 lines (19 loc) • 742 B
JavaScript
import { isArray, resolve, resolveGraph } from "../../../util/_internal";
const $exists = (selector, value, _options) => {
const nested = selector.includes(".");
const b = !!value;
if (!nested || selector.match(/\.\d+$/)) {
const opts2 = { pathArray: selector.split(".") };
return (o) => resolve(o, selector, opts2) !== void 0 === b;
}
const parentSelector = selector.substring(0, selector.lastIndexOf("."));
const opts = { pathArray: parentSelector.split("."), preserveIndex: true };
return (o) => {
const path = resolveGraph(o, selector, opts);
const val = resolve(path, parentSelector, opts);
return isArray(val) ? val.some((v) => v !== void 0) === b : val !== void 0 === b;
};
};
export {
$exists
};