index.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict';
  2. var isObj = require('is-obj');
  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('Sources cannot be null or undefined');
  8. }
  9. return Object(val);
  10. }
  11. function base(to, from) {
  12. if (to === from) {
  13. return to;
  14. }
  15. from = Object(from);
  16. for (var key in from) {
  17. if (hasOwnProperty.call(from, key)) {
  18. var val = from[key];
  19. if (Array.isArray(val)) {
  20. to[key] = val.slice();
  21. } else if (isObj(val)) {
  22. to[key] = base(to[key] || {}, val);
  23. } else if (val !== undefined) {
  24. to[key] = val;
  25. }
  26. }
  27. }
  28. if (Object.getOwnPropertySymbols) {
  29. var symbols = Object.getOwnPropertySymbols(from);
  30. for (var i = 0; i < symbols.length; i++) {
  31. if (propIsEnumerable.call(from, symbols[i])) {
  32. to[symbols[i]] = from[symbols[i]];
  33. }
  34. }
  35. }
  36. return to;
  37. }
  38. module.exports = function deepAssign(target) {
  39. target = toObject(target);
  40. for (var s = 1; s < arguments.length; s++) {
  41. base(target, arguments[s]);
  42. }
  43. return target;
  44. };