Files
payloadcms/packages/eslint-config/deepMerge.js
Alessio Gravili 1038e1c228 chore: move to eslint v9 (#7041)
- Upgrades eslint from v8 to v9
- Upgrades all other eslint packages. We will have to do a new
full-project lint, as new rules have been added
- Upgrades husky from v8 to v9
- Upgrades lint-staged from v14 to v15
- Moves the old .eslintrc.cjs file format to the new eslint.config.js
flat file format.

Previously, we were very specific regarding which rules are applied to
which files. Now that `extends` is no longer a thing, I have to use
deepMerge & imports instead.

This is rather uncommon and is not a documented pattern - e.g.
typescript-eslint docs want us to add the default typescript-eslint
rules to the top-level & then disable it in files using the
disable-typechecked config.

However, I hate this opt-out approach. The way I did it here adds a lot
of clarity as to which rules are applied to which files, and is pretty
easy to read. Much less black magic

## .eslintignore

These files are no longer supported (see
https://eslint.org/docs/latest/use/configure/migration-guide#ignoring-files).
I moved the entries to the ignores property in the eslint config. => one
less file in each package folder!
2024-07-09 09:50:37 -04:00

59 lines
1.7 KiB
JavaScript

/**
* obj2 has priority over obj1
*
* Merges obj2 into obj1
*/
export function _deepMerge(obj1, obj2, doNotMergeInNulls = true) {
const output = { ...obj1 }
for (const key in obj2) {
if (Object.prototype.hasOwnProperty.call(obj2, key)) {
if (doNotMergeInNulls) {
if (
(obj2[key] === null || obj2[key] === undefined) &&
obj1[key] !== null &&
obj1[key] !== undefined
) {
continue
}
}
// Check if both are arrays
if (Array.isArray(obj1[key]) && Array.isArray(obj2[key])) {
// Merge each element in the arrays
// We need the Array.from, map rather than a normal map because this handles holes in arrays properly. A simple .map would skip holes.
output[key] = Array.from(obj2[key], (item, index) => {
if (doNotMergeInNulls) {
if (
(item === undefined || item === null) &&
obj1[key][index] !== null &&
obj1[key][index] !== undefined
) {
return obj1[key][index]
}
}
if (typeof item === 'object' && !Array.isArray(item) && obj1[key][index]) {
// Deep merge for objects in arrays
return deepMerge(obj1[key][index], item, doNotMergeInNulls)
}
return item
})
} else if (typeof obj2[key] === 'object' && !Array.isArray(obj2[key]) && obj1[key]) {
// Existing behavior for objects
output[key] = deepMerge(obj1[key], obj2[key], doNotMergeInNulls)
} else {
// Direct assignment for values
output[key] = obj2[key]
}
}
}
return output
}
export function deepMerge(...objs) {
return objs.reduce((acc, obj) => _deepMerge(acc, obj), {})
}