Start sharing components as a team!Share components as a team!Join Bit to build your applications faster.Get Started Free

_base-flatten

v4.17.11arrow_drop_down
v4.17.11
STATUS
Passing
DOWNLOADS
925
LICENSE
MIT
VISIBILITY
Public
PUBLISHED
A year ago
SIZE
1 KB
1 contributor
Install _base-flatten as a package?
Copied
npm i @bit/lodash.lodash.internal._base-flatten
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.dev
_baseFlatten.js
38 Lines(22 sloc)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import arrayPush from './_arrayPush.js';
import isFlattenable from './_isFlattenable.js';

/**
 * The base implementation of `_.flatten` with support for restricting flattening.
 *
 * @private
 * @param {Array} array The array to flatten.
 * @param {number} depth The maximum recursion depth.
 * @param {boolean} [predicate=isFlattenable] The function invoked per iteration.
 * @param {boolean} [isStrict] Restrict to values that pass `predicate` checks.
 * @param {Array} [result=[]] The initial result value.
 * @returns {Array} Returns the new flattened array.
 */
function baseFlatten(array, depth, predicate, isStrict, result) {
  var index = -1,
      length = array.length;

  predicate || (predicate = isFlattenable);
  result || (result = []);

  while (++index < length) {
    var value = array[index];
    if (depth > 0 && predicate(value)) {
      if (depth > 1) {
        // Recursively flatten arrays (susceptible to call stack limits).
        baseFlatten(value, depth - 1, predicate, isStrict, result);
      } else {
        arrayPush(result, value);
      }
    } else if (!isStrict) {
      result[result.length] = value;
    }
  }
  return result;
}

export default baseFlatten;