index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. if (!isValidKey(key)) {
  23. return;
  24. }
  25. var obj = this[key];
  26. if (isObject(val) && isObject(obj)) {
  27. mixinDeep(obj, val);
  28. } else {
  29. this[key] = val;
  30. }
  31. }
  32. /**
  33. * Returns true if `val` is an object or function.
  34. *
  35. * @param {any} val
  36. * @return {Boolean}
  37. */
  38. function isObject(val) {
  39. return isExtendable(val) && !Array.isArray(val);
  40. }
  41. /**
  42. * Returns true if `key` is a valid key to use when extending objects.
  43. *
  44. * @param {String} `key`
  45. * @return {Boolean}
  46. */
  47. function isValidKey(key) {
  48. return key !== '__proto__' && key !== 'constructor' && key !== 'prototype';
  49. };
  50. /**
  51. * Expose `mixinDeep`
  52. */
  53. module.exports = mixinDeep;