From ed06cb080eb8703dc6de96f368e5516fe8be378a Mon Sep 17 00:00:00 2001 From: David <> Date: Mon, 8 Feb 2021 16:29:32 -0600 Subject: [PATCH] Rewrite `filter`. --- lib/index.js | 19 ++++--------------- test/index.js | 2 +- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/lib/index.js b/lib/index.js index 0353f48..63ae469 100644 --- a/lib/index.js +++ b/lib/index.js @@ -140,21 +140,10 @@ const Channel = function(length = 0) { } }, - filter: (callbackfn, thisArg) => { - const output = Channel(); - - (async () => { - await readOnly.forEach(async (value) => { - if (await callbackfn.call(thisArg, value)) { - await output.push(value); - } - }); - - await output.close(); - })(); - - return output; - }, + filter: (callbackfn, thisArg) => + readOnly.flatMap((value) => + callbackfn.call(thisArg, value) ? Channel.of(value) : Channel.of() + ), flat: (depth) => readOnly.flatMap((value) => diff --git a/test/index.js b/test/index.js index 7f84cd0..a160726 100644 --- a/test/index.js +++ b/test/index.js @@ -250,7 +250,7 @@ describe(`Channel object`, function() { it(`filter`, async function() { assert.deepEqual( await Channel.of(0, 1, 2, 3, 4, 5) - .filter(async (value) => value % 2 !== 0) + .filter((value) => value % 2 !== 0) .values(), [1, 3, 5] );