Skip to content

Commit 371bad0

Browse files
authored
Merge pull request #6
Enhance flat to support recursive flattening with depth -1
2 parents 6708d4a + 7615859 commit 371bad0

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

src/iterables/select-many.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function flatMapIterator<TValue, TResult>(input: Iterable<TValue>, mapper
2929
export function* flatGenerator<TValue>(input: Iterable<TValue>, depth: number, level: number): Generator<TValue> {
3030
for (const item of input) {
3131
if (Array.isArray(item)) {
32-
if (level >= depth) {
32+
if (depth > 0 && level >= depth) {
3333
yield item;
3434
} else {
3535
level++;

test/unit/select-many.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ describe('select many tests', () => {
110110
expect(output).toStrictEqual([1, 2, 3, 4, 5, 6, 'a', 'b', 'c', 'd', 'e']);
111111
});
112112

113+
it('should flat sequence recursive: depth -1', () => {
114+
const input = [
115+
[1, 2, 3, 4, [5, 6, [7, [8, [9, [10, 11, [12]]]]]]],
116+
'e'
117+
];
118+
const output = from(input).flat(-1).toArray();
119+
expect(output).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 'e']);
120+
});
121+
113122
it('should flatMap sequence', () => {
114123
const input = [
115124
[1, 2, 3, 4, [5, 6]],

0 commit comments

Comments
 (0)