UNPKG

mingo

Version:

MongoDB query language for in-memory objects

15 lines (14 loc) 488 B
import { computeValue } from "../../core/_internal"; import { compare, isNil } from "../../util"; import { $push } from "./push"; const $maxN = (collection, expr, options) => { const copts = options; const m = collection.length; const n = computeValue(copts?.local?.groupId, expr.n, null, copts); const arr = $push(collection, expr.input, options).filter((o) => !isNil(o)); arr.sort((a, b) => -1 * compare(a, b)); return m <= n ? arr : arr.slice(0, n); }; export { $maxN };