@graphql-tools/utils 10.6.1 → 10.6.2

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,31 +3,43 @@ 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
- 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
- }
6
+ let expectedLength;
7
+ let allArrays = true;
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;
17
16
  }
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
17
  }
18
+ else {
19
+ allArrays = false;
20
+ }
21
+ return false;
22
+ });
23
+ if (respectArrayLength && areArraysInTheSameLength) {
24
+ return new Array(expectedLength).fill(null).map((_, index) => mergeDeep(sources.map(source => source[index]), respectPrototype, respectArrays, respectArrayLength));
25
+ }
26
+ if (allArrays) {
27
+ return sources.flat(1);
23
28
  }
24
- const output = {};
29
+ let output;
30
+ let firstObjectSource;
25
31
  if (respectPrototype) {
26
- Object.setPrototypeOf(output, Object.create(Object.getPrototypeOf(sources[0])));
32
+ firstObjectSource = sources.find(source => isObject(source));
33
+ if (output == null) {
34
+ output = {};
35
+ }
36
+ if (firstObjectSource) {
37
+ Object.setPrototypeOf(output, Object.create(Object.getPrototypeOf(firstObjectSource)));
38
+ }
27
39
  }
28
40
  for (const source of sources) {
29
41
  if (isObject(source)) {
30
- if (respectPrototype) {
42
+ if (firstObjectSource) {
31
43
  const outputPrototype = Object.getPrototypeOf(output);
32
44
  const sourcePrototype = Object.getPrototypeOf(source);
33
45
  if (sourcePrototype) {
@@ -40,32 +52,28 @@ function mergeDeep(sources, respectPrototype = false, respectArrays = false, res
40
52
  }
41
53
  }
42
54
  for (const key in source) {
43
- if (isObject(source[key])) {
44
- if (!(key in output)) {
45
- Object.assign(output, { [key]: source[key] });
46
- }
47
- else {
48
- output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays, respectArrayLength);
49
- }
55
+ if (output == null) {
56
+ output = {};
50
57
  }
51
- else if (respectArrays && Array.isArray(output[key])) {
52
- if (Array.isArray(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
- }
59
- }
60
- else {
61
- output[key].push(source[key]);
62
- }
58
+ if (key in output) {
59
+ output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays, respectArrayLength);
63
60
  }
64
61
  else {
65
- Object.assign(output, { [key]: source[key] });
62
+ output[key] = source[key];
66
63
  }
67
64
  }
68
65
  }
66
+ else if (Array.isArray(source)) {
67
+ if (!Array.isArray(output)) {
68
+ output = source;
69
+ }
70
+ else {
71
+ output = mergeDeep([output, source], respectPrototype, respectArrays, respectArrayLength);
72
+ }
73
+ }
74
+ else {
75
+ output = source;
76
+ }
69
77
  }
70
78
  return output;
71
79
  }
package/esm/mergeDeep.js CHANGED
@@ -1,30 +1,42 @@
1
1
  import { isSome } from './helpers.js';
2
2
  export function mergeDeep(sources, respectPrototype = false, respectArrays = false, respectArrayLength = false) {
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
- }
3
+ let expectedLength;
4
+ let allArrays = true;
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;
14
13
  }
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
14
  }
15
+ else {
16
+ allArrays = false;
17
+ }
18
+ return false;
19
+ });
20
+ if (respectArrayLength && areArraysInTheSameLength) {
21
+ return new Array(expectedLength).fill(null).map((_, index) => mergeDeep(sources.map(source => source[index]), respectPrototype, respectArrays, respectArrayLength));
22
+ }
23
+ if (allArrays) {
24
+ return sources.flat(1);
20
25
  }
21
- const output = {};
26
+ let output;
27
+ let firstObjectSource;
22
28
  if (respectPrototype) {
23
- Object.setPrototypeOf(output, Object.create(Object.getPrototypeOf(sources[0])));
29
+ firstObjectSource = sources.find(source => isObject(source));
30
+ if (output == null) {
31
+ output = {};
32
+ }
33
+ if (firstObjectSource) {
34
+ Object.setPrototypeOf(output, Object.create(Object.getPrototypeOf(firstObjectSource)));
35
+ }
24
36
  }
25
37
  for (const source of sources) {
26
38
  if (isObject(source)) {
27
- if (respectPrototype) {
39
+ if (firstObjectSource) {
28
40
  const outputPrototype = Object.getPrototypeOf(output);
29
41
  const sourcePrototype = Object.getPrototypeOf(source);
30
42
  if (sourcePrototype) {
@@ -37,32 +49,28 @@ export function mergeDeep(sources, respectPrototype = false, respectArrays = fal
37
49
  }
38
50
  }
39
51
  for (const key in source) {
40
- if (isObject(source[key])) {
41
- if (!(key in output)) {
42
- Object.assign(output, { [key]: source[key] });
43
- }
44
- else {
45
- output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays, respectArrayLength);
46
- }
52
+ if (output == null) {
53
+ output = {};
47
54
  }
48
- else if (respectArrays && Array.isArray(output[key])) {
49
- if (Array.isArray(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
- }
56
- }
57
- else {
58
- output[key].push(source[key]);
59
- }
55
+ if (key in output) {
56
+ output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays, respectArrayLength);
60
57
  }
61
58
  else {
62
- Object.assign(output, { [key]: source[key] });
59
+ output[key] = source[key];
63
60
  }
64
61
  }
65
62
  }
63
+ else if (Array.isArray(source)) {
64
+ if (!Array.isArray(output)) {
65
+ output = source;
66
+ }
67
+ else {
68
+ output = mergeDeep([output, source], respectPrototype, respectArrays, respectArrayLength);
69
+ }
70
+ }
71
+ else {
72
+ output = source;
73
+ }
66
74
  }
67
75
  return output;
68
76
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/utils",
3
- "version": "10.6.1",
3
+ "version": "10.6.2",
4
4
  "description": "Common package containing utils and types for GraphQL tools",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {