[Pkg-javascript-commits] [node-array-flatten] 01/02: Imported Upstream version 2.0.0

Thorsten Alteholz alteholz at moszumanska.debian.org
Sat Feb 27 14:00:25 UTC 2016


This is an automated email from the git hooks/post-receive script.

alteholz pushed a commit to branch master
in repository node-array-flatten.

commit b206ba5c2a129a26988a465e77377fcf0e661d2a
Author: Thorsten Alteholz <debian at alteholz.de>
Date:   Sat Feb 27 15:00:13 2016 +0100

    Imported Upstream version 2.0.0
---
 .editorconfig                                 |  11 +++
 .gitignore                                    |   4 +
 .travis.yml                                   |  13 ++++
 LICENSE                                       |  21 +++++
 README.md                                     |  50 ++++++++++++
 array-flatten.js                              | 108 ++++++++++++++++++++++++++
 benchmark/README.md                           |   5 ++
 benchmark/code/arguments/pass-as-arguments.js |   7 ++
 benchmark/code/arguments/pass-as-for.js       |  14 ++++
 benchmark/code/arguments/pass-as-slice.js     |   8 ++
 benchmark/code/depth/current.js               |   1 +
 benchmark/code/depth/while.js                 |  26 +++++++
 benchmark/code/flatten/concat.js              |  11 +++
 benchmark/code/flatten/current.js             |   1 +
 benchmark/code/flatten/for-value.js           |  19 +++++
 benchmark/code/flatten/for.js                 |  17 ++++
 benchmark/code/flatten/if-depth.js            |  33 ++++++++
 benchmark/code/flatten/no-return.js           |  19 +++++
 benchmark/code/flatten/reduce.js              |  11 +++
 benchmark/code/flatten/tostring.js            |  19 +++++
 benchmark/code/flatten/while-2.js             |  20 +++++
 benchmark/code/flatten/while-3.js             |  22 ++++++
 benchmark/code/flatten/while-4.js             |  23 ++++++
 benchmark/code/flatten/while.js               |  22 ++++++
 benchmark/code/flatten/with-depth.js          |  17 ++++
 benchmark/fixtures/large.js                   |   1 +
 benchmark/fixtures/medium.js                  |   1 +
 benchmark/fixtures/none.js                    |   1 +
 benchmark/fixtures/small.js                   |   1 +
 benchmark/fixtures/typical.js                 |   3 +
 benchmark/index.js                            |  42 ++++++++++
 bower.json                                    |  19 +++++
 package.json                                  |  46 +++++++++++
 test.js                                       |  60 ++++++++++++++
 34 files changed, 676 insertions(+)

diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..8e84acc
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,11 @@
+# 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
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4c7c31f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+.DS_Store
+node_modules
+coverage
+npm-debug.log
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..9de48f9
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,13 @@
+language: node_js
+
+notifications:
+  email:
+    on_success: never
+    on_failure: change
+
+node_js:
+  - "0.10"
+  - "4.0"
+  - "4.1"
+
+after_script: "npm install coveralls at 2 && cat ./coverage/lcov.info | coveralls"
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..983fbe8
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2014 Blake Embrey (hello at 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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..4c4588e
--- /dev/null
+++ b/README.md
@@ -0,0 +1,50 @@
+# 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
diff --git a/array-flatten.js b/array-flatten.js
new file mode 100644
index 0000000..c630d22
--- /dev/null
+++ b/array-flatten.js
@@ -0,0 +1,108 @@
+'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
+}
diff --git a/benchmark/README.md b/benchmark/README.md
new file mode 100644
index 0000000..ccda56e
--- /dev/null
+++ b/benchmark/README.md
@@ -0,0 +1,5 @@
+# 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.
diff --git a/benchmark/code/arguments/pass-as-arguments.js b/benchmark/code/arguments/pass-as-arguments.js
new file mode 100644
index 0000000..e827f57
--- /dev/null
+++ b/benchmark/code/arguments/pass-as-arguments.js
@@ -0,0 +1,7 @@
+var current = require('../../..').from
+
+module.exports = args
+
+function args () {
+  return current(arguments)
+}
diff --git a/benchmark/code/arguments/pass-as-for.js b/benchmark/code/arguments/pass-as-for.js
new file mode 100644
index 0000000..3c72b17
--- /dev/null
+++ b/benchmark/code/arguments/pass-as-for.js
@@ -0,0 +1,14 @@
+var 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)
+}
diff --git a/benchmark/code/arguments/pass-as-slice.js b/benchmark/code/arguments/pass-as-slice.js
new file mode 100644
index 0000000..f34d7ac
--- /dev/null
+++ b/benchmark/code/arguments/pass-as-slice.js
@@ -0,0 +1,8 @@
+var current = require('../../..').from
+var _slice = Array.prototype.slice
+
+module.exports = slice
+
+function slice () {
+  return current(_slice.call(arguments))
+}
diff --git a/benchmark/code/depth/current.js b/benchmark/code/depth/current.js
new file mode 100644
index 0000000..ab91f89
--- /dev/null
+++ b/benchmark/code/depth/current.js
@@ -0,0 +1 @@
+module.exports = require('../../..').depth
diff --git a/benchmark/code/depth/while.js b/benchmark/code/depth/while.js
new file mode 100644
index 0000000..c3a4a23
--- /dev/null
+++ b/benchmark/code/depth/while.js
@@ -0,0 +1,26 @@
+module.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
+}
diff --git a/benchmark/code/flatten/concat.js b/benchmark/code/flatten/concat.js
new file mode 100644
index 0000000..1b20361
--- /dev/null
+++ b/benchmark/code/flatten/concat.js
@@ -0,0 +1,11 @@
+module.exports = flatten
+
+function flatten (array) {
+  var result = Array.prototype.concat.apply([], array)
+
+  if (result.length === array.length) {
+    return result
+  }
+
+  return flatten(result)
+}
diff --git a/benchmark/code/flatten/current.js b/benchmark/code/flatten/current.js
new file mode 100644
index 0000000..e515817
--- /dev/null
+++ b/benchmark/code/flatten/current.js
@@ -0,0 +1 @@
+module.exports = require('../../..')
diff --git a/benchmark/code/flatten/for-value.js b/benchmark/code/flatten/for-value.js
new file mode 100644
index 0000000..3e2fd0e
--- /dev/null
+++ b/benchmark/code/flatten/for-value.js
@@ -0,0 +1,19 @@
+module.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
+}
diff --git a/benchmark/code/flatten/for.js b/benchmark/code/flatten/for.js
new file mode 100644
index 0000000..1fbe30e
--- /dev/null
+++ b/benchmark/code/flatten/for.js
@@ -0,0 +1,17 @@
+module.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
+}
diff --git a/benchmark/code/flatten/if-depth.js b/benchmark/code/flatten/if-depth.js
new file mode 100644
index 0000000..659e4bd
--- /dev/null
+++ b/benchmark/code/flatten/if-depth.js
@@ -0,0 +1,33 @@
+module.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
+}
diff --git a/benchmark/code/flatten/no-return.js b/benchmark/code/flatten/no-return.js
new file mode 100644
index 0000000..beccae7
--- /dev/null
+++ b/benchmark/code/flatten/no-return.js
@@ -0,0 +1,19 @@
+module.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)
+    }
+  }
+}
diff --git a/benchmark/code/flatten/reduce.js b/benchmark/code/flatten/reduce.js
new file mode 100644
index 0000000..610cc70
--- /dev/null
+++ b/benchmark/code/flatten/reduce.js
@@ -0,0 +1,11 @@
+module.exports = reduce
+
+function reduce (array) {
+  return array.reduce(function (acc, value) {
+    if (Array.isArray(value)) {
+      return acc.concat(reduce(value))
+    }
+
+    return acc.concat(value)
+  }, [])
+}
diff --git a/benchmark/code/flatten/tostring.js b/benchmark/code/flatten/tostring.js
new file mode 100644
index 0000000..9a3c82e
--- /dev/null
+++ b/benchmark/code/flatten/tostring.js
@@ -0,0 +1,19 @@
+var _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
+}
diff --git a/benchmark/code/flatten/while-2.js b/benchmark/code/flatten/while-2.js
new file mode 100644
index 0000000..20ee008
--- /dev/null
+++ b/benchmark/code/flatten/while-2.js
@@ -0,0 +1,20 @@
+module.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
+}
diff --git a/benchmark/code/flatten/while-3.js b/benchmark/code/flatten/while-3.js
new file mode 100644
index 0000000..8a45319
--- /dev/null
+++ b/benchmark/code/flatten/while-3.js
@@ -0,0 +1,22 @@
+module.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
+}
diff --git a/benchmark/code/flatten/while-4.js b/benchmark/code/flatten/while-4.js
new file mode 100644
index 0000000..f546478
--- /dev/null
+++ b/benchmark/code/flatten/while-4.js
@@ -0,0 +1,23 @@
+module.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
+}
diff --git a/benchmark/code/flatten/while.js b/benchmark/code/flatten/while.js
new file mode 100644
index 0000000..695bbd3
--- /dev/null
+++ b/benchmark/code/flatten/while.js
@@ -0,0 +1,22 @@
+module.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
+}
diff --git a/benchmark/code/flatten/with-depth.js b/benchmark/code/flatten/with-depth.js
new file mode 100644
index 0000000..52aa9a5
--- /dev/null
+++ b/benchmark/code/flatten/with-depth.js
@@ -0,0 +1,17 @@
+module.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
+}
diff --git a/benchmark/fixtures/large.js b/benchmark/fixtures/large.js
new file mode 100644
index 0000000..327a245
--- /dev/null
+++ b/benchmark/fixtures/large.js
@@ -0,0 +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 [...]
diff --git a/benchmark/fixtures/medium.js b/benchmark/fixtures/medium.js
new file mode 100644
index 0000000..1e7e5c7
--- /dev/null
+++ b/benchmark/fixtures/medium.js
@@ -0,0 +1 @@
+module.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']
diff --git a/benchmark/fixtures/none.js b/benchmark/fixtures/none.js
new file mode 100644
index 0000000..b028d80
--- /dev/null
+++ b/benchmark/fixtures/none.js
@@ -0,0 +1 @@
+module.exports = [1, 2, 3, 4, 5, 6, 7]
diff --git a/benchmark/fixtures/small.js b/benchmark/fixtures/small.js
new file mode 100644
index 0000000..303d345
--- /dev/null
+++ b/benchmark/fixtures/small.js
@@ -0,0 +1 @@
+module.exports = ['a', ['b', ['c', ['d']]], ['e', ['f']], 'h', ['i'], 'j', 'k']
diff --git a/benchmark/fixtures/typical.js b/benchmark/fixtures/typical.js
new file mode 100644
index 0000000..f42cbbd
--- /dev/null
+++ b/benchmark/fixtures/typical.js
@@ -0,0 +1,3 @@
+function noop () {}
+
+module.exports = [noop, noop, [noop, noop, noop], noop]
diff --git a/benchmark/index.js b/benchmark/index.js
new file mode 100644
index 0000000..c6eb383
--- /dev/null
+++ b/benchmark/index.js
@@ -0,0 +1,42 @@
+var 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]
+})
diff --git a/bower.json b/bower.json
new file mode 100644
index 0000000..6514566
--- /dev/null
+++ b/bower.json
@@ -0,0 +1,19 @@
+{
+  "name": "array-flatten",
+  "version": "2.0.0",
+  "homepage": "https://github.com/blakeembrey/array-flatten",
+  "authors": [
+    "{{{author}}}"
+  ],
+  "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"
+  ]
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..6bbb54d
--- /dev/null
+++ b/package.json
@@ -0,0 +1,46 @@
+{
+  "name": "array-flatten",
+  "version": "2.0.0",
+  "description": "Flatten nested arrays",
+  "main": "array-flatten.js",
+  "files": [
+    "array-flatten.js",
+    "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 at 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",
+    "pre-commit": "^1.0.7",
+    "standard": "^5.3.1"
+  }
+}
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..9588a58
--- /dev/null
+++ b/test.js
@@ -0,0 +1,60 @@
+/* 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'])
+    })
+  })
+})

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-array-flatten.git



More information about the Pkg-javascript-commits mailing list