Lodash 4.0.0 更新文档

来源:互联网 发布:google大数据 编辑:程序博客网 时间:2024/06/05 06:50

兼容性警告

  • 专注于 npm,移除 Bower & Component 包支持
  • 放弃对 IE 6-8 的支持
    – Use es5-shim, & optionally es6-shim, to enable support
    – 可以使用 es5-shim, & optionally es6-shim, 以继续开启支持
  • .forEach, .forIn, .forOwn, & .times 隐式结束链式调用队列
var wrapped = _([1, 2, 3]);// 在 3.10.1wrapped.forEach(function(n) { console.log(n); });// → returns the lodash wrapper without logging until `value` is called// → 在 `value` 调用之前返回 lodash 包装器(并不会打印记录)wrapped.forEach(function(n) { console.log(n); }).value();// → logs each value from left to right and returns the array// → 从左到右记录每个值,并返回该数组// 在 4.0.0wrapped.forEach(function(n) { console.log(n); });// → logs each value from left to right and returns the array// → 从左到右记录每个值,并返回该数组
  • 移除模块路径的分类名
// in 3.10.1var chunk = require('lodash/array/chunk');// in 4.0.0var chunk = require('lodash/chunk');
  • 移除 .pluck,使用 .map 迭代器简写代替
var objects = [{ 'a': 1 }, { 'a': 2 }];// in 3.10.1_.pluck(objects, 'a'); // → [1, 2]_.map(objects, 'a'); // → [1, 2]// in 4.0.0_.map(objects, 'a'); // → [1, 2]
  • 大多数方法移除 thisArg
var objects = [{ 'a': 1 }, { 'a': 2 }];var context = { 'b': 5 };function callback(item) {  return item.a + this.b;}// in 3.10.1_.map(objects, callback, context);// in 4.0.0_.map(objects, _.bind(callback, context));
  • .max & .min 拆分成 .maxBy & .minBy
var array = [1, 2, 3],    objects = [{ 'a': 1 }, { 'a': 2 }];// in 3.10.1_.max(array); // → 3_.max(objects, 'a'); // → { 'a': 2 }_.min(array); // → 1_.min(objects, 'a'); // → { 'a': 1 }// in 4.0.0_.max(array); // → 3_.maxBy(objects, 'a'); // → { 'a': 2 }_.min(array); // → 1_.minBy(objects, 'a'); // → { 'a': 1 }
  • 移除方法
    1. 移除 _.support
    2. 移除 .findWhere,使用 .find 迭代器简写代替
    3. 移除 .where,使用 .filter 迭代器简写代替
    4. 移除 _.pluck,使用 _map 迭代器简写代替
  • 方法名变更
    1. .first 更名为 .head
    2. .indexBy 更名为 .keyBy
    3. .invoke 更名为 .invokeMap
    4. .modArgs 更名为 .overArgs
    5. .padLeft & .padRight 更名为 padStart & .padEnd
    6. .pairs 更名为 .toPairs
    7. .rest 更名为 .tail
    8. .restParam 更名为 .rest
    9. .sortByOrder 更名为 .orderBy
    10. .trimLeft & .trimRight 更名为 .trimStart & .trimEnd
    11. .trunc 更名为 .truncate
  • 拆分方法
    1. .indexOf & .lastIndexOf 拆分出 .sortedIndexOf & .sortedLastIndexOf
    2. .max & .min 拆分出 .maxBy & .minBy
    3. .omit & .pick 拆分出 .omitBy & .pickBy
    4. .sample 拆分出 .sampleSize
    5. .sortedIndex 拆分出 .sortedIndexBy
    6. .sortedLastIndex 拆分出 .sortedLastIndexBy
    7. .uniq 拆分出 .sortedUniq, .sortedUniqBy, & .uniqBy
  • .sortByAll 并入 .sortBy
  • 变更 _.at 的类别为 “Object”
  • 变更 _.bindAll 的类别为 Utility
  • 令 “By” 方法的迭代器只提供一个参数
  • 令 _.capitalize 会转换第一个字符为大写 & 其他字符为小写
  • 令 _.functions 返回自有方法的方法名名
  • 令 _.words 默认可链式调用
  • .clone & .flatten 移除 isDeep 参数
  • 当未指定方法名时,移除_.bindAll 绑定所有方法的支持
  • .before & .after 移除 func -第一个参数前面

低风险兼容性警告

  • .debounce, .mixin, & _.throttle 移除布尔值 options 参数支持
  • _.orderBy 移除布尔值 orders 参数的支持
  • .max, .min, & _.sum 只支持数组
  • _.template 移除传统 options 参数签名
  • 简化 _.escapeRegExp,以向已停止的 ES7 提案对齐

明显的变更

  • 4 kB (gzipped) core build (65 个方法; Backbone v1.2.4 compatible)

.assignIn, .before, .bind, .chain, .clone, .compact, _.concat,
.create, .defaults, .defer, .delay, .each, .escape, _.every,
.filter, .find, .first, .flatten, .flattenDeep, .forEach,
.has, .head, .identity, .indexOf, .invokeMap, .isArguments,
.isArray, .isBoolean, .isDate, .isEmpty, .isEqual, .isFinite,
.isFunction, .isNaN, .isNull, .isNumber, .isObject, .isRegExp,
.isString, .isUndefined, .iteratee, .keys, .last, .map,
.max, .min, .mixin, .negate, .noConflict, .noop, _.now,
.once, .pick, .reduce, .result, .size, .slice, _.some,
.sortBy, .tap, .thru, .toArray, .uniqueId, .value, & _.values

  • 新增 80 个方法
    23 个 array 方法:

.concat, .differenceBy, .differenceWith, .flatMap, .fromPairs, .intersectionBy,
.intersectionWith, .join, .pullAll, .pullAllBy, .reverse, .sortedIndexBy, _.sortedIndexOf,
.sortedLastIndexBy, .sortedLastIndexOf, .sortedUniq, .sortedUniqBy, _.unionBy,
.unionWith, .uniqBy, .uniqWith, .xorBy, & _.xorWith

  • 18 个 lang 方法:

.cloneDeepWith, .cloneWith, .eq, .isArrayLike, .isArrayLikeObject, .isEqualWith, _.isInteger,
.isLength, .isMatchWith, .isNil, .isObjectLike, .isSafeInteger, .isSymbol, _.toInteger,
.toLength, .toNumber, .toSafeInteger, & .toString

  • 13 个object :

.assignIn, .assignInWith, .assignWith, .functionsIn, .hasIn, .invoke, _.mergeWith,
.omitBy, .pickBy, .setWith, .toPairs, .toPairsIn, & .unset

  • 8 个 string 方法:

.lowerCase, .lowerFirst, .replace, .split, .upperCase, .upperFirst, .toLower, & .toUpper
- 8 个 utility 方法:

.cond, .conforms, .nthArg, .over, .overEvery, .overSome, .rangeRight, & .toPath
- 4 个 math 方法:

.maxBy, .mean, .minBy, & .sumBy
- 2 个 function 方法:

.flip & .unary
- 2 个 number 方法:

.clamp & .subtract
- 1 个 chain 方法:

_#next
- 1 个 collection 方法:

_.sampleSize
- 添加 3 别名
1. .extend 作为 .assignIn 的别名
2. .extendWith 作为 .assignInWith 的别名
3. .first 作为 .head 的别名
- 移除 17 个别名

.all, .any, .backflow, .callback, .collect, .compose, _.contains,
.detect, .foldl, .foldr, .include, .inject, .methods, _.object,
.#run, .select, & _.unique

  • 性能优化
    1. 开启 .at, .find & _.findLast 的快速合并机制
    2. 优化匹配方法,避免如果 object 和 source 相同时进行深度爬行
    3. 优化循环引用搜索
    4. 优化 _.isEqual,避免当数组或对象拥有不同长度时的堆栈爬行
  • 支持 Emoji
    1. Added support for astral symbols, combining diacritical marks, dingbats,
      regional indicator symbols, unicode modifiers, variation selectors, &
      zero-width-joiners to string methods
  • 功能性改进
    1. 添加 .cond, .conforms, .flip, .nthArg, .over, .overEvery, .overSome, & .unary
    2. 将 lodash-fp 移动到 lodash,使用 require(‘lodash/fp’) 作为不可变 iteratee-first data-last 方法

其他变更

  • 为 _.memoize.Cache 添加 clear 方法
  • 为防反跳函数和节流阀函数添加 flush 方法
  • .clone, .isEqual, & _.toArray 支持 ES6 映射、集合 & 标记
  • _.isEqual 支持数组缓存
  • _.toArray 支持转换迭代器
  • _.zipObject 支持深度路径
  • Changed UMD to export to window or self when available regardless of other exports
  • .flow & .flowRight 现在可以接受一个函数数组
  • 确保 “Collection” 方法能将函数视为对象
  • 确保防反跳方法 cancel 能够清除 args & thisArg 引用
  • 确保 .add, .subtract, & _.sum 不会跳过 NaN 值
  • 确保 .assign, .defaults, & _.merge 强制将 object 值转换为对象
  • 确保使用 new 操作符调用时 _.bindKey 绑定的函数时调用 object[key]
  • 确保 _.clone 能够将生成器视为函数
  • 确保 _.clone 能够根据源对象的 [[Prototype]] 进行克隆
  • 确保 _.defaults 能够根据 Object.prototype 分配属性
  • 确保 _.defaultsDeep 不能将字符串并入数组
  • 确保 .defaultsDeep & .merge 不会修改源对象
  • 确保 _.defaultsDeep 能够循环引用
  • 确保 _.isFunction 对生成器函数能够返回 true
  • 确保 _.keys 在 Safari 9 的严格模式下将跳过 “length” 属性
  • 确保 _.merge 能够直接布置类型数组
  • 确保 _.merge 不能将字符串转换为数组
  • 确保 _.merge 能够将纯粹对象合并到非纯粹对象中
  • 确保 _#plant 的克隆队列充值迭代器的数据
  • 确保 _.random min 大于 max 时交换 min & max
  • 确保 _.range preserves the sign of start of -0
  • 确保 .reduce & .reduceRight 在数组分支中使用 getIteratee
  • 修正 _.floor 带 precision 参数时四舍五入的问题
  • 使 _(…) 成为一个迭代器 & 可迭代的对象
  • 使 .drop, .take, & 从右迭代的方法强制将 n 由 undefined 转换为 0
原创粉丝点击