Skip to content

Commit

Permalink
Merge pull request #11399 from Uzlopak/why-not-ObjectAssign
Browse files Browse the repository at this point in the history
PoC for Object assign
  • Loading branch information
vkarpov15 committed Feb 15, 2022
2 parents 7c46428 + 09c6f8e commit 96c067e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
9 changes: 2 additions & 7 deletions lib/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,10 @@ Aggregate.prototype.append = function() {
* @api public
*/
Aggregate.prototype.addFields = function(arg) {
const fields = {};
if (typeof arg === 'object' && !Array.isArray(arg)) {
Object.keys(arg).forEach(function(field) {
fields[field] = arg[field];
});
} else {
if (typeof arg !== 'object' || arg === null || Array.isArray(arg)) {
throw new Error('Invalid addFields() argument. Must be an object');
}
return this.append({ $addFields: fields });
return this.append({ $addFields: Object.assign({}, arg) });
};

/**
Expand Down
12 changes: 12 additions & 0 deletions test/aggregate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,18 @@ describe('aggregate: ', function() {
});

describe('addFields', function() {
it('should throw if passed a non object', function () {
const aggregate = new Aggregate();
assert.throws(() => {aggregate.addFields("invalid")}, /Invalid addFields\(\) argument\. Must be an object/)
});
it('should throw if passed null', function () {
const aggregate = new Aggregate();
assert.throws(() => {aggregate.addFields(null)}, /Invalid addFields\(\) argument\. Must be an object/)
});
it('should throw if passed an Array', function () {
const aggregate = new Aggregate();
assert.throws(() => {aggregate.addFields([])}, /Invalid addFields\(\) argument\. Must be an object/)
});
it('(object)', function() {
const aggregate = new Aggregate();

Expand Down

0 comments on commit 96c067e

Please sign in to comment.