object-assign.js 2.3 KB

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