iter-tools-es
Version:
The iterable toolbox
32 lines (27 loc) • 864 B
JavaScript
const {
asyncIterableCurry,
asyncIsLoopable
} = require('../../internal/async-iterable.js');
const {
validateArgs
} = require('./internal/validate-args.js');
const defaultShouldFlat = value => typeof value !== 'string' && asyncIsLoopable(value);
async function* asyncFlatInternal(shouldFlat, depth, currentDepth, source) {
for await (const value of source) {
if (currentDepth < depth && (await shouldFlat(value))) {
yield* asyncFlatInternal(shouldFlat, depth, currentDepth + 1, value);
} else {
yield value;
}
}
}
function __asyncFlat(source, depth = 1, shouldFlat = defaultShouldFlat) {
return asyncFlatInternal(shouldFlat, depth, 0, source);
}
exports.__asyncFlat = __asyncFlat;
const asyncFlat = /*#__PURE__*/asyncIterableCurry(__asyncFlat, {
minArgs: 0,
maxArgs: 2,
validateArgs
});
exports.asyncFlat = asyncFlat;