array-flatten
Advanced tools
Comparing version 1.0.2 to 1.1.0
/** | ||
* Recursive flatten function. Fastest implementation for array flattening. | ||
* Recursive flatten function with depth. | ||
* | ||
@@ -9,15 +9,38 @@ * @param {Array} array | ||
*/ | ||
function flatten (array, result, depth) { | ||
function flattenDepth (array, result, depth) { | ||
for (var i = 0; i < array.length; i++) { | ||
if (depth > 0 && Array.isArray(array[i])) { | ||
flatten(array[i], result, depth - 1); | ||
var value = array[i] | ||
if (depth > 0 && Array.isArray(value)) { | ||
flattenDepth(value, result, depth - 1) | ||
} else { | ||
result.push(array[i]); | ||
result.push(value) | ||
} | ||
} | ||
return result; | ||
return result | ||
} | ||
/** | ||
* Recursive flatten function. Omitting depth is slightly faster. | ||
* | ||
* @param {Array} array | ||
* @param {Array} result | ||
* @return {Array} | ||
*/ | ||
function flattenForever (array, result) { | ||
for (var i = 0; i < array.length; i++) { | ||
var value = array[i] | ||
if (Array.isArray(value)) { | ||
flattenForever(value, result) | ||
} else { | ||
result.push(value) | ||
} | ||
} | ||
return result | ||
} | ||
/** | ||
* Flatten an array, with the ability to define a depth. | ||
@@ -30,3 +53,7 @@ * | ||
module.exports = function (array, depth) { | ||
return flatten(array, [], depth || Infinity); | ||
}; | ||
if (depth == null) { | ||
return flattenForever(array, []) | ||
} | ||
return flattenDepth(array, [], depth) | ||
} |
{ | ||
"name": "array-flatten", | ||
"version": "1.0.2", | ||
"description": "Flatten a multi-dimensional array.", | ||
"version": "1.1.0", | ||
"description": "Flatten an array of nested arrays into a single flat array", | ||
"main": "array-flatten.js", | ||
@@ -20,3 +20,4 @@ "files": [ | ||
"flatten", | ||
"arguments" | ||
"arguments", | ||
"depth" | ||
], | ||
@@ -34,6 +35,7 @@ "author": { | ||
"devDependencies": { | ||
"istanbul": "^0.2.6", | ||
"mocha": "^1.18.0", | ||
"pre-commit": "0.0.9" | ||
"istanbul": "^0.3.13", | ||
"mocha": "^2.2.4", | ||
"pre-commit": "^1.0.7", | ||
"standard": "^3.7.3" | ||
} | ||
} |
# Array Flatten | ||
[![NPM version][npm-image]][npm-url] | ||
[![NPM downloads][downloads-image]][downloads-url] | ||
[![Build status][travis-image]][travis-url] | ||
[![Test coverage][coveralls-image]][coveralls-url] | ||
Flatten an array of nested arrays into a single flat array. Accepts an optional depth argument. | ||
> Flatten an array of nested arrays into a single flat array. Accepts an optional depth. | ||
@@ -18,13 +19,13 @@ ## Installation | ||
```javascript | ||
var flatten = require('array-flatten'); | ||
var flatten = require('array-flatten') | ||
flatten([1, [2, [3, [4, [5], 6], 7], 8], 9]); | ||
flatten([1, [2, [3, [4, [5], 6], 7], 8], 9]) | ||
//=> [1, 2, 3, 4, 5, 6, 7, 8, 9] | ||
flatten([1, [2, [3, [4, [5], 6], 7], 8], 9], 2); | ||
flatten([1, [2, [3, [4, [5], 6], 7], 8], 9], 2) | ||
//=> [1, 2, 3, [4, [5], 6], 7, 8, 9] | ||
(function () { | ||
flatten(arguments); //=> [1, 2, 3] | ||
})(1, [2, 3]); | ||
flatten(arguments) //=> [1, 2, 3] | ||
})(1, [2, 3]) | ||
``` | ||
@@ -38,2 +39,4 @@ | ||
[npm-url]: https://npmjs.org/package/array-flatten | ||
[downloads-image]: https://img.shields.io/npm/dm/array-flatten.svg?style=flat | ||
[downloads-url]: https://npmjs.org/package/array-flatten | ||
[travis-image]: https://img.shields.io/travis/blakeembrey/array-flatten.svg?style=flat | ||
@@ -40,0 +43,0 @@ [travis-url]: https://travis-ci.org/blakeembrey/array-flatten |
4335
50
44
4