Skip to content

Commit

Permalink
Adding $reduce operator (#28)
Browse files Browse the repository at this point in the history
* Adding $reduce operator

* Tweak to argument order for $setField

* Adding test for $rand and $rank
  • Loading branch information
speedytwenty authored Sep 19, 2024
1 parent 3aab7b7 commit 7696a86
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
9 changes: 8 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2988,6 +2988,11 @@ const $rand = ne('$rand');
// TODO
const $rank = ne('$rank');

// TODO
const $reduce = (input: ObjectExpression, initialValue: Expression, inExpr: Expression) => ({
$reduce: { input, initialValue, in: inExpr },
});

type RoundOperator = {
$round: NumberExpression,
};
Expand Down Expand Up @@ -3108,7 +3113,7 @@ const $setDifference = at('$setDifference');
const $setEquals = pta('$setEquals');

// TODO
const $setField = (field: string, value: Expression, input: ObjectExpression) => ({
const $setField = (input: ObjectExpression, field: string, value: Expression) => ({
$setField: { field, input, value },
});

Expand Down Expand Up @@ -3956,6 +3961,8 @@ export = {
rank: $rank,
$redact,
redact: $redact,
$reduce,
reduce: $reduce,
$replaceRoot,
replaceRoot: $replaceRoot,
$replaceWith,
Expand Down
38 changes: 37 additions & 1 deletion pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,42 @@ describe('aggregation', () => {
expect($.radiansToDegrees('$value')).toEqual({ $radiansToDegrees: '$value' });
});
});
describe('$rand', () => {
it('exports expected vars', () => {
expect($.rand).toBeDefined();
expect($.$rand).toBeDefined();
expect($.rand).toStrictEqual($.$rand);
});
it('returns expected result', () => {
expect($.rand()).toEqual({ $rand: {} });
});
});
describe('$rank', () => {
it('exports expected vars', () => {
expect($.rank).toBeDefined();
expect($.$rank).toBeDefined();
expect($.rank).toStrictEqual($.$rank);
});
it('returns expected result', () => {
expect($.rank()).toEqual({ $rank: {} });
});
});
describe('$reduce', () => {
it('exports expected vars', () => {
expect($.reduce).toBeDefined();
expect($.$reduce).toBeDefined();
expect($.reduce).toStrictEqual($.$reduce);
});
it('returns expected result', () => {
expect($.reduce('$input', 'foo', '$bar')).toEqual({
$reduce: {
input: '$input',
initialValue: 'foo',
in: '$bar',
},
})
});
});
describe('$round', () => {
it('exports expected vars', () => {
expect($.round).toBeDefined();
Expand Down Expand Up @@ -1555,7 +1591,7 @@ describe('aggregation', () => {
expect($.setField).toStrictEqual($.$setField);
});
it('returns expected result', () => {
expect($.setField('foo', '$value', '$doc')).toEqual({
expect($.setField('$doc', 'foo', '$value')).toEqual({
$setField: {
field: 'foo',
value: '$value',
Expand Down

0 comments on commit 7696a86

Please sign in to comment.