index.js 826 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. var isExtendable = require('is-extendable');
  3. var forIn = require('for-in');
  4. function mixinDeep(target, objects) {
  5. var len = arguments.length, i = 0;
  6. while (++i < len) {
  7. var obj = arguments[i];
  8. if (isObject(obj)) {
  9. forIn(obj, copy, target);
  10. }
  11. }
  12. return target;
  13. }
  14. /**
  15. * Copy properties from the source object to the
  16. * target object.
  17. *
  18. * @param {*} `val`
  19. * @param {String} `key`
  20. */
  21. function copy(val, key) {
  22. var obj = this[key];
  23. if (isObject(val) && isObject(obj)) {
  24. mixinDeep(obj, val);
  25. } else {
  26. this[key] = val;
  27. }
  28. }
  29. /**
  30. * Returns true if `val` is an object or function.
  31. *
  32. * @param {any} val
  33. * @return {Boolean}
  34. */
  35. function isObject(val) {
  36. return isExtendable(val) && !Array.isArray(val);
  37. }
  38. /**
  39. * Expose `mixinDeep`
  40. */
  41. module.exports = mixinDeep;