mingo
Version:
MongoDB query language for in-memory objects
21 lines (20 loc) • 620 B
JavaScript
import { computeValue } from "../../core/_internal";
import { isInteger } from "../../util";
import { errExpectNumber, INT_OPTS } from "../expression/_internal";
import { $push } from "./push";
const $firstN = (collection, expr, options) => {
const foe = options.failOnError;
const copts = options;
const m = collection.length;
const n = computeValue(copts?.local?.groupId, expr.n, null, copts);
if (!isInteger(n) || n < 1)
return errExpectNumber(foe, "$firstN 'n'", INT_OPTS.pos);
return $push(
m <= n ? collection : collection.slice(0, n),
expr.input,
options
);
};
export {
$firstN
};