Skip to content

Commit

Permalink
Merge pull request #162 from identity-com/CIV-2732_bug_fix
Browse files Browse the repository at this point in the history
BUG FIX: max/min and boolean params
  • Loading branch information
dmelosantos authored Feb 23, 2021
2 parents 984faaf + d0c3513 commit 6acb8df
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
24 changes: 24 additions & 0 deletions __test__/services/AggregationService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,36 @@ describe('Aggregation Service', () => {
expect(result).toStrictEqual([{ k: 'a' }]);
});

it('should return the first elements using boolean', () => {
const collection = [{ k: 'a' }, { k: 'b' }, { k: 'c' }];
const result = aggregate(collection, [{ $first: true }]);
expect(result).toStrictEqual([{ k: 'a' }]);
});

it('should return the last elements only', () => {
const collection = [{ k: 'a' }, { k: 'b' }, { k: 'c' }];
const result = aggregate(collection, [{ $last: 'true' }]);
expect(result).toStrictEqual([{ k: 'c' }]);
});

it('should return the last elements using boolean', () => {
const collection = [{ k: 'a' }, { k: 'b' }, { k: 'c' }];
const result = aggregate(collection, [{ $last: true }]);
expect(result).toStrictEqual([{ k: 'c' }]);
});

it('should return the max elements only', () => {
const collection = [{ k: 'a' }, { k: 'b' }, { k: 'c' }];
const result = aggregate(collection, [{ $max: 'k' }]);
expect(result).toStrictEqual([{ k: 'c' }]);
});

it('should return the min elements only', () => {
const collection = [{ k: 'a' }, { k: 'b' }, { k: 'c' }];
const result = aggregate(collection, [{ $min: 'k' }]);
expect(result).toStrictEqual([{ k: 'a' }]);
});

it('should return in ascending order ', () => {
const collection = [{ k: 'b' }, { k: 'a' }, { k: 'c' }];
const result = aggregate(collection, [{ $sort: { k: 'ASC' } }]);
Expand Down
6 changes: 3 additions & 3 deletions src/AggregationHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const validateEmptyParametersOperators = (parameters) => {
return true;
};
const validateNotEmptyParametersOperators = (parameters) => {
if (_.isEmpty(parameters)) { throw new Error('parameters should not be empty'); }
if (!parameters && _.isEmpty(parameters)) { throw new Error('parameters should not be empty'); }
return true;
};
const validatePathParametersOperators = (parameters) => {
Expand Down Expand Up @@ -34,9 +34,9 @@ const AGGREGATION_OPERATORS_MAP = {
$limit: (collection, params) => (validateNumberParametersOperators(params)
? [...(_.slice(collection, 0, params))] : null),
$min: (collection, params) => (validatePathParametersOperators(params)
? [...(_.minBy(collection, params))] : null),
? [(_.minBy(collection, params))] : null),
$max: (collection, params) => (validatePathParametersOperators(params)
? [...(_.maxBy(collection, params))] : null),
? [(_.maxBy(collection, params))] : null),
$first: (collection, params) => (validateNotEmptyParametersOperators(params)
? [_.first(collection)] : null),
$last: (collection, params) => (validateNotEmptyParametersOperators(params)
Expand Down

0 comments on commit 6acb8df

Please sign in to comment.