Add flat
and flatMap
.
This commit is contained in:
parent
e9023771cc
commit
6b1c55cff3
5 changed files with 129 additions and 62 deletions
22
lib/index.js
22
lib/index.js
|
@ -172,6 +172,28 @@ const Channel = function(length = 0) {
|
|||
return output;
|
||||
},
|
||||
|
||||
flat: depth => {
|
||||
const output = Channel();
|
||||
|
||||
(async () => {
|
||||
await readOnly.forEach(async value => {
|
||||
if (Channel.isChannel(value)) {
|
||||
const input = depth > 1 ? value.flat(depth - 1) : value;
|
||||
await input.forEach(output.push);
|
||||
} else {
|
||||
await output.push(value);
|
||||
}
|
||||
});
|
||||
|
||||
await output.close();
|
||||
})();
|
||||
|
||||
return output;
|
||||
},
|
||||
|
||||
flatMap: (mapperFunction, thisArg) =>
|
||||
readOnly.map(mapperFunction, thisArg).flat(),
|
||||
|
||||
forEach: async (callbackfn, thisArg) => {
|
||||
for (;;) {
|
||||
const value = await readOnly.shift();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue