mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 10:44:55 +00:00
Add camelCaseConverter helper function
This commit is contained in:
20
libs/common/src/helper/camelCaseConverter.ts
Normal file
20
libs/common/src/helper/camelCaseConverter.ts
Normal file
@ -0,0 +1,20 @@
|
||||
export function convertKeysToCamelCase(obj: any): any {
|
||||
if (!obj || typeof obj !== 'object') {
|
||||
return obj;
|
||||
}
|
||||
|
||||
if (Array.isArray(obj)) {
|
||||
return obj.map(convertKeysToCamelCase);
|
||||
}
|
||||
|
||||
const camelCaseObj: { [key: string]: any } = {};
|
||||
|
||||
for (const key of Object.keys(obj)) {
|
||||
const camelCaseKey = key.replace(/_([a-z])/g, (_, letter) =>
|
||||
letter.toUpperCase(),
|
||||
);
|
||||
camelCaseObj[camelCaseKey] = convertKeysToCamelCase(obj[key]);
|
||||
}
|
||||
|
||||
return camelCaseObj;
|
||||
}
|
||||
Reference in New Issue
Block a user