UNPKG

mingo

Version:

MongoDB query language for in-memory objects

20 lines (19 loc) 581 B
import { computeValue } from "../../../core/_internal"; import { assert, isArray, isNil, isNumber } from "../../../util"; import { errExpectArray } from "../_internal"; const $multiply = (obj, expr, options) => { assert(isArray(expr), "$multiply expects array"); const args = computeValue(obj, expr, null, options); const foe = options.failOnError; if (args.some(isNil)) return null; let res = 1; for (const n of args) { if (!isNumber(n)) return errExpectArray(foe, "$multiply", { type: "number" }); res *= n; } return res; }; export { $multiply };