Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export function flatten(input) {
- let output = [];
- input.forEach((item) => {
- if (Array.isArray(item)) {
- output = output.concat(flatten(item));
- } else {
- output = output.concat([item]);
- }
- });
- return output;
- }
- describe('Array flatten', () => {
- it('should flatten an array no matter the type and/or depth of the entries', () => {
- expect(flatten([1, 2, 3, 4])).toEqual([1, 2, 3, 4]);
- expect(flatten([1, 2, [3], 4])).toEqual([1, 2, 3, 4]);
- expect(flatten([1, 2, [3], [4, 5, 6]])).toEqual([1, 2, 3, 4, 5, 6]);
- expect(flatten([1, 2, [3], [4, [5, 6, [7, 8, [9]]], 10]])).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
- expect(flatten([1, 2, [3, 'str'], { prop: 'value' }])).toEqual([1, 2, 3, 'str', { prop: 'value' }]);
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement