time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

_baseFlatten.js (1201B)


      1 var arrayPush = require('./_arrayPush'),
      2     isFlattenable = require('./_isFlattenable');
      3 
      4 /**
      5  * The base implementation of `_.flatten` with support for restricting flattening.
      6  *
      7  * @private
      8  * @param {Array} array The array to flatten.
      9  * @param {number} depth The maximum recursion depth.
     10  * @param {boolean} [predicate=isFlattenable] The function invoked per iteration.
     11  * @param {boolean} [isStrict] Restrict to values that pass `predicate` checks.
     12  * @param {Array} [result=[]] The initial result value.
     13  * @returns {Array} Returns the new flattened array.
     14  */
     15 function baseFlatten(array, depth, predicate, isStrict, result) {
     16   var index = -1,
     17       length = array.length;
     18 
     19   predicate || (predicate = isFlattenable);
     20   result || (result = []);
     21 
     22   while (++index < length) {
     23     var value = array[index];
     24     if (depth > 0 && predicate(value)) {
     25       if (depth > 1) {
     26         // Recursively flatten arrays (susceptible to call stack limits).
     27         baseFlatten(value, depth - 1, predicate, isStrict, result);
     28       } else {
     29         arrayPush(result, value);
     30       }
     31     } else if (!isStrict) {
     32       result[result.length] = value;
     33     }
     34   }
     35   return result;
     36 }
     37 
     38 module.exports = baseFlatten;