merge.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.mergeAll = exports.hasOwn = void 0;
  4. /* eslint-disable @typescript-eslint/unbound-method */
  5. exports.hasOwn = Function.prototype.call.bind(Object.prototype.hasOwnProperty);
  6. const objToString = Function.prototype.call.bind(Object.prototype.toString);
  7. /* eslint-enable @typescript-eslint/unbound-method */
  8. function isPlainObject(obj) {
  9. return objToString(obj) === '[object Object]';
  10. }
  11. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  12. function merge(target, source, options) {
  13. for (const key of Object.keys(source)) {
  14. const newValue = source[key];
  15. if ((0, exports.hasOwn)(target, key)) {
  16. if (Array.isArray(target[key]) && Array.isArray(newValue)) {
  17. if (options.mergeArrays) {
  18. target[key].push(...newValue);
  19. continue;
  20. }
  21. }
  22. else if (isPlainObject(target[key]) && isPlainObject(newValue)) {
  23. target[key] = merge(target[key], newValue, options);
  24. continue;
  25. }
  26. }
  27. target[key] = newValue;
  28. }
  29. return target;
  30. }
  31. /**
  32. * Merges multiple objects. Doesn't care about cloning non-primitives, as we load all these objects fresh from a file.
  33. */
  34. function mergeAll(
  35. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  36. objects, options) {
  37. return objects.reduce((target, source) => merge(target, source, options), {});
  38. }
  39. exports.mergeAll = mergeAll;
  40. //# sourceMappingURL=merge.js.map