Last active
January 26, 2017 19:05
-
-
Save dannycroft/5fdaa7c0931ea8da36140d280fe7fc5d to your computer and use it in GitHub Desktop.
Flatten multi-dimensional array. Run the tests here: http://codepen.io/dannycroft/full/wgrmQo/
This file contains hidden or 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
| var flattenArray = require('../flattenArray'); | |
| describe("flattenArray()", function(){ | |
| it("should return an array", function() { | |
| var test = flattenArray([1,2,3]); | |
| expect(test).to.be.an('array'); | |
| expect(test.length).to.equal(3); | |
| }); | |
| it("should throw if passed a non array", function() { | |
| expect(function() { flattenArray("array"); }).to.throw(TypeError); | |
| expect(function() { flattenArray(1); }).to.throw(TypeError); | |
| expect(function() { flattenArray(true); }).to.throw(TypeError); | |
| expect(function() { flattenArray(false); }).to.throw(TypeError); | |
| expect(function() { flattenArray(Array); }).to.throw(TypeError); | |
| }); | |
| it("should flatten a multi-dimensional array", function() { | |
| var array1 = [[1,2,[3]],4]; | |
| var array2 = [[1, 2],[3, 4, 5], [6, 7, 8, 9]]; | |
| var array3 = [1, [2,3], [4,[5,[6]]]]; | |
| expect(flattenArray(array1)).to.deep.equal([1, 2, 3, 4]); | |
| expect(flattenArray(array2)).to.deep.equal([1, 2, 3, 4, 5, 6, 7, 8, 9]); | |
| expect(flattenArray(array3)).to.deep.equal([1, 2, 3, 4, 5, 6]); | |
| }); | |
| it("should return the same item type as the origin array", function() { | |
| var array1 = [[1,2,[3]],4]; | |
| var array2 = [['1', 2],[true, 4, 5], [6, 7, 8, '9']]; | |
| var array3 = ['1', ['2',3], ['4',[5,[false]]]]; | |
| expect(flattenArray(array1)).to.deep.equal([1, 2, 3, 4]); | |
| expect(flattenArray(array2)).to.deep.equal(['1', 2, true, 4, 5, 6, 7, 8, '9']); | |
| expect(flattenArray(array3)).to.deep.equal(['1', '2', 3, '4', 5, false]); | |
| }); | |
| it("should handle deeply nested arrays", function() { | |
| var array = [[[[[[[[[[[[[[[['hello']]]]]], 'world']]]]]]]]]]; | |
| expect(flattenArray(array).join(' ')).to.equal('hello world'); | |
| }); | |
| }); |
This file contains hidden or 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
| 'use strict'; | |
| module.exports = flattenArray; | |
| /** | |
| * Flatten multi-dimensional array. | |
| * | |
| * @param {Array} arr | |
| * @return {Array} | |
| */ | |
| function flattenArray(arr) { | |
| if (!Array.isArray(arr)) { | |
| throw new TypeError('Expected value to be an array'); | |
| } | |
| return arr.reduce((accumulator, item) => { | |
| return Array.isArray(item) | |
| // item is array so flatten before adding to accumulator | |
| ? accumulator = accumulator.concat(flattenArray(item)) | |
| // item not array so add into accumulator | |
| : accumulator.push(item) && accumulator; | |
| }, []); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment