Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

array-flatten

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-flatten - npm Package Compare versions

Comparing version 1.1.1 to 2.0.0

88

array-flatten.js

@@ -6,28 +6,63 @@ 'use strict'

*/
module.exports = arrayFlatten
module.exports = flatten
module.exports.from = flattenFrom
module.exports.depth = flattenDepth
module.exports.fromDepth = flattenFromDepth
/**
* Recursive flatten function with depth.
* Flatten an array.
*
* @param {Array} array
* @return {Array}
*/
function flatten (array) {
if (!Array.isArray(array)) {
throw new TypeError('Expected value to be an array')
}
return flattenFrom(array)
}
/**
* Flatten an array-like structure.
*
* @param {Array} array
* @return {Array}
*/
function flattenFrom (array) {
return flattenDown(array, [], Infinity)
}
/**
* Flatten an array-like structure with depth.
*
* @param {Array} array
* @param {Array} result
* @param {Number} depth
* @param {number} depth
* @return {Array}
*/
function flattenWithDepth (array, result, depth) {
for (var i = 0; i < array.length; i++) {
var value = array[i]
function flattenDepth (array, depth) {
if (!Array.isArray(array)) {
throw new TypeError('Expected value to be an array')
}
if (depth > 0 && Array.isArray(value)) {
flattenWithDepth(value, result, depth - 1)
} else {
result.push(value)
}
return flattenFromDepth(array, depth)
}
/**
* Flatten an array-like structure with depth.
*
* @param {Array} array
* @param {number} depth
* @return {Array}
*/
function flattenFromDepth (array, depth) {
if (typeof depth !== 'number') {
throw new TypeError('Expected the depth to be a number')
}
return result
return flattenDownDepth(array, [], depth)
}
/**
* Recursive flatten function. Omitting depth is slightly faster.
* Flatten an array indefinitely.
*

@@ -38,3 +73,3 @@ * @param {Array} array

*/
function flattenForever (array, result) {
function flattenDown (array, result) {
for (var i = 0; i < array.length; i++) {

@@ -44,3 +79,3 @@ var value = array[i]

if (Array.isArray(value)) {
flattenForever(value, result)
flattenDown(value, result)
} else {

@@ -55,14 +90,23 @@ result.push(value)

/**
* Flatten an array, with the ability to define a depth.
* Flatten an array with depth.
*
* @param {Array} array
* @param {Number} depth
* @param {Array} result
* @param {number} depth
* @return {Array}
*/
function arrayFlatten (array, depth) {
if (depth == null) {
return flattenForever(array, [])
function flattenDownDepth (array, result, depth) {
depth--
for (var i = 0; i < array.length; i++) {
var value = array[i]
if (depth > -1 && Array.isArray(value)) {
flattenDownDepth(value, result, depth)
} else {
result.push(value)
}
}
return flattenWithDepth(array, [], depth)
return result
}
{
"name": "array-flatten",
"version": "1.1.1",
"description": "Flatten an array of nested arrays into a single flat array",
"version": "2.0.0",
"description": "Flatten nested arrays",
"main": "array-flatten.js",

@@ -11,3 +11,7 @@ "files": [

"scripts": {
"test": "istanbul cover _mocha -- -R spec"
"lint": "standard",
"test-spec": "mocha -R spec --bail",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- -R spec --bail",
"test": "npm run lint && npm run test-cov",
"benchmark": "node benchmark"
},

@@ -22,3 +26,5 @@ "repository": {

"arguments",
"depth"
"depth",
"fast",
"for"
],

@@ -36,7 +42,8 @@ "author": {

"devDependencies": {
"istanbul": "^0.3.13",
"benchmarked": "^0.1.4",
"istanbul": "^0.4.0",
"mocha": "^2.2.4",
"pre-commit": "^1.0.7",
"standard": "^3.7.3"
"standard": "^5.3.1"
}
}

@@ -8,3 +8,3 @@ # Array Flatten

> Flatten an array of nested arrays into a single flat array. Accepts an optional depth.
> Flatten nested arrays.

@@ -25,10 +25,17 @@ ## Installation

flatten([1, [2, [3, [4, [5], 6], 7], 8], 9], 2)
flatten.depth([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]
flatten.from(arguments) //=> [1, 2, 3]
})(1, [2, 3])
```
### Methods
* **flatten(array)** Flatten a nested array structure
* **flatten.from(arrayish)** Flatten an array-like structure (E.g. arguments)
* **flatten.depth(array, depth)** Flatten a nested array structure with a specific depth
* **flatten.fromDepth(arrayish, depth)** Flatten an array-like structure with a specific depth
## License

@@ -35,0 +42,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc