UNPKG

mingo

Version:

MongoDB query language for in-memory objects

50 lines (49 loc) 1.32 kB
import { ComputeOptions, evalExpr } from "../../core/_internal"; import { has, isArray, isNil, isObject } from "../../util"; function $redact(coll, expr, options) { const copts = ComputeOptions.init(options); return coll.map( (root) => redact(root, expr, copts.update({ root })) ); } function redact(obj, expr, options) { const action = evalExpr(obj, expr, options); switch (action) { case "$$KEEP": return obj; case "$$PRUNE": return void 0; case "$$DESCEND": { if (!has(expr, "$cond")) return obj; const output = {}; for (const key of Object.keys(obj)) { const value = obj[key]; if (isArray(value)) { const res = new Array(); for (let elem of value) { if (isObject(elem)) { elem = redact(elem, expr, options.update({ root: elem })); } if (!isNil(elem)) res.push(elem); } output[key] = res; } else if (isObject(value)) { const res = redact( value, expr, options.update({ root: value }) ); if (!isNil(res)) output[key] = res; } else { output[key] = value; } } return output; } default: return action; } } export { $redact };