pax_global_header00006660000000000000000000000064127105270250014513gustar00rootroot0000000000000052 comment=d5446b9a5e373c3dfa73b9275c53fc9c32ef0979 array-flatten-2.1.0/000077500000000000000000000000001271052702500142645ustar00rootroot00000000000000array-flatten-2.1.0/.editorconfig000066400000000000000000000003071271052702500167410ustar00rootroot00000000000000# EditorConfig is awesome: http://EditorConfig.org root = true [*] indent_size = 2 indent_style = space end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true array-flatten-2.1.0/.gitignore000066400000000000000000000000561271052702500162550ustar00rootroot00000000000000.DS_Store node_modules coverage npm-debug.log array-flatten-2.1.0/.travis.yml000066400000000000000000000003221271052702500163720ustar00rootroot00000000000000language: node_js notifications: email: on_success: never on_failure: change node_js: - "0.10" - "4.0" - "4.1" after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" array-flatten-2.1.0/LICENSE000066400000000000000000000021171271052702500152720ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2014 Blake Embrey (hello@blakeembrey.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. array-flatten-2.1.0/README.md000066400000000000000000000027621271052702500155520ustar00rootroot00000000000000# 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 nested arrays. ## Installation ``` npm install array-flatten --save ``` ## Usage ```javascript var flatten = require('array-flatten') flatten([1, [2, [3, [4, [5], 6], 7], 8], 9]) //=> [1, 2, 3, 4, 5, 6, 7, 8, 9] flatten.depth([1, [2, [3, [4, [5], 6], 7], 8], 9], 2) //=> [1, 2, 3, [4, [5], 6], 7, 8, 9] (function () { 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 MIT [npm-image]: https://img.shields.io/npm/v/array-flatten.svg?style=flat [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 [travis-url]: https://travis-ci.org/blakeembrey/array-flatten [coveralls-image]: https://img.shields.io/coveralls/blakeembrey/array-flatten.svg?style=flat [coveralls-url]: https://coveralls.io/r/blakeembrey/array-flatten?branch=master array-flatten-2.1.0/array-flatten.d.ts000066400000000000000000000006721271052702500176340ustar00rootroot00000000000000declare function flatten (array: flatten.NestedArray): T[]; declare namespace flatten { interface NestedArray { [index: number]: T | NestedArray; length: number; } export function from (array: NestedArray): T[]; export function depth (array: NestedArray, depth: number): NestedArray; export function depthFrom (array: NestedArray, depth: number): NestedArray; } export = flatten; array-flatten-2.1.0/array-flatten.js000066400000000000000000000037101271052702500173740ustar00rootroot00000000000000'use strict' /** * Expose `arrayFlatten`. */ module.exports = flatten module.exports.from = flattenFrom module.exports.depth = flattenDepth module.exports.fromDepth = flattenFromDepth /** * 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 {number} depth * @return {Array} */ function flattenDepth (array, depth) { if (!Array.isArray(array)) { throw new TypeError('Expected value to be an array') } 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 flattenDownDepth(array, [], depth) } /** * Flatten an array indefinitely. * * @param {Array} array * @param {Array} result * @return {Array} */ function flattenDown (array, result) { for (var i = 0; i < array.length; i++) { var value = array[i] if (Array.isArray(value)) { flattenDown(value, result) } else { result.push(value) } } return result } /** * Flatten an array with depth. * * @param {Array} array * @param {Array} result * @param {number} depth * @return {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 result } array-flatten-2.1.0/benchmark/000077500000000000000000000000001271052702500162165ustar00rootroot00000000000000array-flatten-2.1.0/benchmark/README.md000066400000000000000000000005301271052702500174730ustar00rootroot00000000000000# Benchmarks There are three different types of benchmarks running with associated modules. All are 100% compatible with others in their respective `code/` directory. **Interesting Note:** The fastest `flatten` method, by far, is using `instanceof` instead of `Array.isArray`. As this breaks compatibility across frames, it is not used here. array-flatten-2.1.0/benchmark/code/000077500000000000000000000000001271052702500171305ustar00rootroot00000000000000array-flatten-2.1.0/benchmark/code/arguments/000077500000000000000000000000001271052702500211355ustar00rootroot00000000000000array-flatten-2.1.0/benchmark/code/arguments/pass-as-arguments.js000066400000000000000000000001601271052702500250420ustar00rootroot00000000000000var current = require('../../..').from module.exports = args function args () { return current(arguments) } array-flatten-2.1.0/benchmark/code/arguments/pass-as-for.js000066400000000000000000000003431271052702500236260ustar00rootroot00000000000000var current = require('../../..').from module.exports = loop function loop () { var args = new Array(arguments.length) var len = args.length while (len--) { args[len] = arguments[len] } return current(args) } array-flatten-2.1.0/benchmark/code/arguments/pass-as-slice.js000066400000000000000000000002421271052702500241350ustar00rootroot00000000000000var current = require('../../..').from var _slice = Array.prototype.slice module.exports = slice function slice () { return current(_slice.call(arguments)) } array-flatten-2.1.0/benchmark/code/depth/000077500000000000000000000000001271052702500202345ustar00rootroot00000000000000array-flatten-2.1.0/benchmark/code/depth/current.js000066400000000000000000000000531271052702500222520ustar00rootroot00000000000000module.exports = require('../../..').depth array-flatten-2.1.0/benchmark/code/depth/while.js000066400000000000000000000006321271052702500217030ustar00rootroot00000000000000module.exports = function (array, depth) { if (depth < 1) { return array } return flatten(array, [], depth || Infinity) } function flatten (array, result, depth) { depth-- var len = array.length var i = 0 while (len--) { if (depth > -1 && Array.isArray(array[i])) { flatten(array[i], result, depth) } else { result.push(array[i]) } i++ } return result } array-flatten-2.1.0/benchmark/code/flatten/000077500000000000000000000000001271052702500205655ustar00rootroot00000000000000array-flatten-2.1.0/benchmark/code/flatten/concat.js000066400000000000000000000003071271052702500223720ustar00rootroot00000000000000module.exports = flatten function flatten (array) { var result = Array.prototype.concat.apply([], array) if (result.length === array.length) { return result } return flatten(result) } array-flatten-2.1.0/benchmark/code/flatten/current.js000066400000000000000000000000451271052702500226040ustar00rootroot00000000000000module.exports = require('../../..') array-flatten-2.1.0/benchmark/code/flatten/for-value.js000066400000000000000000000005321271052702500230230ustar00rootroot00000000000000module.exports = flatten function flatten (array) { return flattenWithResult(array, []) } function flattenWithResult (array, result) { for (var i = 0; i < array.length; i++) { var value = array[i] if (Array.isArray(value)) { flattenWithResult(value, result) } else { result.push(value) } } return result } array-flatten-2.1.0/benchmark/code/flatten/for.js000066400000000000000000000005111271052702500217060ustar00rootroot00000000000000module.exports = flatten function flatten (array) { return flattenWithResult(array, []) } function flattenWithResult (array, result) { for (var i = 0; i < array.length; i++) { if (Array.isArray(array[i])) { flattenWithResult(array[i], result) } else { result.push(array[i]) } } return result } array-flatten-2.1.0/benchmark/code/flatten/if-depth.js000066400000000000000000000012511271052702500226220ustar00rootroot00000000000000module.exports = flatten function flatten (array, depth) { if (depth == null) { return flattenWithoutDepth(array, []) } return flattenWithDepth(array, [], depth) } function flattenWithoutDepth (array, result) { for (var i = 0; i < array.length; i++) { if (Array.isArray(array[i])) { flattenWithoutDepth(array[i], result) } else { result.push(array[i]) } } return result } function flattenWithDepth (array, result, depth) { for (var i = 0; i < array.length; i++) { if (depth > 0 && Array.isArray(array[i])) { flattenWithDepth(array[i], result, depth - 1) } else { result.push(array[i]) } } return result } array-flatten-2.1.0/benchmark/code/flatten/no-return.js000066400000000000000000000005501271052702500230540ustar00rootroot00000000000000module.exports = flatten function flatten (array) { var result = [] flattenWithResult(array, result) return result } function flattenWithResult (array, result) { for (var i = 0; i < array.length; i++) { var value = array[i] if (Array.isArray(value)) { flattenWithResult(value, result) } else { result.push(value) } } } array-flatten-2.1.0/benchmark/code/flatten/reduce.js000066400000000000000000000003271271052702500223740ustar00rootroot00000000000000module.exports = reduce function reduce (array) { return array.reduce(function (acc, value) { if (Array.isArray(value)) { return acc.concat(reduce(value)) } return acc.concat(value) }, []) } array-flatten-2.1.0/benchmark/code/flatten/tostring.js000066400000000000000000000006121271052702500227730ustar00rootroot00000000000000var _toString = Object.prototype.toString module.exports = flatten function flatten (array) { return flattenWithResult(array, []) } function flattenWithResult (array, result) { for (var i = 0; i < array.length; i++) { if (_toString.call(array[i]) === '[object Array]') { flattenWithResult(array[i], result) } else { result.push(array[i]) } } return result } array-flatten-2.1.0/benchmark/code/flatten/while-2.js000066400000000000000000000005331271052702500223730ustar00rootroot00000000000000module.exports = flatten function flatten (array) { return flattenWithResult(array, []) } function flattenWithResult (array, result) { var len = array.length var i = -1 while (++i < len) { if (Array.isArray(array[i])) { flattenWithResult(array[i], result) } else { result.push(array[i]) } } return result } array-flatten-2.1.0/benchmark/code/flatten/while-3.js000066400000000000000000000005531271052702500223760ustar00rootroot00000000000000module.exports = flatten function flatten (array) { return flattenWithResult(array, []) } function flattenWithResult (array, result) { var len = array.length var num = 0 while (len--) { var i = num++ if (Array.isArray(array[i])) { flattenWithResult(array[i], result) } else { result.push(array[i]) } } return result } array-flatten-2.1.0/benchmark/code/flatten/while-4.js000066400000000000000000000005631271052702500224000ustar00rootroot00000000000000module.exports = flatten function flatten (array) { return flattenWithResult(array, []) } function flattenWithResult (array, result) { var len = array.length while (len) { var i = array.length - len if (Array.isArray(array[i])) { flattenWithResult(array[i], result) } else { result.push(array[i]) } len-- } return result } array-flatten-2.1.0/benchmark/code/flatten/while.js000066400000000000000000000005371271052702500222400ustar00rootroot00000000000000module.exports = flatten function flatten (array) { return flattenWithResult(array, []) } function flattenWithResult (array, result) { var len = array.length var i = 0 while (len--) { if (Array.isArray(array[i])) { flattenWithResult(array[i], result) } else { result.push(array[i]) } i++ } return result } array-flatten-2.1.0/benchmark/code/flatten/with-depth.js000066400000000000000000000006211271052702500231770ustar00rootroot00000000000000module.exports = flatten function flatten (array, depth) { return flattenWithResult(array, [], depth == null ? Infinity : depth) } function flattenWithResult (array, result, depth) { for (var i = 0; i < array.length; i++) { if (depth > 0 && Array.isArray(array[i])) { flattenWithResult(array[i], result, depth - 1) } else { result.push(array[i]) } } return result } array-flatten-2.1.0/benchmark/fixtures/000077500000000000000000000000001271052702500200675ustar00rootroot00000000000000array-flatten-2.1.0/benchmark/fixtures/large.js000066400000000000000000000011351271052702500215170ustar00rootroot00000000000000module.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']]]]], 'b', ['d'], [['g']], ['d', ['e']]] array-flatten-2.1.0/benchmark/fixtures/medium.js000066400000000000000000000002641271052702500217070ustar00rootroot00000000000000module.exports = [['a', ['b', ['k', ['a', ['b', ['c']], ['a', ['x', ['c'], ['a', ['x', ['k']]], ['d', ['z']]]], ['d', ['m']]], ['d', ['e']]]]], ['d', ['e']], 'd', ['e', 'z'], 'j'] array-flatten-2.1.0/benchmark/fixtures/none.js000066400000000000000000000000471271052702500213650ustar00rootroot00000000000000module.exports = [1, 2, 3, 4, 5, 6, 7] array-flatten-2.1.0/benchmark/fixtures/small.js000066400000000000000000000001201271052702500215260ustar00rootroot00000000000000module.exports = ['a', ['b', ['c', ['d']]], ['e', ['f']], 'h', ['i'], 'j', 'k'] array-flatten-2.1.0/benchmark/fixtures/typical.js000066400000000000000000000001151271052702500220670ustar00rootroot00000000000000function noop () {} module.exports = [noop, noop, [noop, noop, noop], noop] array-flatten-2.1.0/benchmark/index.js000066400000000000000000000014211271052702500176610ustar00rootroot00000000000000var Suite = require('benchmarked') var path = require('path') function name (filename) { return path.basename(path.dirname(filename)) + '/' + path.basename(filename, '.js') } var sample = [1, [2, [3, [4], 3], 2], 1] var flattenSuite = new Suite({ cwd: __dirname, fixtures: 'fixtures/*.js', add: 'code/flatten/*.js', name: name, sample: [sample] }) flattenSuite.run(function (fixture) { return [fixture] }) var argsSuite = new Suite({ cwd: __dirname, fixtures: 'fixtures/*.js', add: 'code/arguments/*.js', name: name, sample: sample }) argsSuite.run() var depthSuite = new Suite({ cwd: __dirname, fixtures: 'fixtures/*.js', add: 'code/depth/*.js', name: name, sample: [sample, 2] }) depthSuite.run(function (fixture) { return [fixture, 4] }) array-flatten-2.1.0/bower.json000066400000000000000000000006201271052702500162730ustar00rootroot00000000000000{ "name": "array-flatten", "version": "2.1.0", "homepage": "https://github.com/blakeembrey/array-flatten", "authors": [ "Blake Embrey" ], "description": "Flatten an array of nested arrays into a single flat array", "main": "array-flatten.js", "keywords": [], "license": "MIT", "ignore": [ "**/.*", "node_modules", "bower_components", "test", "tests" ] } array-flatten-2.1.0/package.json000066400000000000000000000021241271052702500165510ustar00rootroot00000000000000{ "name": "array-flatten", "version": "2.1.0", "description": "Flatten nested arrays", "main": "array-flatten.js", "typings": "array-flatten.d.ts", "files": [ "array-flatten.js", "array-flatten.d.ts", "LICENSE" ], "scripts": { "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" }, "repository": { "type": "git", "url": "git://github.com/blakeembrey/array-flatten.git" }, "keywords": [ "array", "flatten", "arguments", "depth", "fast", "for" ], "author": { "name": "Blake Embrey", "email": "hello@blakeembrey.com", "url": "http://blakeembrey.me" }, "license": "MIT", "bugs": { "url": "https://github.com/blakeembrey/array-flatten/issues" }, "homepage": "https://github.com/blakeembrey/array-flatten", "devDependencies": { "benchmarked": "^0.1.4", "istanbul": "^0.4.0", "mocha": "^2.2.4", "standard": "^5.3.1" } } array-flatten-2.1.0/test.js000066400000000000000000000030351271052702500156020ustar00rootroot00000000000000/* global describe, it */ var assert = require('assert') var flatten = require('./') describe('array-flatten', function () { describe('flatten', function () { it('should flatten an array', function () { var result = flatten([1, [2, [3, [4, [5]]], 6, [[7], 8], 9], 10]) assert.deepEqual(result, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) }) it('should throw on non-array', function () { assert.throws(function () { flatten('test') }, TypeError) }) it('should work with non-array', function () { var result = flatten.from('test') assert.deepEqual(result, ['t', 'e', 's', 't']) }) }) describe('depth', function () { it('should flatten an array to a specific depth', function () { var result = flatten.depth([1, [2, [3], 4], 5], 1) assert.deepEqual(result, [1, 2, [3], 4, 5]) }) it('should clone an array when no depth is specified', function () { var array = [1, [2, 3]] var clone = flatten.depth(array, 0) assert.ok(clone !== array) assert.deepEqual(clone, array) }) it('should throw on non-array', function () { assert.throws(function () { flatten.depth('test', 10) }, TypeError) }) it('should throw on non-numeric depth', function () { assert.throws(function () { flatten.fromDepth('test', 'test') }, TypeError) }) it('should work with "from"', function () { var result = flatten.fromDepth('test', 1) assert.deepEqual(result, ['t', 'e', 's', 't']) }) }) })