暂无描述

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict';
  2. var hasOwn = Object.prototype.hasOwnProperty;
  3. var toStr = Object.prototype.toString;
  4. var isArray = function isArray(arr) {
  5. if (typeof Array.isArray === 'function') {
  6. return Array.isArray(arr);
  7. }
  8. return toStr.call(arr) === '[object Array]';
  9. };
  10. var isPlainObject = function isPlainObject(obj) {
  11. if (!obj || toStr.call(obj) !== '[object Object]') {
  12. return false;
  13. }
  14. var hasOwnConstructor = hasOwn.call(obj, 'constructor');
  15. var hasIsPrototypeOf = obj.constructor && obj.constructor.prototype && hasOwn.call(obj.constructor.prototype, 'isPrototypeOf');
  16. // Not own constructor property must be Object
  17. if (obj.constructor && !hasOwnConstructor && !hasIsPrototypeOf) {
  18. return false;
  19. }
  20. // Own properties are enumerated firstly, so to speed up,
  21. // if last one is own, then all properties are own.
  22. var key;
  23. for (key in obj) { /**/ }
  24. return typeof key === 'undefined' || hasOwn.call(obj, key);
  25. };
  26. module.exports = function extend() {
  27. var options, name, src, copy, copyIsArray, clone;
  28. var target = arguments[0];
  29. var i = 1;
  30. var length = arguments.length;
  31. var deep = false;
  32. // Handle a deep copy situation
  33. if (typeof target === 'boolean') {
  34. deep = target;
  35. target = arguments[1] || {};
  36. // skip the boolean and the target
  37. i = 2;
  38. }
  39. if (target == null || (typeof target !== 'object' && typeof target !== 'function')) {
  40. target = {};
  41. }
  42. for (; i < length; ++i) {
  43. options = arguments[i];
  44. // Only deal with non-null/undefined values
  45. if (options != null) {
  46. // Extend the base object
  47. for (name in options) {
  48. src = target[name];
  49. copy = options[name];
  50. // Prevent never-ending loop
  51. if (target !== copy) {
  52. // Recurse if we're merging plain objects or arrays
  53. if (deep && copy && (isPlainObject(copy) || (copyIsArray = isArray(copy)))) {
  54. if (copyIsArray) {
  55. copyIsArray = false;
  56. clone = src && isArray(src) ? src : [];
  57. } else {
  58. clone = src && isPlainObject(src) ? src : {};
  59. }
  60. // Never move original objects, clone them
  61. target[name] = extend(deep, clone, copy);
  62. // Don't bring in undefined values
  63. } else if (typeof copy !== 'undefined') {
  64. target[name] = copy;
  65. }
  66. }
  67. }
  68. }
  69. }
  70. // Return the modified object
  71. return target;
  72. };