All files / libs/lang assign.js

100% Statements 17/17
100% Branches 7/7
100% Functions 1/1
100% Lines 14/14
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57                1x 1x                                             1x 186x     183x   183x 196x   196x 487x       196x 196x 196x 2x         183x    
/**
 * @module      libs/lang/assign
 * @createdAt   2016-06-30
 *
 * @copyright   Copyright (c) 2016 Zhonglei Qiu
 * @license     Licensed under the MIT license.
 */
 
var hasOwnProperty = Object.prototype.hasOwnProperty
var propIsEnumerable = Object.prototype.propertyIsEnumerable
 
/**
 *
 * 原生的 Object.assign 的一个 polyfill
 *
 * Node v4 以前基本上都不支持 Object.assign 这个方法,此方法是 sindresorhus 写
 * 的 object-assign 的一个简化版,源版本还对原生的 Object.assign 做了 bug detect
 * ( Detect buggy property enumeration order in older V8 versions ),我这个
 * 版本相对简单点
 *
 * @param     {Object} target     要赋值到的对象
 * @param     {...Object} source  源对象,可以有多个
 * @throws    {TypeError}         如果第一个参数是 null 或者 undefined
 * @return    {Object}
 *
 * @example
 * assign({}, {a: '1', b: 2}, null, true, {a: 1})
 *
 * @see       [object-assign@4.1.0]{@link https://github.com/sindresorhus/object-assign/tree/v4.1.0}
 * @author    Zhongle Qiu
 * @since     2.0.0
 */
module.exports = function(target, source) {
  if (target == null) throw new TypeError('Object.assign cannot be called with null or undefined')
 
  var i, j, k, from, symbols
  var to = Object(target)
 
  for (i = 1; i < arguments.length; i++) {
    from = Object(arguments[i])
 
    for (k in from) {
      if (hasOwnProperty.call(from, k)) to[k] = from[k]
    }
 
    /* istanbul ignore else */
    if (Object.getOwnPropertySymbols) {
      symbols = Object.getOwnPropertySymbols(from)
      for (j = 0; j < symbols.length; j++) {
        if (propIsEnumerable.call(from, symbols[j])) to[symbols[j]] = from[symbols[j]]
      }
    }
  }
 
  return to
}