Skip to content

Commit

Permalink
fix: camel to snake case
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jul 23, 2023
1 parent fddcd6e commit 9f61562
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
32 changes: 21 additions & 11 deletions src/helpers/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,28 @@ export function snakeCaseToCamelCase(obj: Record<string, any>): void {
Object.entries(obj).forEach(([snakeKey, value]) => {
if (typeof value === 'object') snakeCaseToCamelCase(value);

const keys = snakeKey.split('_').filter((key) => key !== '');
const camelKey = snakeKey.replace(
/_[a-z]/g,
(_char) => _char[1].toUpperCase()
);

if (keys.length >= 1) {
const camelCaseKey = keys.reduce((previousValue, currentValue) =>
previousValue +
currentValue.charAt(0).toUpperCase() +
currentValue.slice(1)
);
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete obj[snakeKey];
obj[camelKey] = value;
});
}

// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete obj[snakeKey];
obj[camelCaseKey] = value;
}
export function camelCaseToSnakeCase(obj: Record<string, any>): void {
Object.entries(obj).forEach(([camelKey, value]) => {
if (typeof value === 'object') camelCaseToSnakeCase(value);

const snakeKey = camelKey.replace(
/[A-Z]/g,
(char) => `_${char.toLowerCase()}`
);

// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete obj[camelKey];
obj[snakeKey] = value;
});
}
4 changes: 3 additions & 1 deletion src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
wrap,
path,
Path,
snakeCaseToCamelCase
snakeCaseToCamelCase,
camelCaseToSnakeCase
} from './general';
import {
insertDividerBetweenElements,
Expand All @@ -20,6 +21,7 @@ export {
wrap,
path, type Path,
snakeCaseToCamelCase,
camelCaseToSnakeCase,
// materialUI
insertDividerBetweenElements,
getStyleOverrides,
Expand Down

0 comments on commit 9f61562

Please sign in to comment.