├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── appveyor.yml ├── benchmark ├── check.js ├── code │ ├── arr-flatten-0.1.0 (do-while).js │ ├── arr-flatten-0.2.0.js │ ├── arr-flatten-1.0.1.js │ ├── compute-flatten.js │ ├── do-while-slice.js │ ├── flatit.js │ ├── flatten-array.js │ ├── flatten.js │ ├── just-flatten-it.js │ ├── lib-array-flatten.js │ ├── lodash.flattendeep.js │ ├── m_flattened.js │ └── utils-flatten.js ├── fixtures │ ├── med.js │ ├── small-med.js │ └── small.js ├── index.js └── libs.js ├── bower.json ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | end_of_line = lf 6 | charset = utf-8 7 | indent_size = 2 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [{**/{actual,fixtures,expected,templates}/**,*.md}] 12 | trim_trailing_whitespace = false 13 | insert_final_newline = false -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": false, 4 | "es6": true, 5 | "node": true, 6 | "mocha": true 7 | }, 8 | 9 | "globals": { 10 | "document": false, 11 | "navigator": false, 12 | "window": false 13 | }, 14 | 15 | "rules": { 16 | "accessor-pairs": 2, 17 | "arrow-spacing": [2, { "before": true, "after": true }], 18 | "block-spacing": [2, "always"], 19 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 20 | "comma-dangle": [2, "never"], 21 | "comma-spacing": [2, { "before": false, "after": true }], 22 | "comma-style": [2, "last"], 23 | "constructor-super": 2, 24 | "curly": [2, "multi-line"], 25 | "dot-location": [2, "property"], 26 | "eol-last": 2, 27 | "eqeqeq": [2, "allow-null"], 28 | "generator-star-spacing": [2, { "before": true, "after": true }], 29 | "handle-callback-err": [2, "^(err|error)$" ], 30 | "indent": [2, 2, { "SwitchCase": 1 }], 31 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 32 | "keyword-spacing": [2, { "before": true, "after": true }], 33 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 34 | "new-parens": 2, 35 | "no-array-constructor": 2, 36 | "no-caller": 2, 37 | "no-class-assign": 2, 38 | "no-cond-assign": 2, 39 | "no-const-assign": 2, 40 | "no-control-regex": 2, 41 | "no-debugger": 2, 42 | "no-delete-var": 2, 43 | "no-dupe-args": 2, 44 | "no-dupe-class-members": 2, 45 | "no-dupe-keys": 2, 46 | "no-duplicate-case": 2, 47 | "no-empty-character-class": 2, 48 | "no-eval": 2, 49 | "no-ex-assign": 2, 50 | "no-extend-native": 2, 51 | "no-extra-bind": 2, 52 | "no-extra-boolean-cast": 2, 53 | "no-extra-parens": [2, "functions"], 54 | "no-fallthrough": 2, 55 | "no-floating-decimal": 2, 56 | "no-func-assign": 2, 57 | "no-implied-eval": 2, 58 | "no-inner-declarations": [2, "functions"], 59 | "no-invalid-regexp": 2, 60 | "no-irregular-whitespace": 2, 61 | "no-iterator": 2, 62 | "no-label-var": 2, 63 | "no-labels": 2, 64 | "no-lone-blocks": 2, 65 | "no-mixed-spaces-and-tabs": 2, 66 | "no-multi-spaces": 2, 67 | "no-multi-str": 2, 68 | "no-multiple-empty-lines": [2, { "max": 1 }], 69 | "no-native-reassign": 0, 70 | "no-negated-in-lhs": 2, 71 | "no-new": 2, 72 | "no-new-func": 2, 73 | "no-new-object": 2, 74 | "no-new-require": 2, 75 | "no-new-wrappers": 2, 76 | "no-obj-calls": 2, 77 | "no-octal": 2, 78 | "no-octal-escape": 2, 79 | "no-proto": 0, 80 | "no-redeclare": 2, 81 | "no-regex-spaces": 2, 82 | "no-return-assign": 2, 83 | "no-self-compare": 2, 84 | "no-sequences": 2, 85 | "no-shadow-restricted-names": 2, 86 | "no-spaced-func": 2, 87 | "no-sparse-arrays": 2, 88 | "no-this-before-super": 2, 89 | "no-throw-literal": 2, 90 | "no-trailing-spaces": 0, 91 | "no-undef": 2, 92 | "no-undef-init": 2, 93 | "no-unexpected-multiline": 2, 94 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 95 | "no-unreachable": 2, 96 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 97 | "no-useless-call": 0, 98 | "no-with": 2, 99 | "one-var": [0, { "initialized": "never" }], 100 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 101 | "padded-blocks": [0, "never"], 102 | "quotes": [2, "single", "avoid-escape"], 103 | "radix": 2, 104 | "semi": [2, "always"], 105 | "semi-spacing": [2, { "before": false, "after": true }], 106 | "space-before-blocks": [2, "always"], 107 | "space-before-function-paren": [2, "never"], 108 | "space-in-parens": [2, "never"], 109 | "space-infix-ops": 2, 110 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 111 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 112 | "use-isnan": 2, 113 | "valid-typeof": 2, 114 | "wrap-iife": [2, "any"], 115 | "yoda": [2, "never"] 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | *.sublime-* 4 | 5 | # test related, or directories generated by tests 6 | test/actual 7 | actual 8 | coverage 9 | .nyc* 10 | 11 | # npm 12 | node_modules 13 | npm-debug.log 14 | 15 | # yarn 16 | yarn.lock 17 | yarn-error.log 18 | 19 | # misc 20 | _gh_pages 21 | _draft 22 | _drafts 23 | bower_components 24 | vendor 25 | temp 26 | tmp 27 | TODO.md 28 | package-lock.json -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | os: 3 | - linux 4 | - osx 5 | language: node_js 6 | node_js: 7 | - node 8 | - iojs 9 | - '8' 10 | - '7' 11 | - '6' 12 | - '5' 13 | - '4' 14 | - '0.12' 15 | - '0.10' 16 | git: 17 | depth: 10 18 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## Install 2 | 3 | {%= include("install-bower", {save: true}) %} 4 | 5 | ## Usage 6 | 7 | ```js 8 | var flatten = require('{%= name %}'); 9 | 10 | flatten(['a', ['b', ['c']], 'd', ['e']]); 11 | //=> ['a', 'b', 'c', 'd', 'e'] 12 | ``` 13 | 14 | ## Why another flatten utility? 15 | 16 | I wanted the fastest implementation I could find, with implementation choices that should work for 95% of use cases, but no cruft to cover the other 5%. 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2017, Jon Schlinkert. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # arr-flatten [![NPM version](https://img.shields.io/npm/v/arr-flatten.svg?style=flat)](https://www.npmjs.com/package/arr-flatten) [![NPM monthly downloads](https://img.shields.io/npm/dm/arr-flatten.svg?style=flat)](https://npmjs.org/package/arr-flatten) [![NPM total downloads](https://img.shields.io/npm/dt/arr-flatten.svg?style=flat)](https://npmjs.org/package/arr-flatten) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/arr-flatten.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/arr-flatten) [![Windows Build Status](https://img.shields.io/appveyor/ci/jonschlinkert/arr-flatten.svg?style=flat&label=AppVeyor)](https://ci.appveyor.com/project/jonschlinkert/arr-flatten) 2 | 3 | > Recursively flatten an array or arrays. 4 | 5 | ## Install 6 | 7 | Install with [npm](https://www.npmjs.com/): 8 | 9 | ```sh 10 | $ npm install --save arr-flatten 11 | ``` 12 | 13 | ## Install 14 | 15 | Install with [bower](https://bower.io/) 16 | 17 | ```sh 18 | $ bower install arr-flatten --save 19 | ``` 20 | 21 | ## Usage 22 | 23 | ```js 24 | var flatten = require('arr-flatten'); 25 | 26 | flatten(['a', ['b', ['c']], 'd', ['e']]); 27 | //=> ['a', 'b', 'c', 'd', 'e'] 28 | ``` 29 | 30 | ## Why another flatten utility? 31 | 32 | I wanted the fastest implementation I could find, with implementation choices that should work for 95% of use cases, but no cruft to cover the other 5%. 33 | 34 | ## About 35 | 36 | ### Related projects 37 | 38 | * [arr-filter](https://www.npmjs.com/package/arr-filter): Faster alternative to javascript's native filter method. | [homepage](https://github.com/jonschlinkert/arr-filter "Faster alternative to javascript's native filter method.") 39 | * [arr-union](https://www.npmjs.com/package/arr-union): Combines a list of arrays, returning a single array with unique values, using strict equality… [more](https://github.com/jonschlinkert/arr-union) | [homepage](https://github.com/jonschlinkert/arr-union "Combines a list of arrays, returning a single array with unique values, using strict equality for comparisons.") 40 | * [array-each](https://www.npmjs.com/package/array-each): Loop over each item in an array and call the given function on every element. | [homepage](https://github.com/jonschlinkert/array-each "Loop over each item in an array and call the given function on every element.") 41 | * [array-unique](https://www.npmjs.com/package/array-unique): Remove duplicate values from an array. Fastest ES5 implementation. | [homepage](https://github.com/jonschlinkert/array-unique "Remove duplicate values from an array. Fastest ES5 implementation.") 42 | 43 | ### Contributing 44 | 45 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 46 | 47 | ### Contributors 48 | 49 | | **Commits** | **Contributor** | 50 | | --- | --- | 51 | | 20 | [jonschlinkert](https://github.com/jonschlinkert) | 52 | | 1 | [lukeed](https://github.com/lukeed) | 53 | 54 | ### Building docs 55 | 56 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ 57 | 58 | To generate the readme, run the following command: 59 | 60 | ```sh 61 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 62 | ``` 63 | 64 | ### Running tests 65 | 66 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: 67 | 68 | ```sh 69 | $ npm install && npm test 70 | ``` 71 | 72 | ### Author 73 | 74 | **Jon Schlinkert** 75 | 76 | * [github/jonschlinkert](https://github.com/jonschlinkert) 77 | * [twitter/jonschlinkert](https://twitter.com/jonschlinkert) 78 | 79 | ### License 80 | 81 | Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). 82 | Released under the [MIT License](LICENSE). 83 | 84 | *** 85 | 86 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on July 05, 2017._ -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | # Test against this version of Node.js 2 | environment: 3 | matrix: 4 | # node.js 5 | - nodejs_version: "7.0" 6 | - nodejs_version: "6.0" 7 | - nodejs_version: "5.0" 8 | - nodejs_version: "4.0" 9 | - nodejs_version: "0.12" 10 | - nodejs_version: "0.10" 11 | 12 | # Install scripts. (runs after repo cloning) 13 | install: 14 | # Get the latest stable version of Node.js 15 | - ps: Install-Product node $env:nodejs_version 16 | # install modules 17 | - npm install 18 | 19 | # Post-install test scripts. 20 | test_script: 21 | # Output useful info for debugging. 22 | - node --version 23 | - npm --version 24 | # run tests 25 | - npm test 26 | 27 | # Don't actually build. 28 | build: off 29 | -------------------------------------------------------------------------------- /benchmark/check.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var path = require('path'); 4 | var util = require('util'); 5 | var glob = require('glob'); 6 | var bold = require('ansi-bold'); 7 | 8 | /** 9 | * Sanity check 10 | * 11 | * Run to ensure that all fns return the same, correct result. 12 | */ 13 | 14 | var fixtures = glob.sync(__dirname + '/fixtures/l*.js'); 15 | 16 | glob.sync(__dirname + '/code/*.js').forEach(function (fp) { 17 | var fn = require(path.resolve(__dirname, 'code', fp)); 18 | var name = path.basename(fp, path.extname(fp)); 19 | 20 | fixtures.forEach(function (fixture) { 21 | console.log(bold(name) + ':', inspect(fn(require(fixture)))); 22 | }); 23 | }); 24 | 25 | function inspect(o) { 26 | var str = util.inspect(o, {depth: null}); 27 | return str.replace(/[\s\n]+/g, ' '); 28 | } 29 | -------------------------------------------------------------------------------- /benchmark/code/arr-flatten-0.1.0 (do-while).js: -------------------------------------------------------------------------------- 1 | module.exports = function () { 2 | var args = [].slice.call(arguments); 3 | 4 | do { 5 | args = [].concat.apply([], args); 6 | } while (args.some(Array.isArray)); 7 | 8 | return args; 9 | }; 10 | -------------------------------------------------------------------------------- /benchmark/code/arr-flatten-0.2.0.js: -------------------------------------------------------------------------------- 1 | module.exports = function (arr) { 2 | return flatten(arr, []); 3 | }; 4 | 5 | function flatten(arr, res) { 6 | var len = arr.length; 7 | var num = 0; 8 | 9 | while (len--) { 10 | var i = num++; 11 | 12 | if (Array.isArray(arr[i])) { 13 | flatten(arr[i], res); 14 | } else { 15 | res.push(arr[i]); 16 | } 17 | } 18 | return res; 19 | } 20 | -------------------------------------------------------------------------------- /benchmark/code/arr-flatten-1.0.1.js: -------------------------------------------------------------------------------- 1 | module.exports = require('../..'); -------------------------------------------------------------------------------- /benchmark/code/compute-flatten.js: -------------------------------------------------------------------------------- 1 | module.exports = require('compute-flatten'); 2 | -------------------------------------------------------------------------------- /benchmark/code/do-while-slice.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var slice = require('array-slice'); 4 | 5 | module.exports = function () { 6 | var args = slice(arguments); 7 | 8 | do { 9 | args = [].concat.apply([], args); 10 | } while (args.some(Array.isArray)); 11 | 12 | return args; 13 | }; -------------------------------------------------------------------------------- /benchmark/code/flatit.js: -------------------------------------------------------------------------------- 1 | module.exports = require('flatit'); 2 | -------------------------------------------------------------------------------- /benchmark/code/flatten-array.js: -------------------------------------------------------------------------------- 1 | module.exports = require('flatten-array'); 2 | -------------------------------------------------------------------------------- /benchmark/code/flatten.js: -------------------------------------------------------------------------------- 1 | module.exports = require('flatten'); 2 | -------------------------------------------------------------------------------- /benchmark/code/just-flatten-it.js: -------------------------------------------------------------------------------- 1 | module.exports = require('just-flatten-it'); 2 | -------------------------------------------------------------------------------- /benchmark/code/lib-array-flatten.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = require('array-flatten'); -------------------------------------------------------------------------------- /benchmark/code/lodash.flattendeep.js: -------------------------------------------------------------------------------- 1 | module.exports = require('lodash.flattendeep'); 2 | -------------------------------------------------------------------------------- /benchmark/code/m_flattened.js: -------------------------------------------------------------------------------- 1 | module.exports = require('m_flattened'); 2 | -------------------------------------------------------------------------------- /benchmark/code/utils-flatten.js: -------------------------------------------------------------------------------- 1 | module.exports = require('utils-flatten'); 2 | -------------------------------------------------------------------------------- /benchmark/fixtures/med.js: -------------------------------------------------------------------------------- 1 | module.exports = [[['a', ['b', ['k', ['a', ['b', ['c'], [['a', [['a', ['b', ['k', ['a', ['b', ['c']], ['a', ['x', ['c'], ['a', ['x', ['k']]], ['d', ['z']]]], ['d', ['m']]], ['d', ['e']]]]], ['d', ['e']]], ['b', ['k', ['a', ['b', ['c']], ['a', ['x', ['c'], ['a', ['x', ['k']]], ['d', ['z']]]], ['d', ['m']]], ['d', ['e']]]]], ['d', ['e']]]], ['a', ['x', ['c'], ['a', ['x', ['k']], [['a', ['b', ['k', ['a', ['b', ['c']], ['a', ['x', ['c'], ['a', ['x', ['k']]], ['d', ['z']]]], ['d', ['m']]], ['d', ['e']]]]], ['d', ['e']]]], ['d', ['z']]]], ['d', ['m']]], ['d', ['e']]]]], ['d', ['e']]]]; -------------------------------------------------------------------------------- /benchmark/fixtures/small-med.js: -------------------------------------------------------------------------------- 1 | module.exports = [[['a', ['b', ['k', ['a', ['b', ['c']], ['a', ['x', ['c'], ['a', ['x', ['k']]], ['d', ['z']]]], ['d', ['m']]], ['d', ['e']]]]], ['d', ['e']]]]; 2 | -------------------------------------------------------------------------------- /benchmark/fixtures/small.js: -------------------------------------------------------------------------------- 1 | module.exports = [[['a', ['b', ['c']]], ['d', ['e']]]]; 2 | -------------------------------------------------------------------------------- /benchmark/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var Suite = require('benchmarked'); 4 | var suite = new Suite({ 5 | result: false, 6 | fixtures: 'fixtures/*.js', 7 | code: 'code/*.js', 8 | cwd: __dirname 9 | }); 10 | 11 | suite.run(); 12 | -------------------------------------------------------------------------------- /benchmark/libs.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var fs = require('fs'); 4 | var path = require('path'); 5 | var write = require('write'); 6 | var cwd = path.join.bind(path, __dirname, 'code'); 7 | 8 | [ 9 | 'compute-flatten', 10 | 'flatit', 11 | 'flatten', 12 | 'flatten-array', 13 | 'just-flatten-it', 14 | 'lodash.flatten', 15 | 'lodash.flattendeep', 16 | 'm_flattened', 17 | 'reduce-flatten', 18 | 'utils-flatten' 19 | ].forEach(function(name) { 20 | var fp = cwd(name + '.js'); 21 | if (!fs.existsSync(fp)) { 22 | write.sync(fp, `module.exports = require('${name}');\n`); 23 | } 24 | }); 25 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "arr-flatten", 3 | "version": "1.0.3", 4 | "main": [ 5 | "index.js" 6 | ], 7 | "description": "Recursively flatten an array or arrays.", 8 | "homepage": "https://github.com/jonschlinkert/arr-flatten", 9 | "repository": "jonschlinkert/arr-flatten", 10 | "bugs": { 11 | "url": "https://github.com/jonschlinkert/arr-flatten/issues" 12 | }, 13 | "license": "MIT", 14 | "files": [ 15 | "index.js" 16 | ], 17 | "devDependencies": { 18 | "ansi-bold": "^0.1.1", 19 | "array-flatten": "^2.1.1", 20 | "array-slice": "^1.0.0", 21 | "benchmarked": "^1.0.0", 22 | "compute-flatten": "^1.0.0", 23 | "flatit": "^1.1.1", 24 | "flatten": "^1.0.2", 25 | "flatten-array": "^1.0.0", 26 | "glob": "^7.1.1", 27 | "gulp-format-md": "^0.1.12", 28 | "just-flatten-it": "^1.1.23", 29 | "lodash.flattendeep": "^4.4.0", 30 | "m_flattened": "^1.0.1", 31 | "mocha": "^3.2.0", 32 | "utils-flatten": "^1.0.0", 33 | "write": "^0.3.3" 34 | }, 35 | "keywords": [ 36 | "arr", 37 | "array", 38 | "elements", 39 | "flat", 40 | "flatten", 41 | "nested", 42 | "recurse", 43 | "recursive", 44 | "recursively" 45 | ], 46 | "authors": [ 47 | "Jon Schlinkert (https://github.com/jonschlinkert)" 48 | ], 49 | "ignore": [ 50 | "actual", 51 | "bower_components", 52 | "fixtures", 53 | "node_modules", 54 | "temp", 55 | "test", 56 | "test.js", 57 | "tmp" 58 | ] 59 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * arr-flatten 3 | * 4 | * Copyright (c) 2014-2017, Jon Schlinkert. 5 | * Released under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | module.exports = function (arr) { 11 | return flat(arr, []); 12 | }; 13 | 14 | function flat(arr, res) { 15 | var i = 0, cur; 16 | var len = arr.length; 17 | for (; i < len; i++) { 18 | cur = arr[i]; 19 | Array.isArray(cur) ? flat(cur, res) : res.push(cur); 20 | } 21 | return res; 22 | } 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "arr-flatten", 3 | "description": "Recursively flatten an array or arrays.", 4 | "version": "1.1.0", 5 | "homepage": "https://github.com/jonschlinkert/arr-flatten", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "contributors": [ 8 | "Jon Schlinkert (http://twitter.com/jonschlinkert)", 9 | "Luke Edwards (https://lukeed.com)" 10 | ], 11 | "repository": "jonschlinkert/arr-flatten", 12 | "bugs": { 13 | "url": "https://github.com/jonschlinkert/arr-flatten/issues" 14 | }, 15 | "license": "MIT", 16 | "files": [ 17 | "index.js" 18 | ], 19 | "main": "index.js", 20 | "engines": { 21 | "node": ">=0.10.0" 22 | }, 23 | "scripts": { 24 | "test": "mocha" 25 | }, 26 | "devDependencies": { 27 | "ansi-bold": "^0.1.1", 28 | "array-flatten": "^2.1.1", 29 | "array-slice": "^1.0.0", 30 | "benchmarked": "^1.0.0", 31 | "compute-flatten": "^1.0.0", 32 | "flatit": "^1.1.1", 33 | "flatten": "^1.0.2", 34 | "flatten-array": "^1.0.0", 35 | "glob": "^7.1.1", 36 | "gulp-format-md": "^0.1.12", 37 | "just-flatten-it": "^1.1.23", 38 | "lodash.flattendeep": "^4.4.0", 39 | "m_flattened": "^1.0.1", 40 | "mocha": "^3.2.0", 41 | "utils-flatten": "^1.0.0", 42 | "write": "^0.3.3" 43 | }, 44 | "keywords": [ 45 | "arr", 46 | "array", 47 | "elements", 48 | "flat", 49 | "flatten", 50 | "nested", 51 | "recurse", 52 | "recursive", 53 | "recursively" 54 | ], 55 | "verb": { 56 | "toc": false, 57 | "layout": "default", 58 | "tasks": [ 59 | "readme" 60 | ], 61 | "plugins": [ 62 | "gulp-format-md" 63 | ], 64 | "related": { 65 | "list": [ 66 | "arr-filter", 67 | "arr-union", 68 | "array-each", 69 | "array-unique" 70 | ] 71 | }, 72 | "lint": { 73 | "reflinks": true 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * arr-flatten 3 | * 4 | * Copyright (c) 2014-2017, Jon Schlinkert. 5 | * Licensed under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | require('mocha'); 11 | var assert = require('assert'); 12 | var flatten = require('./'); 13 | 14 | describe('flatten', function() { 15 | it('should flatten nested arrays:', function() { 16 | assert.deepEqual(flatten(['a', 'b', ['c'], 'd', ['e']]), ['a', 'b', 'c', 'd', 'e']); 17 | }); 18 | 19 | it('should flatten deeply nested arrays:', function() { 20 | assert.deepEqual(flatten(['a', [[[[[[[['b', [['c']]]]]], 'd', ['e']]]]]]), ['a', 'b', 'c', 'd', 'e']); 21 | assert.deepEqual(flatten(['a', 'b', ['c'], 'd', ['e']]), ['a', 'b', 'c', 'd', 'e']); 22 | assert.deepEqual(flatten([['a', ['b', ['k', ['a', ['b', ['c'], [['a', [['a', ['b', ['k', ['a', ['b', ['c']], ['a', ['x', ['c'], ['a', ['x', ['k']]], ['d', ['z']]]], ['d', ['m']]], ['d', ['e']]]]], ['d', ['e']]], ['b', ['k', ['a', ['b', ['c']], ['a', ['x', ['c'], ['a', ['x', ['k']]], ['d', ['z']]]], ['d', ['m']]], ['d', ['e']]]]], ['d', ['e']]]], ['a', ['x', ['c'], ['a', ['x', ['k']], [['a', ['b', ['k', ['a', ['b', ['c']], ['a', ['x', ['c'], ['a', ['x', ['k']]], ['d', ['z']]]], ['d', ['m']]], ['d', ['e']]]]], ['d', ['e']]]], ['d', ['z']]]], ['d', ['m']]], ['d', ['e']]]]], ['d', ['e']]]), [ 'a', 'b', 'k', 'a', 'b', 'c', 'a', 'a', 'b', 'k', 'a', 'b', 'c', 'a', 'x', 'c', 'a', 'x', 'k', 'd', 'z', 'd', 'm', 'd', 'e', 'd', 'e', 'b', 'k', 'a', 'b', 'c', 'a', 'x', 'c', 'a', 'x', 'k', 'd', 'z', 'd', 'm', 'd', 'e', 'd', 'e', 'a', 'x', 'c', 'a', 'x', 'k', 'a', 'b', 'k', 'a', 'b', 'c', 'a', 'x', 'c', 'a', 'x', 'k', 'd', 'z', 'd', 'm', 'd', 'e', 'd', 'e', 'd', 'z', 'd', 'm', 'd', 'e', 'd', 'e' ]); 23 | }); 24 | }); 25 | 26 | --------------------------------------------------------------------------------