Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
blond committed Jul 3, 2016
1 parent e156d4b commit f64a9b2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/series.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ function series(iterables) {
return {
[Symbol.iterator]() { return this; },
next() {
while (!iter && iterables.length) {
iter = takeIterator(iterables.shift());
}

if (!iter) {
return done();
}
Expand All @@ -27,7 +31,9 @@ function series(iterables) {
// If next iterator is empty (is ended) go to the next,
// until you get not empty iterator, or all iterators are ended.
while (next.done) {
iter = takeIterator(iterables.shift());
do {
iter = takeIterator(iterables.shift());
} while (!iter && iterables.length);

// If iterators are ended, then exit.
if (!iter) {
Expand Down
41 changes: 40 additions & 1 deletion test/series.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const test = require('ava');
const series = require('../lib/series');
const done = require('../lib/done');

const createIter = (vals) => (new Set(vals)).values();

test('should return empty iterator', t => {
const iter = series();

Expand Down Expand Up @@ -63,9 +65,46 @@ test('should concat equal iterators', t => {
});

test('should traverse the same iterator once', t => {
const values = (new Set([1, 2])).values();
const values = createIter([1, 2]);

// redundant
const iter = series(values, values);

t.deepEqual(Array.from(iter), [1, 2]);
});

test('nul 12', t => {
const iter = series(null, createIter([1, 2]));

t.deepEqual(Array.from(iter), [1, 2]);
});

test('12 null 34', t => {
const iter = series(createIter([1, 2]), null, createIter([3, 4]));

t.deepEqual(Array.from(iter), [1, 2, 3, 4]);
});

test('nulnul 12', t => {
const iter = series(null, null, createIter([1, 2]));

t.deepEqual(Array.from(iter), [1, 2]);
});

test('null 12 nulnul 34', t => {
const iter = series(null, createIter([1, 2]), null, null, createIter([3, 4]));

t.deepEqual(Array.from(iter), [1, 2, 3, 4]);
});

test('null empty nulnul 12', t => {
const iter = series(null, createIter(), null, null, createIter([1, 2]));

t.deepEqual(Array.from(iter), [1, 2]);
});

test('nulls', t => {
const iter = series(null, null, null);

t.deepEqual(Array.from(iter), []);
});

0 comments on commit f64a9b2

Please sign in to comment.