-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement $firstN and $lastN array expression operators.
- Loading branch information
Showing
7 changed files
with
100 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/firstN-array-element/#mongodb-expression-exp.-firstN | ||
|
||
import { computeValue, Options } from "../../../core"; | ||
import { AnyVal, RawObject } from "../../../types"; | ||
import { assert, isArray, isNil } from "../../../util"; | ||
import { $firstN as __firstN } from "../../accumulator/firstN"; | ||
|
||
interface InputExpr { | ||
n: AnyVal; | ||
input: AnyVal; | ||
} | ||
|
||
/** | ||
* Returns a specified number of elements from the beginning of an array. | ||
* | ||
* @param {Object} obj | ||
* @param {*} expr | ||
* @return {*} | ||
*/ | ||
export function $firstN( | ||
obj: RawObject, | ||
expr: InputExpr, | ||
options?: Options | ||
): AnyVal { | ||
// first try the accumulator if input is an array. | ||
if (obj instanceof Array) return __firstN(obj, expr, options); | ||
const { input, n } = computeValue(obj, expr, null, options) as InputExpr; | ||
if (isNil(input)) return null; | ||
assert(isArray(input), "Must resolve to an array/null or missing"); | ||
return __firstN(input as RawObject[], { n, input: "$$this" }, options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// https://www.mongodb.com/docs/manual/reference/operator/aggregation/lastN-array-element/#mongodb-expression-exp.-lastN | ||
|
||
import { computeValue, Options } from "../../../core"; | ||
import { AnyVal, RawObject } from "../../../types"; | ||
import { assert, isArray, isNil } from "../../../util"; | ||
import { $lastN as __lastN } from "../../accumulator/lastN"; | ||
|
||
interface InputExpr { | ||
n: AnyVal; | ||
input: AnyVal; | ||
} | ||
|
||
/** | ||
* Returns a specified number of elements from the end of an array. | ||
* | ||
* @param {Object} obj | ||
* @param {*} expr | ||
* @return {*} | ||
*/ | ||
export function $lastN( | ||
obj: RawObject, | ||
expr: InputExpr, | ||
options?: Options | ||
): AnyVal { | ||
// first try the accumulator if input is an array. | ||
if (obj instanceof Array) return __lastN(obj, expr, options); | ||
const { input, n } = computeValue(obj, expr, null, options) as InputExpr; | ||
if (isNil(input)) return null; | ||
assert(isArray(input), "Must resolve to an array/null or missing"); | ||
return __lastN(input as RawObject[], { n, input: "$$this" }, options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters