diff options
Diffstat (limited to 'node_modules/flatten')
| -rw-r--r-- | node_modules/flatten/LICENSE | 21 | ||||
| -rw-r--r-- | node_modules/flatten/README.md | 34 | ||||
| -rw-r--r-- | node_modules/flatten/index.js | 23 | ||||
| -rw-r--r-- | node_modules/flatten/package.json | 21 | ||||
| -rw-r--r-- | node_modules/flatten/test.js | 25 |
5 files changed, 124 insertions, 0 deletions
diff --git a/node_modules/flatten/LICENSE b/node_modules/flatten/LICENSE new file mode 100644 index 00000000..54a689b8 --- /dev/null +++ b/node_modules/flatten/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Joshua Holbrook + +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/node_modules/flatten/README.md b/node_modules/flatten/README.md new file mode 100644 index 00000000..7b0a3b69 --- /dev/null +++ b/node_modules/flatten/README.md @@ -0,0 +1,34 @@ +# flatten + +A tiny utility to flatten arrays of arrays (of arrays, etc., recursively, infinitely or to an optional depth) into a single array of non-arrays. + +## example: + +```js +> var flatten = require('flatten'); +undefined +> flatten([1, [2, 3], [4, 5, 6], [7, [8, 9]], 10]) +[ 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10 ] +> flatten([1, [2, [3, [4, [5]]]]], 2) +[ 1, + 2, + 3, + [ 4, [ 5 ] ] ] +``` + +## install: + + npm install flatten + +## license: + +MIT/X11. diff --git a/node_modules/flatten/index.js b/node_modules/flatten/index.js new file mode 100644 index 00000000..a9bd9b8c --- /dev/null +++ b/node_modules/flatten/index.js @@ -0,0 +1,23 @@ +module.exports = function flatten(list, depth) { + depth = (typeof depth == 'number') ? depth : Infinity; + + if (!depth) { + if (Array.isArray(list)) { + return list.map(function(i) { return i; }); + } + return list; + } + + return _flatten(list, 1); + + function _flatten(list, d) { + return list.reduce(function (acc, item) { + if (Array.isArray(item) && d < depth) { + return acc.concat(_flatten(item, d + 1)); + } + else { + return acc.concat(item); + } + }, []); + } +}; diff --git a/node_modules/flatten/package.json b/node_modules/flatten/package.json new file mode 100644 index 00000000..855f892f --- /dev/null +++ b/node_modules/flatten/package.json @@ -0,0 +1,21 @@ +{ + "author": "Joshua Holbrook <josh.holbrook@gmail.com> (http://jesusabdullah.net)", + "name": "flatten", + "description": "Flatten arbitrarily nested arrays into a non-nested list of non-array items", + "version": "1.0.2", + "repository": { + "type": "git", + "url": "git://github.com/jesusabdullah/node-flatten.git" + }, + "main": "./index.js", + "scripts": { + "test": "node ./test.js" + }, + "license": "MIT", + "dependencies": {}, + "devDependencies": {}, + "optionalDependencies": {}, + "engines": { + "node": "*" + } +} diff --git a/node_modules/flatten/test.js b/node_modules/flatten/test.js new file mode 100644 index 00000000..385c27fa --- /dev/null +++ b/node_modules/flatten/test.js @@ -0,0 +1,25 @@ +var flatten = require('./index'), + util = require('util'), + assert = require('assert'); + +[ + [ [1, [ 2, 3]], [1, [2, 3]], 0], + [ [1, 2, 3 ], [1, 2, 3] ], + [ ['a', ['b', ['c']]], ['a', 'b', 'c'] ], + [ [2, [4, 6], 8, [[10]]], [2, 4, 6, 8, 10] ], + [ [1, [2, [3, [4, [5]]]]], [1, 2, 3, [4, [5]]], 2 ] // depth of 2 +].forEach(function (t) { + assert.deepEqual(flatten(t[0], t[2]), t[1], + util.format('☠☠☠☠☠☠☠☠☠ %s ☠☠☠☠☠☠☠☠☠', formatCall(t)) + ); + console.log('✓ %s', formatCall(t)); +}); + +function formatCall(t) { + if (typeof t[2] === 'undefined') { + return util.format('`flatten(%j) == %j`', t[0], t[1]); + } + else { + return util.format('`flatten(%j, %j) == %j`', t[0], t[2], t[1]); + } +} |
