1.0.2 • Published 9 years ago

deep-flat v1.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

deep-flat

Flatten an arrays of arrays ad infinitum

Usage

deep-flat exports a single function accepting two parameters:

(array = [], depth = 1) => [...] (flattened array)

depth dictates how many times we flatten the array, if the depth is greater than the amount of levels no error will be thrown, the lowest possible array level will be returned.

Example

var flat = require('deep-flat')
console.log(flat([[[[1, 2, 3]]]], 3))

Implementation

This is a zero production dependencies module.

The code is tiny:

module.exports =
  (array = [], depth = 1) => 
    Array.apply(0, {length: depth})
      .reduce(Function.apply.bind([].concat, []), array)

Two interesting techniques are employed here:

Transpilation

The source is ES2015/ES6 but the entrypoint (index.js) is transpiled from source(index.es6) into ES5 compatible code.

Limits

Supplying depth as Infinity or Number.MAX_VALUE will result in the original array being returned.

The amount of times the array can be flattened is limited by maximum stack size, passing in a very large depth such as 1e6 will cause a stack overflow.