1625行,解开 underscore.js 的面纱 - 第五章

每次小章节的开题都烦恼写什么好,因此直接接下文 (~o▔▽▔)~o o~(▔▽▔o~) 。数组

_.first = _.head = _.take = function(array, n, guard) {
    if (array == null) return void 0;
    if (n == null || guard) return array[0];
    return _.initial(array, array.length - n);
  };

_.first 用于返回数组中从左到右指定数目 n 的结果集,传入 array、n、guard 三个参数中 array 只能为 Array,当 n = null 时返回数组第一个元素,这里须要讲解的是 _.initial 函数是与 _.first 彻底对立的函数,它用于返回数组中从左到右指定数目 Array.length - n 的结果集。app

_.initial = function(array, n, guard) {
    return slice.call(array, 0, Math.max(0, array.length - (n == null || guard ? 1 : n)));
  };

那么它是如何实现的呢,依然是应用数组 Array 的 Array.prototype.slice.call(array, start, end); 实现,这个概念请参看:Array.prototype.slice()函数

_.last = function(array, n, guard) {
    if (array == null) return void 0;
    if (n == null || guard) return array[array.length - 1];
    return _.rest(array, Math.max(0, array.length - n));
  };

_.last 是返回数组中从右到左指定数目 n 的结果集。实现原理依旧 Array.prototype.slice.call(array, start, end);this

_.rest = _.tail = _.drop = function(array, n, guard) {
    return slice.call(array, n == null || guard ? 1 : n);
  };

_.rest 用于返回数组中从右到左指定数目 Array.length - n 的结果集。prototype

_.compact = function(array) {
    return _.filter(array, Boolean);
  };

_.compact,我喜欢称它为过滤器,过滤坏的数据,那么什么样的数据为坏数据呢,咱们能够看下 _.filter,前面讲 _.filter 接收三个参数 obj, predicate, context,其中 predicate 依旧由 cb 处理,那么这里 _.compact 传的 predicate 是 Boolean = function Boolean() { [native code] },这是一个 JAVASCRIPT 内置的函数用于 Boolean 判断,咱们能够参考 BooleanBoolean data type。那么重点来了,什么的值会是 Boolean 函数断言为 false 呢,答案就是 false, 0, "", null, undefined, NaN,这个可不是我瞎说或者 copy 官网,我是有理论依据的(vˍv),当当当,看这里 Truthyrest

var flatten = function(input, shallow, strict, output) {
    output = output || [];
    var idx = output.length;
    for (var i = 0, length = getLength(input); i < length; i++) {
      var value = input[i];
      if (isArrayLike(value) && (_.isArray(value) || _.isArguments(value))) {
        if (shallow) {
          var j = 0, len = value.length;
          while (j < len) output[idx++] = value[j++];
        } else {
          flatten(value, shallow, strict, output);
          idx = output.length;
        }
      } else if (!strict) {
        output[idx++] = value;
      }
    }
    return output;
  };

flatten 传入四个参数,input, shallow, strict, output,其中咱们能够经过 flatten 内部的 for 循环中 length = getLength(input); 知道 input 数据类型为 Array。而后经过对 shallow, strict 两个 Boolean 型变量的控制执行相应的数据处理方式。好比 shallow 为 false 会一直执行 flatten(value, shallow, strict, output);output[idx++] = value; 对多维数组进行一维数组的转换。code

_.flatten = function(array, shallow) {
    return flatten(array, shallow, false);
  };

_.flatten 函数用于对多维度数组进行扁平化处理,即将任意维数的数组转换为一维数组,上面已经说到了这个的实现方式。对象

_.without = restArgs(function(array, otherArrays) {
    return _.difference(array, otherArrays);
  });

_.without 用于删除数组中的某些特定元素。它由 _.difference 构成。排序

_.uniq = _.unique = function(array, isSorted, iteratee, context) {
    if (!_.isBoolean(isSorted)) {
      context = iteratee;
      iteratee = isSorted;
      isSorted = false;
    }
    if (iteratee != null) iteratee = cb(iteratee, context);
    var result = [];
    var seen = [];
    for (var i = 0, length = getLength(array); i < length; i++) {
      var value = array[i],
          computed = iteratee ? iteratee(value, i, array) : value;
      if (isSorted) {
        if (!i || seen !== computed) result.push(value);
        seen = computed;
      } else if (iteratee) {
        if (!_.contains(seen, computed)) {
          seen.push(computed);
          result.push(value);
        }
      } else if (!_.contains(result, value)) {
        result.push(value);
      }
    }
    return result;
  };

_.uniq 是数组去重,实现原理是若是 isSorted 及后面元素省略,那么 _.uniq 简化为:ip

_.uniq = _.unique = function(array) {
        context = null;
        iteratee = null;
        isSorted = false;
        var result = [];
        var seen = [];
        for (var i = 0, length = getLength(array); i < length; i++) {
          var value = array[i];
          if (!_.contains(result, value)) {
            result.push(value);
          }
        }
        return result;
      };

咱们能够看到其核心代码只有 if (!_.contains(result, value)),用于判断数组中是否包含其值,以此达到数组去重的目的。是这里我想说的是 context、iteratee、isSorted 变成了未定义的参数,做者没有处理它会在这种状况下变成全局污染。
接下来咱们说一下传入 array, isSorted, iteratee 三个参数的状况,咱们已经知道 isSorted 默认为 false,表明去重,那么若是定义 isSorted 为 true 则就是不去重,若是 isSorted 是回调函数,则默认内部从新定义 isSorted 为 false,并将回调函数赋给 iteratee,而后很悲剧的 iteratee 参数依然是没有 var 过的,又污染了啊(‧_‧?) 。大体就是这酱了。

_.union = restArgs(function(arrays) {
    return _.uniq(flatten(arrays, true, true));
  });

_.union 对多个一维数组进行并运算,实际上就是增强版的 _.uniq。在代码中做者首先用 flatten 函数处理参数,以前咱们说到 flatten 是用于多个多维数组进行一位转换,实际上就是要把 arrays 转换。这里有同窗可能问道 flatten 直接收一个 Array 剩下的值是 Boolean 啊,那么使用 _.union 的时候是一次性传入 n 个 Array(如这样:_.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);),说不通啊。因此我要说的是 restArgs 这个函数,将传入参数转换为一个数组进行 func.apply(this, args) 到 restArgs 的回调函数 function(arrays) {} 中,以此达到 flatten 函数 arrays 接到的是一个一维数组的集合。最后经过 _.uniq 函数对数组进行处理。

_.intersection = function(array) {
    var result = [];
    var argsLength = arguments.length;
    for (var i = 0, length = getLength(array); i < length; i++) {
      var item = array[i];
      if (_.contains(result, item)) continue;
      var j;
      for (j = 1; j < argsLength; j++) {
        if (!_.contains(arguments[j], item)) break;
      }
      if (j === argsLength) result.push(item);
    }
    return result;
  };

_.intersection 用于获取多个一维数组的相同数据的集合,即交集。又是一番对 Array 的 for 啊 for 啊 for,而后 if 而后 push,相信你们这么聪明,不用多说了,由于这个函数很直白,没太多可讲的。

_.difference = restArgs(function(array, rest) {
    rest = flatten(rest, true, true);
    return _.filter(array, function(value){
      return !_.contains(rest, value);
    });
  });

_.difference 函数的实现与 _.union 相似,都是经过 restArgs 对 n 个传参进行数组转变,而后赋给回调函数,区别在于这个函数可能更加复杂,它首先 restArgs 回调写了两个传参 array, rest,但实际上 rest 是 undefined,以后在回调内部给 rest 赋值为 flatten 函数处理以后的数组,即扁平化后的一维数组。由于 restArgs 函数只有一个 function 回调,因此内部执行 return func.call(this, arguments[0], rest);,返回的是第一个数组和其余数组的集合,即 array, rest

_.unzip = function(array) {
    var length = array && _.max(array, getLength).length || 0;
    var result = Array(length);
    for (var index = 0; index < length; index++) {
      result[index] = _.pluck(array, index);
    }
    return result;
  };

_.unzip 用于将多个数组中元素按照数组下标进行拼接,只接收一个二维数组,返回值一样是一个二维数组。

_.zip = restArgs(_.unzip);

_.zip_.unzip 不一样之处在于它能够传入不定的一维数组参数而后经过 restArgs 函数转换实现 _.unzip 传参的效果。

_.object = function(list, values) {
    var result = {};
    for (var i = 0, length = getLength(list); i < length; i++) {
      if (values) {
        result[list[i]] = values[i];
      } else {
        result[list[i][0]] = list[i][1];
      }
    }
    return result;
  };

_.object 用于将数组转换成对象。

var createPredicateIndexFinder = function(dir) {
    return function(array, predicate, context) {
      predicate = cb(predicate, context);
      var length = getLength(array);
      var index = dir > 0 ? 0 : length - 1;
      for (; index >= 0 && index < length; index += dir) {
        if (predicate(array[index], index, array)) return index;
      }
      return -1;
    };
  };

createPredicateIndexFinder 这个函数适用于生成 _.findIndex 之类的函数,当咱们看到 return index; 的是后就已经能够知道,其核心是与数组下标有关。

_.findIndex = createPredicateIndexFinder(1);

_.findIndex 函数由 createPredicateIndexFinder 包装而成,咱们能够看到它的默认传值是 1,也就是:

_.findIndex = function(array, predicate, context) {
       predicate = cb(predicate, context);
       for (var index >= 0; index < getLength(array); index += 1) {
           if (predicate(array[index], index, array)) return index;
       }
       return -1;
   };

其中 predicate 是回调函数接收 array[index], index, array 三个值用于 Boolean 判断,最终结果是返回符合规则的数组中的第一条数据的数组下标。

_.findLastIndex = createPredicateIndexFinder(-1);

_.findLastIndex 顾名思义就是返回数组中符合规则的最后一条数据的下标,说直白了就是遍历数组的时候从右往左而已。

_.sortedIndex = function(array, obj, iteratee, context) {
    iteratee = cb(iteratee, context, 1);
    var value = iteratee(obj);
    var low = 0, high = getLength(array);
    while (low < high) {
      var mid = Math.floor((low + high) / 2);
      if (iteratee(array[mid]) < value) low = mid + 1; else high = mid;
    }
    return low;
  };

_.sortedIndex 官网解释说 使用二分查找肯定value在list中的位置序号,value按此序号插入能保持list原有的排序。,很绕口,这里咱们须要注意的是若是进行 _.sortedIndex 查找这个特定的序列号,必定要事先将 array 进行按需排序。

var createIndexFinder = function(dir, predicateFind, sortedIndex) {
    return function(array, item, idx) {
      var i = 0, length = getLength(array);
      if (typeof idx == 'number') {
        if (dir > 0) {
          i = idx >= 0 ? idx : Math.max(idx + length, i);
        } else {
          length = idx >= 0 ? Math.min(idx + 1, length) : idx + length + 1;
        }
      } else if (sortedIndex && idx && length) {
        idx = sortedIndex(array, item);
        return array[idx] === item ? idx : -1;
      }
      if (item !== item) {
        idx = predicateFind(slice.call(array, i, length), _.isNaN);
        return idx >= 0 ? idx + i : -1;
      }
      for (idx = dir > 0 ? i : length - 1; idx >= 0 && idx < length; idx += dir) {
        if (array[idx] === item) return idx;
      }
      return -1;
    };
  };

createIndexFinder,看命名就能够知道依旧与数组下标有关。咱们能够看到数据处理的一个关键是 idx,它多是一个数字也多是一个字符串或者对象。当它是 Number 的时候遵循 idx 是限制查找范围的数组下标规则,若是它是其余的则使用 sortedIndex 函数查找到 idx 的数组下标再岁数组查找范围进行限定。

_.indexOf = createIndexFinder(1, _.findIndex, _.sortedIndex);

_.indexOf 函数与 _.findIndex 区别在于 _.findIndex 须要查找的数据可能存在于数组中也可能不存在数组中,而 _.indexOf 的 predicateFind 必定是数组中的元素。同时也用 array, item, idx 三个参数中的 idx 限定开始查找的范围。

_.lastIndexOf = createIndexFinder(-1, _.findLastIndex);

_.lastIndexOf 查找数组中的符合结果的最后条数据的数组下标。

_.range = function(start, stop, step) {
    if (stop == null) {
      stop = start || 0;
      start = 0;
    }
    if (!step) {
      step = stop < start ? -1 : 1;
    }
    var length = Math.max(Math.ceil((stop - start) / step), 0);
    var range = Array(length);
    for (var idx = 0; idx < length; idx++, start += step) {
      range[idx] = start;
    }
    return range;
  };

_.range 用于生成一个有序的数组,经过 start 和 stop 限定数组范围,经过 step 限定差值。

_.chunk = function(array, count) {
    if (count == null || count < 1) return [];
    var result = [];
    var i = 0, length = array.length;
    while (i < length) {
      result.push(slice.call(array, i, i += count));
    }
    return result;
  };

_.chunk,这个函数目前官网并无释义,估计做者忘记加进去了吧,咱们看到 chunk 很天然的就应该想到 stream 的概念,这里也差很少,只不过拆分的不限定是 Buffer 数组, _.chunk 传入两个参数 Array 以及 count,其中 count 用来限定拆分出的每一组的大小,举个栗子:

_.chunk([1,2,3,4,5,6,7,8,9], 1)
   [[1],[2],[3],[4],[5],[6],[7],[8],[9]]
   _.chunk([1,2,3,4,5,6,7,8,9], 2)
   [[1,2],[3,4],[5,6],[7,8],[9]]

然而但凡对 stream 的概念有所了解都知道这个函数吧,没什么特殊的地方。

相关文章
相关标签/搜索