@graphql-tools/utils
Advanced tools
Comparing version 10.6.1 to 10.6.2-alpha-20241204120043-4eb970a1e7fffafa2a0eca8ececaf956c6282876
@@ -6,27 +6,39 @@ "use strict"; | ||
function mergeDeep(sources, respectPrototype = false, respectArrays = false, respectArrayLength = false) { | ||
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; | ||
} | ||
let expectedLength; | ||
let allArrays = true; | ||
const areArraysInTheSameLength = sources.every(source => { | ||
if (Array.isArray(source)) { | ||
if (expectedLength === undefined) { | ||
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)); | ||
else if (expectedLength === source.length) { | ||
return true; | ||
} | ||
} | ||
else { | ||
allArrays = false; | ||
} | ||
return false; | ||
}); | ||
if (respectArrayLength && areArraysInTheSameLength) { | ||
return new Array(expectedLength).fill(null).map((_, index) => mergeDeep(sources.map(source => source[index]), respectPrototype, respectArrays, respectArrayLength)); | ||
} | ||
const output = {}; | ||
if (allArrays) { | ||
return sources.flat(1); | ||
} | ||
let output; | ||
let firstObjectSource; | ||
if (respectPrototype) { | ||
Object.setPrototypeOf(output, Object.create(Object.getPrototypeOf(sources[0]))); | ||
firstObjectSource = sources.find(source => isObject(source)); | ||
if (output == null) { | ||
output = {}; | ||
} | ||
if (firstObjectSource) { | ||
Object.setPrototypeOf(output, Object.create(Object.getPrototypeOf(firstObjectSource))); | ||
} | ||
} | ||
for (const source of sources) { | ||
if (isObject(source)) { | ||
if (respectPrototype) { | ||
if (firstObjectSource) { | ||
const outputPrototype = Object.getPrototypeOf(output); | ||
@@ -44,28 +56,24 @@ const sourcePrototype = Object.getPrototypeOf(source); | ||
for (const key in source) { | ||
if (isObject(source[key])) { | ||
if (!(key in output)) { | ||
Object.assign(output, { [key]: source[key] }); | ||
} | ||
else { | ||
output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays, respectArrayLength); | ||
} | ||
if (output == null) { | ||
output = {}; | ||
} | ||
else if (respectArrays && Array.isArray(output[key])) { | ||
if (Array.isArray(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]); | ||
} | ||
} | ||
else { | ||
output[key].push(source[key]); | ||
} | ||
if (key in output) { | ||
output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays, respectArrayLength); | ||
} | ||
else { | ||
Object.assign(output, { [key]: source[key] }); | ||
output[key] = source[key]; | ||
} | ||
} | ||
} | ||
else if (Array.isArray(source)) { | ||
if (!Array.isArray(output)) { | ||
output = source; | ||
} | ||
else { | ||
output = mergeDeep([output, source], respectPrototype, respectArrays, respectArrayLength); | ||
} | ||
} | ||
else { | ||
output = source; | ||
} | ||
} | ||
@@ -72,0 +80,0 @@ return output; |
import { isSome } from './helpers.js'; | ||
export function mergeDeep(sources, respectPrototype = false, respectArrays = false, respectArrayLength = false) { | ||
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; | ||
} | ||
let expectedLength; | ||
let allArrays = true; | ||
const areArraysInTheSameLength = sources.every(source => { | ||
if (Array.isArray(source)) { | ||
if (expectedLength === undefined) { | ||
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)); | ||
else if (expectedLength === source.length) { | ||
return true; | ||
} | ||
} | ||
else { | ||
allArrays = false; | ||
} | ||
return false; | ||
}); | ||
if (respectArrayLength && areArraysInTheSameLength) { | ||
return new Array(expectedLength).fill(null).map((_, index) => mergeDeep(sources.map(source => source[index]), respectPrototype, respectArrays, respectArrayLength)); | ||
} | ||
const output = {}; | ||
if (allArrays) { | ||
return sources.flat(1); | ||
} | ||
let output; | ||
let firstObjectSource; | ||
if (respectPrototype) { | ||
Object.setPrototypeOf(output, Object.create(Object.getPrototypeOf(sources[0]))); | ||
firstObjectSource = sources.find(source => isObject(source)); | ||
if (output == null) { | ||
output = {}; | ||
} | ||
if (firstObjectSource) { | ||
Object.setPrototypeOf(output, Object.create(Object.getPrototypeOf(firstObjectSource))); | ||
} | ||
} | ||
for (const source of sources) { | ||
if (isObject(source)) { | ||
if (respectPrototype) { | ||
if (firstObjectSource) { | ||
const outputPrototype = Object.getPrototypeOf(output); | ||
@@ -40,28 +52,24 @@ const sourcePrototype = Object.getPrototypeOf(source); | ||
for (const key in source) { | ||
if (isObject(source[key])) { | ||
if (!(key in output)) { | ||
Object.assign(output, { [key]: source[key] }); | ||
} | ||
else { | ||
output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays, respectArrayLength); | ||
} | ||
if (output == null) { | ||
output = {}; | ||
} | ||
else if (respectArrays && Array.isArray(output[key])) { | ||
if (Array.isArray(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]); | ||
} | ||
} | ||
else { | ||
output[key].push(source[key]); | ||
} | ||
if (key in output) { | ||
output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays, respectArrayLength); | ||
} | ||
else { | ||
Object.assign(output, { [key]: source[key] }); | ||
output[key] = source[key]; | ||
} | ||
} | ||
} | ||
else if (Array.isArray(source)) { | ||
if (!Array.isArray(output)) { | ||
output = source; | ||
} | ||
else { | ||
output = mergeDeep([output, source], respectPrototype, respectArrays, respectArrayLength); | ||
} | ||
} | ||
else { | ||
output = source; | ||
} | ||
} | ||
@@ -68,0 +76,0 @@ return output; |
{ | ||
"name": "@graphql-tools/utils", | ||
"version": "10.6.1", | ||
"version": "10.6.2-alpha-20241204120043-4eb970a1e7fffafa2a0eca8ececaf956c6282876", | ||
"description": "Common package containing utils and types for GraphQL tools", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11212
515825
2