@klodianimeri/pipejs
Version:
Pipe functions that provide convenient and efficient ways to work with iterators.
15 lines (13 loc) • 540 B
text/typescript
import { Pipe } from "../pipe.js";
import { Yields } from "../util/index.js";
export function flat(depth?: number): Pipe {
depth = (typeof depth === 'number' && depth >= 1) ? depth : 1;
return () => {
return (result: IteratorResult<any>): IteratorResult<any> | Array<IteratorResult<any>> => {
if (result?.done || typeof result.value[Symbol.iterator] !== 'function') {
return result;
}
return Yields(Array.from(result.value).flat(depth));
};
}
}