|
| 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