t.test('stream factory: concat', async t => { const s = stream(counterGenerator()) .concat(counterGenerator(), 666, [1, 2], counterGenerator()); t.equal(Object.getPrototypeOf(s), proto, 'should have the stream prototype'); const result = []; for await (const item of s) { result.push(item); } t.deepEqual(result, [0, 1, 2, 0, 1, 2, 666, [1, 2], 0, 1, 2], 'should have seen the concatenated stream'); t.equal(result.length, 11, 'should have seen 11 iterations'); });
t.test('stream factory find', async t => { const s = stream(counterGenerator(10)); const value = await s.find(i => i > 5 && i % 2 === 0); t.equal(value, 6, 'should return the first odd integer greater than 5'); const notFound = await stream(counterGenerator()).find(i => i > 666); t.equal(notFound, undefined, 'should return undefined when not found'); });
t.test('stream factory findIndex', async t => { const s = stream(counterGenerator(10)).map(i => i * i); const value = await s.findIndex(i => i > 5 && i % 2 === 0); t.equal(value, 4, 'should return the index of the first odd squared integer greater than 5'); const notFound = await stream(counterGenerator()).findIndex(i => i > 666); t.equal(notFound, -1, 'should return undefined when not found'); });
test('utils test suite', t => { t.test('should curry function with two arguments', t => { const sum = curry((a, b) => a + b); const plusTwo = sum(2); t.equal(sum(3, 2), 5, 'should return result when called with two arguments'); t.equal(typeof plusTwo, 'function', 'should return a function when called with one argument'); t.equal(plusTwo(4), 6, 'should curry function if called with one argument'); }); })
t.test('from array', async t => { let iter = 0; const s = toAsync([0, 1, 2]); t.equal(typeof s[Symbol.asyncIterator], 'function', 'should be an async iterator'); for await (const i of s) { t.equal(i, iter, `should see the value ${iter}`); iter++; } t.equal(iter, 3, 'should have seen 3 iterations'); });
t.test('stream factory: slice (two arguments)', async t => { const s = stream(counterGenerator(5)) .slice(2, 3); t.equal(Object.getPrototypeOf(s), proto, 'should have the stream prototype'); const result = []; for await (const item of s) { result.push(item); } t.deepEqual(result, [2], 'should have seen starting from third item ending to the third'); t.equal(result.length, 1, 'should have seen 1 iteration'); });
t.test('stream factory: filterMap', async t => { const s = stream([counterGenerator(), counterGenerator(5)]) .flatMap(i => i * i); t.equal(Object.getPrototypeOf(s), proto, 'should have the stream prototype'); const result = []; for await (const item of s) { result.push(item); } t.deepEqual(result, [0, 1, 4, 0, 1, 4, 9, 16], 'should have seen the flattened squared numbers'); t.equal(result.length, 8, 'should have seen 8 iterations'); });
t.test('stream factory every', async t => { const hasEvery = await stream(counterGenerator()).every(i => i < 5); t.equal(hasEvery, true, 'every single item is lower than 5'); const hasNotEvery = await stream(counterGenerator()).every(i => i % 2 === 0); t.equal(hasNotEvery, false, 'not every item is odd'); });
t.test('stream factory: map', async t => { const s = stream(counterGenerator()) .map(i => i * i); t.equal(Object.getPrototypeOf(s), proto, 'should have the stream prototype'); let i = 0; for await (const item of s) { t.equal(item, i * i, `should have seen ${i * i} the square of ${i}`); i++; } t.equal(i, 3, 'should have seen 3 iterations'); });
t.test('stream factory: filter', async t => { const s = stream(counterGenerator()) .filter(i => i % 2 === 0); t.equal(Object.getPrototypeOf(s), proto, 'should have the stream prototype'); const result = []; for await (const item of s) { result.push(item); } t.deepEqual(result, [0, 2], 'should have seen the odd numbers only'); t.equal(result.length, 2, 'should have seen 2 iterations'); });
t.test('stream factory: slice (one argument)', async t => { const s = stream(counterGenerator(5)) .slice(2); t.equal(Object.getPrototypeOf(s), proto, 'should have the stream prototype'); const result = []; for await (const item of s) { result.push(item); } t.deepEqual(result, [2, 3, 4], 'should have seen starting from third item to the end'); t.equal(result.length, 3, 'should have seen 3 iterations'); });