4 * Expose `arrayFlatten`.
6 module.exports = arrayFlatten
9 * Recursive flatten function with depth.
11 * @param {Array} array
12 * @param {Array} result
13 * @param {Number} depth
16 function flattenWithDepth (array, result, depth) {
17 for (var i = 0; i < array.length; i++) {
20 if (depth > 0 && Array.isArray(value)) {
21 flattenWithDepth(value, result, depth - 1)
31 * Recursive flatten function. Omitting depth is slightly faster.
33 * @param {Array} array
34 * @param {Array} result
37 function flattenForever (array, result) {
38 for (var i = 0; i < array.length; i++) {
41 if (Array.isArray(value)) {
42 flattenForever(value, result)
52 * Flatten an array, with the ability to define a depth.
54 * @param {Array} array
55 * @param {Number} depth
58 function arrayFlatten (array, depth) {
60 return flattenForever(array, [])
63 return flattenWithDepth(array, [], depth)