From 4a4a2db7ed3077e80f2e294396b2f5677ac434dd Mon Sep 17 00:00:00 2001 From: David Braun Date: Tue, 20 Feb 2018 14:20:34 -0500 Subject: [PATCH] map: Return a buffered channel if appropriate. --- lib/index.js | 3 ++- test/index.js | 14 ++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/index.js b/lib/index.js index 1eafee3..96c5a8f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -178,7 +178,8 @@ const Channel = function(length = 0) { }, map: (callbackfn, thisArg) => { - const output = Channel(); + const output = Channel(length); + (async () => { await readOnly.forEach(value => { output.push(callbackfn.call(thisArg, value)); diff --git a/test/index.js b/test/index.js index c1af825..63693e4 100644 --- a/test/index.js +++ b/test/index.js @@ -269,12 +269,14 @@ describe(`Channel object`, function() { }); it(`map`, async function() { - assert.deepEqual( - await Channel.of(`a`, `b`, `c`) - .map(value => value.toUpperCase()) - .values(), - [`A`, `B`, `C`] - ); + const channel = Channel(3); + await channel.push(`a`); + await channel.push(`b`); + await channel.push(`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() {