map: Return a buffered channel if appropriate.

This commit is contained in:
David Braun 2018-02-20 14:20:34 -05:00
parent e8c27056de
commit 4a4a2db7ed
2 changed files with 10 additions and 7 deletions

View file

@ -178,7 +178,8 @@ const Channel = function(length = 0) {
}, },
map: (callbackfn, thisArg) => { map: (callbackfn, thisArg) => {
const output = Channel(); const output = Channel(length);
(async () => { (async () => {
await readOnly.forEach(value => { await readOnly.forEach(value => {
output.push(callbackfn.call(thisArg, value)); output.push(callbackfn.call(thisArg, value));

View file

@ -269,12 +269,14 @@ describe(`Channel object`, function() {
}); });
it(`map`, async function() { it(`map`, async function() {
assert.deepEqual( const channel = Channel(3);
await Channel.of(`a`, `b`, `c`) await channel.push(`a`);
.map(value => value.toUpperCase()) await channel.push(`b`);
.values(), await channel.push(`c`);
[`A`, `B`, `C`] await channel.close();
); const mapped = channel.map(value => value.toUpperCase());
assert.equal(mapped.length, channel.length);
assert.deepEqual(await mapped.values(), [`A`, `B`, `C`]);
}); });
describe(`push`, function() { describe(`push`, function() {