Skip to content

Commit bda7bcd

Browse files
authored
convertCamelToSnakeCase function added (#9)
1 parent 11a34e4 commit bda7bcd

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

case-types-parser.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,28 @@ export function capitalizeFirstForClassName(str: string) {
2424
return parts.join("");
2525
}
2626

27+
function toSnakeCase(key) {
28+
return key.replace(/([A-Z])/g, "_$1").toLowerCase();
29+
}
30+
31+
export function convertCamelToSnakeCase(obj) {
32+
const newObj = {};
33+
Object.keys(obj).forEach((key) => {
34+
const newKey = toSnakeCase(key);
35+
const value = obj[key];
36+
if (value && typeof value === 'object' && !Array.isArray(value)) {
37+
newObj[newKey] = convertCamelToSnakeCase(value);
38+
} else {
39+
newObj[newKey] = value;
40+
}
41+
});
42+
return newObj;
43+
}
44+
2745
export function camelCaseToDashCase(str: string) {
2846
return str.replace(/[A-Z]/g, m => "-" + m.toLowerCase());
2947
}
48+
3049
export function snakeCaseToCamelCase(str: string) {
3150
return str.toLowerCase().replace(/([_][a-z])/g, group =>
3251
group
@@ -35,7 +54,6 @@ export function snakeCaseToCamelCase(str: string) {
3554
);
3655
}
3756

38-
3957
export function convertVarToCamel(obj: any) {
4058
if (isDict(obj)) {
4159
const n = {};
@@ -54,7 +72,6 @@ export function convertVarToCamel(obj: any) {
5472
return obj;
5573
}
5674

57-
5875
export enum caseType {
5976
LOWER,
6077
UPPER,

0 commit comments

Comments
 (0)