2024-02-06 07:03:07 +01:00
|
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2024-02-06 07:03:07 +01:00
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { deepClone } from './clone.js';
|
|
|
|
|
import type { Cloneable } from './clone.js';
|
|
|
|
|
|
2024-02-16 07:39:48 +01:00
|
|
|
|
type DeepPartial<T> = {
|
|
|
|
|
[P in keyof T]?: T[P] extends Record<string | number | symbol, unknown> ? DeepPartial<T[P]> : T[P];
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-06 07:03:07 +01:00
|
|
|
|
function isPureObject(value: unknown): value is Record<string | number | symbol, unknown> {
|
|
|
|
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* valueにないキーをdefからもらう(再帰的)\
|
|
|
|
|
* nullはそのまま、undefinedはdefの値
|
|
|
|
|
**/
|
2024-02-16 07:39:48 +01:00
|
|
|
|
export function deepMerge<X extends Record<string | number | symbol, unknown>>(value: DeepPartial<X>, def: X): X {
|
2024-02-06 07:03:07 +01:00
|
|
|
|
if (isPureObject(value) && isPureObject(def)) {
|
|
|
|
|
const result = deepClone(value as Cloneable) as X;
|
|
|
|
|
for (const [k, v] of Object.entries(def) as [keyof X, X[keyof X]][]) {
|
|
|
|
|
if (!Object.prototype.hasOwnProperty.call(value, k) || value[k] === undefined) {
|
|
|
|
|
result[k] = v;
|
|
|
|
|
} else if (isPureObject(v) && isPureObject(result[k])) {
|
2024-02-16 07:39:48 +01:00
|
|
|
|
const child = deepClone(result[k] as Cloneable) as DeepPartial<X[keyof X] & Record<string | number | symbol, unknown>>;
|
2024-02-06 07:03:07 +01:00
|
|
|
|
result[k] = deepMerge<typeof v>(child, v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2024-02-16 07:39:48 +01:00
|
|
|
|
throw new Error('deepMerge: value and def must be pure objects');
|
2024-02-06 07:03:07 +01:00
|
|
|
|
}
|