The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/apps/meteor/ee/server/lib/ldap/replacesNestedValues.ts

26 lines
794 B

export const replacesNestedValues = (obj: Record<string, unknown>, key: string, value: unknown): Record<string, unknown> => {
const keys = key.split('.');
const lastKey = keys.shift();
if (!lastKey) {
throw new Error(`Failed to assign custom field: ${key}`);
}
if (keys.length && obj[lastKey] !== undefined && (typeof obj[lastKey] !== 'object' || Array.isArray(obj[lastKey]))) {
throw new Error(`Failed to assign custom field: ${key}`);
}
if (keys.length === 0 && typeof obj[lastKey] === 'object') {
throw new Error(`Failed to assign custom field: ${key}`);
}
return {
...obj,
...(keys.length === 0 && {
[lastKey]: value,
}),
...(keys.length > 0 && {
[lastKey]: replacesNestedValues(obj[lastKey] as Record<string, unknown>, keys.join('.'), value),
}),
};
};