object-assign.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict';
  2. var DESCRIPTORS = require('../internals/descriptors');
  3. var fails = require('../internals/fails');
  4. var objectKeys = require('../internals/object-keys');
  5. var getOwnPropertySymbolsModule = require('../internals/object-get-own-property-symbols');
  6. var propertyIsEnumerableModule = require('../internals/object-property-is-enumerable');
  7. var toObject = require('../internals/to-object');
  8. var IndexedObject = require('../internals/indexed-object');
  9. var nativeAssign = Object.assign;
  10. var defineProperty = Object.defineProperty;
  11. // `Object.assign` method
  12. // https://tc39.github.io/ecma262/#sec-object.assign
  13. module.exports = !nativeAssign || fails(function () {
  14. // should have correct order of operations (Edge bug)
  15. if (DESCRIPTORS && nativeAssign({ b: 1 }, nativeAssign(defineProperty({}, 'a', {
  16. enumerable: true,
  17. get: function () {
  18. defineProperty(this, 'b', {
  19. value: 3,
  20. enumerable: false
  21. });
  22. }
  23. }), { b: 2 })).b !== 1) return true;
  24. // should work with symbols and should have deterministic property order (V8 bug)
  25. var A = {};
  26. var B = {};
  27. // eslint-disable-next-line no-undef
  28. var symbol = Symbol();
  29. var alphabet = 'abcdefghijklmnopqrst';
  30. A[symbol] = 7;
  31. alphabet.split('').forEach(function (chr) { B[chr] = chr; });
  32. return nativeAssign({}, A)[symbol] != 7 || objectKeys(nativeAssign({}, B)).join('') != alphabet;
  33. }) ? function assign(target, source) { // eslint-disable-line no-unused-vars
  34. var T = toObject(target);
  35. var argumentsLength = arguments.length;
  36. var index = 1;
  37. var getOwnPropertySymbols = getOwnPropertySymbolsModule.f;
  38. var propertyIsEnumerable = propertyIsEnumerableModule.f;
  39. while (argumentsLength > index) {
  40. var S = IndexedObject(arguments[index++]);
  41. var keys = getOwnPropertySymbols ? objectKeys(S).concat(getOwnPropertySymbols(S)) : objectKeys(S);
  42. var length = keys.length;
  43. var j = 0;
  44. var key;
  45. while (length > j) {
  46. key = keys[j++];
  47. if (!DESCRIPTORS || propertyIsEnumerable.call(S, key)) T[key] = S[key];
  48. }
  49. } return T;
  50. } : nativeAssign;