index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 'use strict';
  2. var hasOwn = Object.prototype.hasOwnProperty;
  3. var toStr = Object.prototype.toString;
  4. var defineProperty = Object.defineProperty;
  5. var gOPD = Object.getOwnPropertyDescriptor;
  6. var isArray = function isArray(arr) {
  7. if (typeof Array.isArray === 'function') {
  8. return Array.isArray(arr);
  9. }
  10. return toStr.call(arr) === '[object Array]';
  11. };
  12. var isPlainObject = function isPlainObject(obj) {
  13. if (!obj || toStr.call(obj) !== '[object Object]') {
  14. return false;
  15. }
  16. var hasOwnConstructor = hasOwn.call(obj, 'constructor');
  17. var hasIsPrototypeOf = obj.constructor && obj.constructor.prototype && hasOwn.call(obj.constructor.prototype, 'isPrototypeOf');
  18. // Not own constructor property must be Object
  19. if (obj.constructor && !hasOwnConstructor && !hasIsPrototypeOf) {
  20. return false;
  21. }
  22. // Own properties are enumerated firstly, so to speed up,
  23. // if last one is own, then all properties are own.
  24. var key;
  25. for (key in obj) { /**/ }
  26. return typeof key === 'undefined' || hasOwn.call(obj, key);
  27. };
  28. // If name is '__proto__', and Object.defineProperty is available, define __proto__ as an own property on target
  29. var setProperty = function setProperty(target, options) {
  30. if (defineProperty && options.name === '__proto__') {
  31. defineProperty(target, options.name, {
  32. enumerable: true,
  33. configurable: true,
  34. value: options.newValue,
  35. writable: true
  36. });
  37. } else {
  38. target[options.name] = options.newValue;
  39. }
  40. };
  41. // Return undefined instead of __proto__ if '__proto__' is not an own property
  42. var getProperty = function getProperty(obj, name) {
  43. if (name === '__proto__') {
  44. if (!hasOwn.call(obj, name)) {
  45. return void 0;
  46. } else if (gOPD) {
  47. // In early versions of node, obj['__proto__'] is buggy when obj has
  48. // __proto__ as an own property. Object.getOwnPropertyDescriptor() works.
  49. return gOPD(obj, name).value;
  50. }
  51. }
  52. return obj[name];
  53. };
  54. module.exports = function extend() {
  55. var options, name, src, copy, copyIsArray, clone;
  56. var target = arguments[0];
  57. var i = 1;
  58. var length = arguments.length;
  59. var deep = false;
  60. // Handle a deep copy situation
  61. if (typeof target === 'boolean') {
  62. deep = target;
  63. target = arguments[1] || {};
  64. // skip the boolean and the target
  65. i = 2;
  66. }
  67. if (target == null || (typeof target !== 'object' && typeof target !== 'function')) {
  68. target = {};
  69. }
  70. for (; i < length; ++i) {
  71. options = arguments[i];
  72. // Only deal with non-null/undefined values
  73. if (options != null) {
  74. // Extend the base object
  75. for (name in options) {
  76. src = getProperty(target, name);
  77. copy = getProperty(options, name);
  78. // Prevent never-ending loop
  79. if (target !== copy) {
  80. // Recurse if we're merging plain objects or arrays
  81. if (deep && copy && (isPlainObject(copy) || (copyIsArray = isArray(copy)))) {
  82. if (copyIsArray) {
  83. copyIsArray = false;
  84. clone = src && isArray(src) ? src : [];
  85. } else {
  86. clone = src && isPlainObject(src) ? src : {};
  87. }
  88. // Never move original objects, clone them
  89. setProperty(target, { name: name, newValue: extend(deep, clone, copy) });
  90. // Don't bring in undefined values
  91. } else if (typeof copy !== 'undefined') {
  92. setProperty(target, { name: name, newValue: copy });
  93. }
  94. }
  95. }
  96. }
  97. }
  98. // Return the modified object
  99. return target;
  100. };