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;
|
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();
|
const output = Channel();
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
await readOnly.forEach(async (value) => {
|
await readOnly.forEach(async (value) => {
|
||||||
if (Channel.isChannel(value)) {
|
await mapperFunction.call(thisArg, value).forEach(output.push);
|
||||||
const input = depth > 1 ? value.flat(depth - 1) : value;
|
|
||||||
await input.forEach(output.push);
|
|
||||||
} else {
|
|
||||||
await output.push(value);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
await output.close();
|
await output.close();
|
||||||
|
@ -189,9 +193,6 @@ const Channel = function(length = 0) {
|
||||||
return output;
|
return output;
|
||||||
},
|
},
|
||||||
|
|
||||||
flatMap: (mapperFunction, thisArg) =>
|
|
||||||
readOnly.map(mapperFunction, thisArg).flat(),
|
|
||||||
|
|
||||||
forEach: async (callbackfn, thisArg) => {
|
forEach: async (callbackfn, thisArg) => {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
const value = await readOnly.shift();
|
const value = await readOnly.shift();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue