-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert job launcher client ingoing and outgoing messages to snake_case
- Loading branch information
Showing
2 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/apps/job-launcher/client/src/utils/case-converter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export class CaseConverter { | ||
static transformToCamelCase(obj: any): any { | ||
if (Array.isArray(obj)) { | ||
return obj.map((item) => CaseConverter.transformToCamelCase(item)); | ||
} else if (typeof obj === 'object' && obj !== null) { | ||
return Object.keys(obj).reduce( | ||
(acc: Record<string, any>, key: string) => { | ||
const camelCaseKey = key.replace(/_([a-z])/g, (g) => | ||
g[1].toUpperCase(), | ||
); | ||
acc[camelCaseKey] = CaseConverter.transformToCamelCase(obj[key]); | ||
return acc; | ||
}, | ||
{}, | ||
); | ||
} else { | ||
return obj; | ||
} | ||
} | ||
|
||
static transformToSnakeCase(obj: any): any { | ||
if (Array.isArray(obj)) { | ||
return obj.map((item) => CaseConverter.transformToSnakeCase(item)); | ||
} else if (typeof obj === 'object' && obj !== null) { | ||
return Object.keys(obj).reduce( | ||
(acc: Record<string, any>, key: string) => { | ||
const snakeCaseKey = key.replace(/([A-Z])/g, '_$1').toLowerCase(); | ||
acc[snakeCaseKey] = CaseConverter.transformToSnakeCase(obj[key]); | ||
return acc; | ||
}, | ||
{}, | ||
); | ||
} else { | ||
return obj; | ||
} | ||
} | ||
} |