Last active
October 18, 2019 07:20
-
-
Save paschalidi/f1fecddc19e9fb658145c619b6956bab to your computer and use it in GitHub Desktop.
A simple function that recursively flattens a given `array`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { flatten } from "./flatten"; | |
describe("flatten method", function() { | |
it("should flatten deeply arrays", function() { | |
const array = [1, [2, [3, [4]], 5]]; | |
const flatArray = flatten(array); | |
const expected = [1, 2, 3, 4, 5]; | |
expect(flatArray).toEqual(expected); | |
}); | |
it("should work with empty arrays", function() { | |
var array = [[], [[]], [[], [[[]]]]]; | |
const flatArray = flatten(array); | |
const expected = []; | |
expect(flatArray).toEqual(expected); | |
}); | |
it("should skip from adding empty arrays to the result", function() { | |
const array = [[1, 2, 3], [], [], []]; | |
const flatArray = flatten(array); | |
const expected = [1, 2, 3]; | |
expect(flatArray).toEqual(expected); | |
}); | |
it("should work only with objects", function() { | |
const array = [{ zero: true }, [{ one: true }, [{ two: true }]]]; | |
const flatArray = flatten(array); | |
const expected = [{ zero: true }, { one: true }, { two: true }]; | |
expect(flatArray).toEqual(expected); | |
}); | |
it("should work with a empty strings", function() { | |
const array = ["", [[[""], ""]]]; | |
const flatArray = flatten(array); | |
const expected = ["", "", ""]; | |
expect(flatArray).toEqual(expected); | |
}); | |
it("should work with a variaty of types", function() { | |
const array = [[1, 2, 3], { zero: true }, [[[{ one: true }, ["a"]]]]]; | |
const flatArray = flatten(array); | |
const expected = [1, 2, 3, { zero: true }, { one: true }, "a"]; | |
expect(flatArray).toEqual(expected); | |
}); | |
it("should work with a variaty of types", function() { | |
const array = [[1, 2, 3], { zero: true }, [[[{ one: true }, ["a"]]]]]; | |
const flatArray = flatten(array); | |
const expected = [1, 2, 3, { zero: true }, { one: true }, "a"]; | |
expect(flatArray).toEqual(expected); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Recursively flattens a given `array`. | |
* | |
* @param {Array} array The array to flatten. | |
* @param {Array} result The accumulated value tha is recursively being flattened. | |
* @returns {Array} Returns the new flattened array. | |
* | |
* @example | |
* flatten([1, [2, [3, [4]], 5]]) | |
* will result into -> [1, 2, 3, 4, 5] | |
*/ | |
export function flatten(array: Array<any>, result?: Array<any>): Array<any> { | |
result || (result = []); | |
if (array == null) { | |
return result; | |
} | |
array.forEach(value => | |
Array.isArray(value) ? flatten(value, result) : result.push(value) | |
); | |
return result; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can visit this environment in case you would be interested in running the tests.