Skip to content

Commit

Permalink
Adding $find utility operator (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
speedytwenty authored Sep 13, 2024
1 parent 3ac29a3 commit 890394d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2322,6 +2322,15 @@ const $filter = (
limit?: number,
) => new FilterOperator(inputExpr, asName, condExpr, limit);

type FindParams = [
ArrayExpression,
string,
Expression,
number?,
];

const $find = (...args: FindParams) => $arrayElemAt($filter(...args), 0);

type FirstOperator = {
$first: Expression,
};
Expand Down Expand Up @@ -3789,6 +3798,8 @@ export = {
facet: $facet,
$filter,
filter: $filter,
$find,
find: $find,
$first,
first: $first,
$floor,
Expand Down
18 changes: 18 additions & 0 deletions pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,24 @@ describe('aggregation', () => {
});
});
});
describe('$find', () => {
it('exports expected vars', () => {
expect($.find).toBeDefined();
expect($.$find).toBeDefined();
expect($.find).toStrictEqual($.$find);
});
it('returns expected result', () => {
expect($.find('$input', 'foo', '$cond')).toEqual({
$arrayElemAt: [{
$filter: {
input: '$input',
as: 'foo',
cond: '$cond',
},
}, 0],
});
});
});
describe('$first', () => {
it('exports expected vars', () => {
expect($.first).toBeDefined();
Expand Down

0 comments on commit 890394d

Please sign in to comment.