Skip to content

Commit f61a939

Browse files
committed
add flatten array util
1 parent 38c3ec9 commit f61a939

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

packages/utils/src/array.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export type NestedArray<T> = Array<NestedArray<T> | T>;
2+
3+
/** Flattens a multi-dimensional array */
4+
export function flatten<T>(input: NestedArray<T>): T[] {
5+
const result: T[] = [];
6+
7+
const flattenHelper = (input: NestedArray<T>): void => {
8+
input.forEach((el: T | NestedArray<T>) => {
9+
if (Array.isArray(el)) {
10+
flattenHelper(el as NestedArray<T>);
11+
} else {
12+
result.push(el as T);
13+
}
14+
});
15+
};
16+
17+
flattenHelper(input);
18+
return result;
19+
}

packages/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './aggregate-errors';
2+
export * from './array';
23
export * from './browser';
34
export * from './dsn';
45
export * from './error';

packages/utils/test/array.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import type { NestedArray } from '../src/array';
2+
import { flatten } from '../src/array';
3+
4+
describe('flatten', () => {
5+
it('should return the same array when input is a flat array', () => {
6+
const input = [1, 2, 3, 4];
7+
const expected = [1, 2, 3, 4];
8+
expect(flatten(input)).toEqual(expected);
9+
});
10+
11+
it('should flatten a nested array of numbers', () => {
12+
const input = [[1, 2, [3]], 4];
13+
const expected = [1, 2, 3, 4];
14+
expect(flatten(input)).toEqual(expected);
15+
});
16+
17+
it('should flatten a nested array of strings', () => {
18+
const input = [
19+
['Hello', 'World'],
20+
['How', 'Are', 'You'],
21+
];
22+
const expected = ['Hello', 'World', 'How', 'Are', 'You'];
23+
expect(flatten(input)).toEqual(expected);
24+
});
25+
26+
it('should flatten a nested array of objects', () => {
27+
const input: NestedArray<{ a: number; b?: number } | { b: number; a?: number }> = [
28+
[{ a: 1 }, { b: 2 }],
29+
[{ a: 3 }, { b: 4 }],
30+
];
31+
const expected = [{ a: 1 }, { b: 2 }, { a: 3 }, { b: 4 }];
32+
expect(flatten(input)).toEqual(expected);
33+
});
34+
35+
it('should flatten a mixed type array', () => {
36+
const input: NestedArray<string | { b: number }> = [['a', { b: 2 }, 'c'], 'd'];
37+
const expected = ['a', { b: 2 }, 'c', 'd'];
38+
expect(flatten(input)).toEqual(expected);
39+
});
40+
41+
it('should flatten a deeply nested array', () => {
42+
const input = [1, [2, [3, [4, [5]]]]];
43+
const expected = [1, 2, 3, 4, 5];
44+
expect(flatten(input)).toEqual(expected);
45+
});
46+
47+
it('should return an empty array when input is empty', () => {
48+
const input: any[] = [];
49+
const expected: any[] = [];
50+
expect(flatten(input)).toEqual(expected);
51+
});
52+
53+
it('should return the same array when input is a flat array', () => {
54+
const input = [1, 'a', { b: 2 }, 'c', 3];
55+
const expected = [1, 'a', { b: 2 }, 'c', 3];
56+
expect(flatten(input)).toEqual(expected);
57+
});
58+
});

0 commit comments

Comments
 (0)