Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding $find utility operator #25

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
*/
type DateExpression = ObjectExpression | Date | string;

type ArrayOfExpressions = Array<ObjectExpression>;

Check warning on line 40 in index.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

'ArrayOfExpressions' is defined but never used

type Timestamp = number;

Expand Down Expand Up @@ -2322,6 +2322,15 @@
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 @@ -2831,7 +2840,7 @@

// TODO
// * @category Safe Operators
const $ninSafe = (...args: any[]) => {

Check warning on line 2843 in index.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

'$ninSafe' is assigned a value but never used
if (args.length === 2) {
const [val, arr] = args;
return $in(val, $ifNull(arr, []));
Expand Down Expand Up @@ -3789,6 +3798,8 @@
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
Loading