index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. var isExtendable = require('is-extendable');
  3. var assignSymbols = require('assign-symbols');
  4. module.exports = Object.assign || function(obj/*, objects*/) {
  5. if (obj === null || typeof obj === 'undefined') {
  6. throw new TypeError('Cannot convert undefined or null to object');
  7. }
  8. if (!isObject(obj)) {
  9. obj = {};
  10. }
  11. for (var i = 1; i < arguments.length; i++) {
  12. var val = arguments[i];
  13. if (isString(val)) {
  14. val = toObject(val);
  15. }
  16. if (isObject(val)) {
  17. assign(obj, val);
  18. assignSymbols(obj, val);
  19. }
  20. }
  21. return obj;
  22. };
  23. function assign(a, b) {
  24. for (var key in b) {
  25. if (hasOwn(b, key)) {
  26. a[key] = b[key];
  27. }
  28. }
  29. }
  30. function isString(val) {
  31. return (val && typeof val === 'string');
  32. }
  33. function toObject(str) {
  34. var obj = {};
  35. for (var i in str) {
  36. obj[i] = str[i];
  37. }
  38. return obj;
  39. }
  40. function isObject(val) {
  41. return (val && typeof val === 'object') || isExtendable(val);
  42. }
  43. /**
  44. * Returns true if the given `key` is an own property of `obj`.
  45. */
  46. function hasOwn(obj, key) {
  47. return Object.prototype.hasOwnProperty.call(obj, key);
  48. }
  49. function isEnum(obj, key) {
  50. return Object.prototype.propertyIsEnumerable.call(obj, key);
  51. }