UNPKG

mingo

Version:

MongoDB query language for in-memory objects

18 lines (17 loc) 560 B
import { evalExpr } from "../../core/_internal"; import { isInteger } from "../../util"; import { errExpectInteger } from "../expression/_internal"; import { $push } from "./push"; const $lastN = (coll, expr, options) => { const copts = options; const m = coll.length; const n = evalExpr(copts?.local?.groupId, expr.n, copts); const foe = options.failOnError; if (!isInteger(n) || n < 1) { return errExpectInteger(foe, "$lastN 'n'", { min: 1 }); } return $push(m <= n ? coll : coll.slice(m - n), expr.input, options); }; export { $lastN };