objects.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. "use strict";
  2. /*
  3. This file is part of web3.js.
  4. web3.js is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. web3.js is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with web3.js. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. exports.mergeDeep = void 0;
  17. const web3_types_1 = require("web3-types");
  18. const web3_validator_1 = require("web3-validator");
  19. const isIterable = (item) => typeof item === 'object' &&
  20. !(0, web3_validator_1.isNullish)(item) &&
  21. !Array.isArray(item) &&
  22. !(item instanceof web3_types_1.TypedArray);
  23. // The following code is a derivative work of the code from the "LiskHQ/lisk-sdk" project,
  24. // which is licensed under Apache version 2.
  25. /**
  26. * Deep merge two objects.
  27. * @param destination - The destination object.
  28. * @param sources - An array of source objects.
  29. * @returns - The merged object.
  30. */
  31. const mergeDeep = (destination, ...sources) => {
  32. const result = destination; // clone deep here
  33. if (!isIterable(result)) {
  34. return result;
  35. }
  36. for (const src of sources) {
  37. // eslint-disable-next-line no-restricted-syntax
  38. for (const key in src) {
  39. if (isIterable(src[key])) {
  40. if (!result[key]) {
  41. result[key] = {};
  42. }
  43. (0, exports.mergeDeep)(result[key], src[key]);
  44. }
  45. else if (!(0, web3_validator_1.isNullish)(src[key]) && Object.hasOwnProperty.call(src, key)) {
  46. if (Array.isArray(src[key]) || src[key] instanceof web3_types_1.TypedArray) {
  47. result[key] = src[key].slice(0);
  48. }
  49. else {
  50. result[key] = src[key];
  51. }
  52. }
  53. }
  54. }
  55. return result;
  56. };
  57. exports.mergeDeep = mergeDeep;
  58. //# sourceMappingURL=objects.js.map