@novo-learning/novo-sdk
Version:
SDK for the Novolanguage Speech Analysis API
46 lines (40 loc) • 1.21 kB
text/typescript
export const isObject = <T>(item: T): item is NonNullable<T> => {
return item != null && typeof item === 'object' && !Array.isArray(item);
};
const forbiddenProperties: string[] = ['__proto__', 'constructor', 'prototype'];
/**
* Merges partial objects into the target object.
* This modifies the target object and returns it.
*
* @param target The object into which the changes are merged
* @param sources A list of patches to apply
* @returns the target
*/
export const mergeDeep = <T>(target: T, ...sources: Partial<T>[]): T => {
if (!sources.length) {
return target;
}
const source = sources.shift();
if (isObject(target) && isObject(source)) {
for (const key in source) {
// Prevent prototype pollution
if (!source.hasOwnProperty(key) || forbiddenProperties.includes(key)) {
continue;
}
const value = source[key];
if (isObject(value)) {
if (!target[key]) {
Object.assign(target, {
[key]: {},
});
}
mergeDeep(target[key], value);
} else {
Object.assign(target, {
[key]: source[key],
});
}
}
}
return mergeDeep<T>(target, ...sources);
};