Skip to content

Commit

Permalink
Collections: add Symbol.iterator
Browse files Browse the repository at this point in the history
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information
rwaldron committed May 2, 2017
1 parent 291a1db commit be3e8bd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/mixins/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ function Collection(numsOrObjects) {
}
}

if (Symbol && Symbol.iterator) {
Collection.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];

This comment has been minimized.

Copy link
@islemaster

islemaster May 2, 2017

Contributor

Heads-up: This is causing v0.10.11 to fail tests on node v0.10.x because Symbol support wasn't added until node 0.12. I think you need to use the check typeof Symbol !== 'undefined' to safely check whether Symbol exists in the global scope.

This comment has been minimized.

Copy link
@rwaldron

rwaldron May 3, 2017

Author Owner

Yep... whoops. I will get this fixed asap

This comment has been minimized.

Copy link
@rwaldron

rwaldron May 3, 2017

Author Owner

Fixed and released in 0.10.12

}

Collection.prototype.add = function() {
var length = this.length;
var aLen = arguments.length;
Expand Down Expand Up @@ -230,6 +234,10 @@ util.inherits(Collection.Emitter, Collection);

Object.assign(Collection.Emitter.prototype, Emitter.prototype);

if (Symbol && Symbol.iterator) {
Collection.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
}

Collection.Emitter.prototype.add = function() {
var inputs = Array.from(arguments);

Expand Down
17 changes: 17 additions & 0 deletions test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ exports["Collection"] = {
Collection.purge();
done();
},

"Symbol.iterator": function(test) {
if (Symbol && Symbol.iterator) {
test.expect(1);
test.equal(Collection.prototype[Symbol.iterator], Array.prototype[Symbol.iterator]);
}
test.done();
},

nested: function(test) {
test.expect(9);

Expand Down Expand Up @@ -322,6 +331,14 @@ exports["Collection.Emitter"] = {
done();
},

"Symbol.iterator": function(test) {
if (Symbol && Symbol.iterator) {
test.expect(1);
test.equal(Collection.prototype[Symbol.iterator], Array.prototype[Symbol.iterator]);
}
test.done();
},

nested: function(test) {
test.expect(9);

Expand Down

0 comments on commit be3e8bd

Please sign in to comment.