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

Don't die when user adds enumerable properties to Array.prototype #1

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/index.js
Expand Up @@ -90,6 +90,7 @@ internals.Topo.prototype._sort = function () {

var expandedGroups = [];
for (var groupIndex in graph[node]) {
if(!graph[node].propertyIsEnumerable(groupIndex)) break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the standard way of doing this is like graph[node].hasOwnproperty(groupIndex).Does that have the same effect?

Also, break will terminate the loop early. You need continue. Finally, I think the normal approach for for ... in loop is

if (object.hasOwnproperty(key) {...}

var group = graph[node][groupIndex];
groups[group] = groups[group] || [];
groups[group].forEach(function (d) {
Expand All @@ -104,9 +105,11 @@ internals.Topo.prototype._sort = function () {

var afterNodes = Object.keys(graphAfters);
for (var n in afterNodes) {
if(!afterNodes.propertyIsEnumerable(n)) break;
var group = afterNodes[n];

for (var itemIndex in groups[group]) {
if(!groups[group].propertyIsEnumerable(itemIndex)) break;
var node = groups[group][itemIndex];
graph[node] = graph[node].concat(graphAfters[group]);
}
Expand All @@ -117,6 +120,7 @@ internals.Topo.prototype._sort = function () {
var ancestors = {};
var graphNodes = Object.keys(graph);
for (var i in graphNodes) {
if(!graphNodes.propertyIsEnumerable(i)) break;
var node = graphNodes[i];
var children = graph[node];

Expand Down
22 changes: 22 additions & 0 deletions test/index.js
Expand Up @@ -112,4 +112,26 @@ describe('Topo', function () {

done();
});

it('allows a user to add enumerable properties to the Array object', function (done) {

var scenario = [
{ id: '0', before: 'a' },
{ id: '1', after: 'f', group: 'a' },
{ id: '2', before: 'a' },
{ id: '3', before: ['b', 'c'], group: 'a' },
{ id: '4', after: 'c', group: 'b' },
{ id: '5', group: 'c' },
{ id: '6', group: 'd' },
{ id: '7', group: 'e' },
{ id: '8', before: 'd' },
{ id: '9', after: 'c', group: 'a' }
];

Array.prototype.bogusProperty = 'gotcha!';
expect(testDeps(scenario)).to.equal('0213547869');
done();
delete Array.prototype.bogusProperty;

});
});