Skip to content
Open
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
8 changes: 7 additions & 1 deletion lib/groupByLimit.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ function groupByLimit(coll, limit, iteratee, callback) {
if (hasOwnProperty.call(result, key)) {
result[key].push(val);
} else {
result[key] = [val];
// Avoid invoking the inherited setter for a group named '__proto__'.
Object.defineProperty(result, key, {
value: [val],
writable: true,
enumerable: true,
configurable: true
});
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions test/groupBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,38 @@ var assert = require('assert');
describe('groupBy', function() {
this.timeout(250);

['groupBy', 'groupByLimit', 'groupBySeries'].forEach((method) => {
var values = ['__proto__', 'constructor', 'toString', '__proto__', 'hasOwnProperty'];

function checkGroups(result) {
expect(Object.getPrototypeOf(result)).to.equal(Object.prototype);
expect(Object.keys(result).sort()).to.eql([
'__proto__', 'constructor', 'hasOwnProperty', 'toString'
]);
expect(result.__proto__).to.eql(['__proto__', '__proto__']);
expect(result.constructor).to.eql(['constructor']);
expect(result.toString).to.eql(['toString']);
expect(result.hasOwnProperty).to.eql(['hasOwnProperty']);
}

it(`${method} groups values under prototype property names`, (done) => {
var args = [values, (value, next) => {
async.setImmediate(() => next(null, value));
}, (err, result) => {
checkGroups(result);
done(err);
}];
if (method === 'groupByLimit') args.splice(1, 0, 2);
async[method](...args);
});

it(`${method} resolves groups under prototype property names`, async () => {
var args = [values, async value => value];
if (method === 'groupByLimit') args.splice(1, 0, 2);
checkGroups(await async[method](...args));
});
});

function groupByIteratee(callOrder, val, next) {
setTimeout(() => {
callOrder.push(val);
Expand Down