Skip to content

Commit

Permalink
feat: make collectWith return the provided object
Browse files Browse the repository at this point in the history
This will make it consistent with other 'collection' methods.

ORIGINAL-PR-URL: metarhia/common#278
  • Loading branch information
lundibundi committed Sep 13, 2020
1 parent 43bb512 commit 49cbdff
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/iterator.js
Expand Up @@ -111,6 +111,7 @@ class Iterator {

collectWith(obj, collector) {
this.forEach(element => collector(obj, element));
return obj;
}

toArray() {
Expand Down
8 changes: 8 additions & 0 deletions test/iterator.js
Expand Up @@ -440,6 +440,14 @@ metatests.test(
}
);

metatests.test('Iterator.collectWith must return provided object', test => {
const set = iter(array).collectWith(new Set(), (obj, element) => {
obj.add(element);
});
test.strictSame([...set.values()], array);
test.end();
});

metatests.testSync('Iterator.enumerate must return tuples', test => {
let i = 0;
iter(array)
Expand Down

0 comments on commit 49cbdff

Please sign in to comment.