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