index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. }