@graphql-tools/utils
Advanced tools
Comparing version 10.2.2-alpha-20240604132826-768b25589d1b501eaba4c3c06bfae6d3b33ce0c9 to 10.2.2-alpha-20240604141848-3e5ddc0bed3db3f0d65611cf4de65e9f38fcb7dd
@@ -6,16 +6,26 @@ "use strict"; | ||
function mergeDeep(sources, respectPrototype = false, respectArrays = false, respectArrayLength = false) { | ||
const target = sources[0] || {}; | ||
if (respectArrays && respectArrayLength) { | ||
let expectedLength; | ||
const areArraysInTheSameLength = sources.every(source => { | ||
if (Array.isArray(source)) { | ||
if (expectedLength === undefined) { | ||
expectedLength = source.length; | ||
return true; | ||
} | ||
else if (expectedLength === source.length) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}); | ||
if (areArraysInTheSameLength) { | ||
return new Array(expectedLength).fill(null).map((_, index) => mergeDeep(sources.map(source => source[index]), respectPrototype, respectArrays, respectArrayLength)); | ||
} | ||
} | ||
const output = {}; | ||
if (respectPrototype) { | ||
Object.setPrototypeOf(output, Object.create(Object.getPrototypeOf(target))); | ||
Object.setPrototypeOf(output, Object.create(Object.getPrototypeOf(sources[0]))); | ||
} | ||
if (Array.isArray(target)) { | ||
// Are they all arrays with the same length? | ||
const allArrays = sources.every(source => Array.isArray(source) && source.length === target.length); | ||
if (allArrays) { | ||
return target.map((_, i) => mergeDeep(sources.map(source => source[i]), respectPrototype, respectArrays, respectArrayLength)); | ||
} | ||
} | ||
for (const source of sources) { | ||
if (isObject(target) && isObject(source)) { | ||
if (isObject(source)) { | ||
if (respectPrototype) { | ||
@@ -39,3 +49,3 @@ const outputPrototype = Object.getPrototypeOf(output); | ||
else { | ||
output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays); | ||
output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays, respectArrayLength); | ||
} | ||
@@ -45,3 +55,8 @@ } | ||
if (Array.isArray(source[key])) { | ||
output[key].push(...source[key]); | ||
if (respectArrayLength && output[key].length === source[key].length) { | ||
output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays, respectArrayLength); | ||
} | ||
else { | ||
output[key].push(...source[key]); | ||
} | ||
} | ||
@@ -57,16 +72,2 @@ else { | ||
} | ||
else if (respectArrays && Array.isArray(target)) { | ||
if (Array.isArray(source)) { | ||
if (respectArrayLength && source.length === target.length) { | ||
return target.map((targetElem, i) => mergeDeep([targetElem, source[i]], respectPrototype, respectArrays, respectArrayLength)); | ||
} | ||
target.push(...source); | ||
} | ||
else { | ||
target.push(source); | ||
} | ||
} | ||
else if (respectArrays && Array.isArray(source)) { | ||
return [target, ...source]; | ||
} | ||
} | ||
@@ -73,0 +74,0 @@ return output; |
import { isSome } from './helpers.js'; | ||
export function mergeDeep(sources, respectPrototype = false, respectArrays = false, respectArrayLength = false) { | ||
const target = sources[0] || {}; | ||
if (respectArrays && respectArrayLength) { | ||
let expectedLength; | ||
const areArraysInTheSameLength = sources.every(source => { | ||
if (Array.isArray(source)) { | ||
if (expectedLength === undefined) { | ||
expectedLength = source.length; | ||
return true; | ||
} | ||
else if (expectedLength === source.length) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}); | ||
if (areArraysInTheSameLength) { | ||
return new Array(expectedLength).fill(null).map((_, index) => mergeDeep(sources.map(source => source[index]), respectPrototype, respectArrays, respectArrayLength)); | ||
} | ||
} | ||
const output = {}; | ||
if (respectPrototype) { | ||
Object.setPrototypeOf(output, Object.create(Object.getPrototypeOf(target))); | ||
Object.setPrototypeOf(output, Object.create(Object.getPrototypeOf(sources[0]))); | ||
} | ||
if (Array.isArray(target)) { | ||
// Are they all arrays with the same length? | ||
const allArrays = sources.every(source => Array.isArray(source) && source.length === target.length); | ||
if (allArrays) { | ||
return target.map((_, i) => mergeDeep(sources.map(source => source[i]), respectPrototype, respectArrays, respectArrayLength)); | ||
} | ||
} | ||
for (const source of sources) { | ||
if (isObject(target) && isObject(source)) { | ||
if (isObject(source)) { | ||
if (respectPrototype) { | ||
@@ -35,3 +45,3 @@ const outputPrototype = Object.getPrototypeOf(output); | ||
else { | ||
output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays); | ||
output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays, respectArrayLength); | ||
} | ||
@@ -41,3 +51,8 @@ } | ||
if (Array.isArray(source[key])) { | ||
output[key].push(...source[key]); | ||
if (respectArrayLength && output[key].length === source[key].length) { | ||
output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays, respectArrayLength); | ||
} | ||
else { | ||
output[key].push(...source[key]); | ||
} | ||
} | ||
@@ -53,16 +68,2 @@ else { | ||
} | ||
else if (respectArrays && Array.isArray(target)) { | ||
if (Array.isArray(source)) { | ||
if (respectArrayLength && source.length === target.length) { | ||
return target.map((targetElem, i) => mergeDeep([targetElem, source[i]], respectPrototype, respectArrays, respectArrayLength)); | ||
} | ||
target.push(...source); | ||
} | ||
else { | ||
target.push(source); | ||
} | ||
} | ||
else if (respectArrays && Array.isArray(source)) { | ||
return [target, ...source]; | ||
} | ||
} | ||
@@ -69,0 +70,0 @@ return output; |
{ | ||
"name": "@graphql-tools/utils", | ||
"version": "10.2.2-alpha-20240604132826-768b25589d1b501eaba4c3c06bfae6d3b33ce0c9", | ||
"version": "10.2.2-alpha-20240604141848-3e5ddc0bed3db3f0d65611cf4de65e9f38fcb7dd", | ||
"description": "Common package containing utils and types for GraphQL tools", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
501350
10849