@graphql-tools/utils 10.2.1 → 10.2.2-alpha-20240604141848-3e5ddc0bed3db3f0d65611cf4de65e9f38fcb7dd

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cjs/mergeDeep.js CHANGED
@@ -3,13 +3,30 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.mergeDeep = void 0;
4
4
  const helpers_js_1 = require("./helpers.js");
5
5
  function mergeDeep(sources, respectPrototype = false, respectArrays = false, respectArrayLength = false) {
6
- const target = sources[0] || {};
6
+ if (respectArrays && respectArrayLength) {
7
+ let expectedLength;
8
+ const areArraysInTheSameLength = sources.every(source => {
9
+ if (Array.isArray(source)) {
10
+ if (expectedLength === undefined) {
11
+ expectedLength = source.length;
12
+ return true;
13
+ }
14
+ else if (expectedLength === source.length) {
15
+ return true;
16
+ }
17
+ }
18
+ return false;
19
+ });
20
+ if (areArraysInTheSameLength) {
21
+ return new Array(expectedLength).fill(null).map((_, index) => mergeDeep(sources.map(source => source[index]), respectPrototype, respectArrays, respectArrayLength));
22
+ }
23
+ }
7
24
  const output = {};
8
25
  if (respectPrototype) {
9
- Object.setPrototypeOf(output, Object.create(Object.getPrototypeOf(target)));
26
+ Object.setPrototypeOf(output, Object.create(Object.getPrototypeOf(sources[0])));
10
27
  }
11
28
  for (const source of sources) {
12
- if (isObject(target) && isObject(source)) {
29
+ if (isObject(source)) {
13
30
  if (respectPrototype) {
14
31
  const outputPrototype = Object.getPrototypeOf(output);
15
32
  const sourcePrototype = Object.getPrototypeOf(source);
@@ -28,12 +45,17 @@ function mergeDeep(sources, respectPrototype = false, respectArrays = false, res
28
45
  Object.assign(output, { [key]: source[key] });
29
46
  }
30
47
  else {
31
- output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays);
48
+ output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays, respectArrayLength);
32
49
  }
33
50
  }
34
51
  else if (respectArrays && Array.isArray(output[key])) {
35
52
  if (Array.isArray(source[key])) {
36
- output[key].push(...source[key]);
53
+ if (respectArrayLength && output[key].length === source[key].length) {
54
+ output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays, respectArrayLength);
55
+ }
56
+ else {
57
+ output[key].push(...source[key]);
58
+ }
37
59
  }
38
60
  else {
39
61
  output[key].push(source[key]);
@@ -44,20 +66,6 @@ function mergeDeep(sources, respectPrototype = false, respectArrays = false, res
44
66
  }
45
67
  }
46
68
  }
47
- else if (respectArrays && Array.isArray(target)) {
48
- if (Array.isArray(source)) {
49
- if (respectArrayLength && source.length === target.length) {
50
- return target.map((targetElem, i) => mergeDeep([targetElem, source[i]], respectPrototype, respectArrays, respectArrayLength));
51
- }
52
- target.push(...source);
53
- }
54
- else {
55
- target.push(source);
56
- }
57
- }
58
- else if (respectArrays && Array.isArray(source)) {
59
- return [target, ...source];
60
- }
61
69
  }
62
70
  return output;
63
71
  }
package/esm/mergeDeep.js CHANGED
@@ -1,12 +1,29 @@
1
1
  import { isSome } from './helpers.js';
2
2
  export function mergeDeep(sources, respectPrototype = false, respectArrays = false, respectArrayLength = false) {
3
- const target = sources[0] || {};
3
+ if (respectArrays && respectArrayLength) {
4
+ let expectedLength;
5
+ const areArraysInTheSameLength = sources.every(source => {
6
+ if (Array.isArray(source)) {
7
+ if (expectedLength === undefined) {
8
+ expectedLength = source.length;
9
+ return true;
10
+ }
11
+ else if (expectedLength === source.length) {
12
+ return true;
13
+ }
14
+ }
15
+ return false;
16
+ });
17
+ if (areArraysInTheSameLength) {
18
+ return new Array(expectedLength).fill(null).map((_, index) => mergeDeep(sources.map(source => source[index]), respectPrototype, respectArrays, respectArrayLength));
19
+ }
20
+ }
4
21
  const output = {};
5
22
  if (respectPrototype) {
6
- Object.setPrototypeOf(output, Object.create(Object.getPrototypeOf(target)));
23
+ Object.setPrototypeOf(output, Object.create(Object.getPrototypeOf(sources[0])));
7
24
  }
8
25
  for (const source of sources) {
9
- if (isObject(target) && isObject(source)) {
26
+ if (isObject(source)) {
10
27
  if (respectPrototype) {
11
28
  const outputPrototype = Object.getPrototypeOf(output);
12
29
  const sourcePrototype = Object.getPrototypeOf(source);
@@ -25,12 +42,17 @@ export function mergeDeep(sources, respectPrototype = false, respectArrays = fal
25
42
  Object.assign(output, { [key]: source[key] });
26
43
  }
27
44
  else {
28
- output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays);
45
+ output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays, respectArrayLength);
29
46
  }
30
47
  }
31
48
  else if (respectArrays && Array.isArray(output[key])) {
32
49
  if (Array.isArray(source[key])) {
33
- output[key].push(...source[key]);
50
+ if (respectArrayLength && output[key].length === source[key].length) {
51
+ output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays, respectArrayLength);
52
+ }
53
+ else {
54
+ output[key].push(...source[key]);
55
+ }
34
56
  }
35
57
  else {
36
58
  output[key].push(source[key]);
@@ -41,20 +63,6 @@ export function mergeDeep(sources, respectPrototype = false, respectArrays = fal
41
63
  }
42
64
  }
43
65
  }
44
- else if (respectArrays && Array.isArray(target)) {
45
- if (Array.isArray(source)) {
46
- if (respectArrayLength && source.length === target.length) {
47
- return target.map((targetElem, i) => mergeDeep([targetElem, source[i]], respectPrototype, respectArrays, respectArrayLength));
48
- }
49
- target.push(...source);
50
- }
51
- else {
52
- target.push(source);
53
- }
54
- }
55
- else if (respectArrays && Array.isArray(source)) {
56
- return [target, ...source];
57
- }
58
66
  }
59
67
  return output;
60
68
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/utils",
3
- "version": "10.2.1",
3
+ "version": "10.2.2-alpha-20240604141848-3e5ddc0bed3db3f0d65611cf4de65e9f38fcb7dd",
4
4
  "description": "Common package containing utils and types for GraphQL tools",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {