You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.9 KiB

7 years ago
  1. 'use strict';
  2. /* eslint-disable no-unused-vars */
  3. var hasOwnProperty = Object.prototype.hasOwnProperty;
  4. var propIsEnumerable = Object.prototype.propertyIsEnumerable;
  5. function toObject(val) {
  6. if (val === null || val === undefined) {
  7. throw new TypeError('Object.assign cannot be called with null or undefined');
  8. }
  9. return Object(val);
  10. }
  11. function shouldUseNative() {
  12. try {
  13. if (!Object.assign) {
  14. return false;
  15. }
  16. // Detect buggy property enumeration order in older V8 versions.
  17. // https://bugs.chromium.org/p/v8/issues/detail?id=4118
  18. var test1 = new String('abc'); // eslint-disable-line
  19. test1[5] = 'de';
  20. if (Object.getOwnPropertyNames(test1)[0] === '5') {
  21. return false;
  22. }
  23. // https://bugs.chromium.org/p/v8/issues/detail?id=3056
  24. var test2 = {};
  25. for (var i = 0; i < 10; i++) {
  26. test2['_' + String.fromCharCode(i)] = i;
  27. }
  28. var order2 = Object.getOwnPropertyNames(test2).map(function (n) {
  29. return test2[n];
  30. });
  31. if (order2.join('') !== '0123456789') {
  32. return false;
  33. }
  34. // https://bugs.chromium.org/p/v8/issues/detail?id=3056
  35. var test3 = {};
  36. 'abcdefghijklmnopqrst'.split('').forEach(function (letter) {
  37. test3[letter] = letter;
  38. });
  39. if (Object.keys(Object.assign({}, test3)).join('') !==
  40. 'abcdefghijklmnopqrst') {
  41. return false;
  42. }
  43. return true;
  44. } catch (e) {
  45. // We don't expect any of the above to throw, but better to be safe.
  46. return false;
  47. }
  48. }
  49. module.exports = shouldUseNative() ? Object.assign : function (target, source) {
  50. var from;
  51. var to = toObject(target);
  52. var symbols;
  53. for (var s = 1; s < arguments.length; s++) {
  54. from = Object(arguments[s]);
  55. for (var key in from) {
  56. if (hasOwnProperty.call(from, key)) {
  57. to[key] = from[key];
  58. }
  59. }
  60. if (Object.getOwnPropertySymbols) {
  61. symbols = Object.getOwnPropertySymbols(from);
  62. for (var i = 0; i < symbols.length; i++) {
  63. if (propIsEnumerable.call(from, symbols[i])) {
  64. to[symbols[i]] = from[symbols[i]];
  65. }
  66. }
  67. }
  68. }
  69. return to;
  70. };