All files / arr flatten.js

91.67% Statements 11/12
75% Branches 3/4
100% Functions 1/1
90% Lines 9/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                    1x     4x 4x 7x 7x     7x 7x   4x     1x  
/**
 * 数组扁平化
 * @method arr/flatten
 * @param {array} arr 要操作的数组
 * @returns {array} 经过扁平化处理的数组
 * @example
 * var $flatten = require('@spore-ui/kit/packages/arr/flatten');
 * console.info($flatten([1, [2,3], [4,5]])); // [1,2,3,4,5]
 */
 
var $type = require('../obj/type');
 
function flatten(arr) {
  var array = [];
  for (var i = 0, l = arr.length; i < l; i += 1) {
    var type = $type(arr[i]);
    Iif (type === 'null') {
      continue;
    }
    var extraArr = type === 'array' ? flatten(arr[i]) : arr[i];
    array = array.concat(extraArr);
  }
  return array;
}
 
module.exports = flatten;