Rewrite flat
and flatMap
.
This commit is contained in:
parent
15f0fc2bdb
commit
44d833575b
1 changed files with 11 additions and 10 deletions
21
lib/index.js
21
lib/index.js
|
@ -170,17 +170,21 @@ const Channel = function(length = 0) {
|
|||
return output;
|
||||
},
|
||||
|
||||
flat: (depth) => {
|
||||
flat: (depth) =>
|
||||
readOnly.flatMap((value) =>
|
||||
Channel.isChannel(value)
|
||||
? depth > 1
|
||||
? value.flat(depth - 1)
|
||||
: value
|
||||
: Channel.of(value)
|
||||
),
|
||||
|
||||
flatMap: (mapperFunction, thisArg) => {
|
||||
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 mapperFunction.call(thisArg, value).forEach(output.push);
|
||||
});
|
||||
|
||||
await output.close();
|
||||
|
@ -189,9 +193,6 @@ const Channel = function(length = 0) {
|
|||
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