You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
729 B

4 years ago
  1. function override(source1, source2) {
  2. var target = {};
  3. var key1;
  4. var key2;
  5. var item;
  6. for (key1 in source1) {
  7. item = source1[key1];
  8. if (Array.isArray(item)) {
  9. target[key1] = item.slice(0);
  10. } else if (typeof item == 'object' && item !== null) {
  11. target[key1] = override(item, {});
  12. } else {
  13. target[key1] = item;
  14. }
  15. }
  16. for (key2 in source2) {
  17. item = source2[key2];
  18. if (key2 in target && Array.isArray(item)) {
  19. target[key2] = item.slice(0);
  20. } else if (key2 in target && typeof item == 'object' && item !== null) {
  21. target[key2] = override(target[key2], item);
  22. } else {
  23. target[key2] = item;
  24. }
  25. }
  26. return target;
  27. }
  28. module.exports = override;