-
Notifications
You must be signed in to change notification settings - Fork 0
/
camelize.js
126 lines (107 loc) · 3.07 KB
/
camelize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
let str0 = "The gods have yet to make a man who lacks the patience for absolute power"; // normal string
let str1 = "Half BLOOD pRINCE"; // normal string with extra spaces
let str2 = "i_AM_IRON_man"; // snake case
let str3 = "tom-------mARVolo-------Riddle"; // hyphen
let str4 = "Spiderman---FAR frOM______home"; // mix of all
let str5 = "AManHasNoName"; // somewhat camelCase
let str6 = "godOfThunder "; // already camelCase
let str7 = "undefined null NaN "; // string contains unexpected words
let str8 = 123; // not a string
let str9 = "123 456 str a987BOY"; // string with numbers
let str10 = "--a__b c"; // string starting with symbols
// Half BLOOD pRINCE -> halfBloodPrince
// tom-------mARVolo-------Riddle -> tomMarvoloRiddle
function camelCase(str) {
if (!str || Number(str)) return str;
const strSymbolsRemoved = str.replace(/-/g, " ").replace(/_/g, " ");
const strSplittedBySpace = strSymbolsRemoved.split(" ");
const extraSpaceRemoved = strSplittedBySpace.filter(item => item !== "");
if (extraSpaceRemoved.length === 1) {
const item = extraSpaceRemoved[0];
return item[0].toLowerCase() + item.slice(1);
} else {
const res = extraSpaceRemoved.reduce((acc, item, idx) => {
const ans = (idx === 0 ? item[0].toLowerCase() : item[0].toUpperCase()) + item.slice(1).toLowerCase();
return acc + ans;
}, "");
return res;
}
}
// console.log("str0", camelCase(str0));
// console.log("str1", camelCase(str1));
// console.log("str2", camelCase(str2));
// console.log("str3", camelCase(str3));
// console.log("str4", camelCase(str4));
// console.log("str5", camelCase(str5));
// console.log("str6", camelCase(str6));
// console.log("str7", camelCase(str7));
// console.log("str8", camelCase(str8));
// console.log("str9", camelCase(str9));
// console.log("str10", camelCase(str10));
let obj = {
[str0]: "1",
[str1]: [
{ [str2]: 2 },
3,
{
[str3]: "4",
[str4]: [
{ [str5]: 5 }
]
},
["6", 7]
],
[str6]: {
[str7]: {
[str8]: 8,
[str9]: {
[str10]: "9"
}
}
},
5: "10"
};
const obj2 = [
{ i_d: 1 },
{ i_d: 2 },
{ i_d: 3 },
"yo",
{
o_ne: "1",
t_wo: "2",
th_ree: [
{ i_d: 1 },
{ i_d: 2 },
]
},
["sa", "a"]
]
let obj3 = [1, 2, 3];
let obj4 = {
0: 0,
1: 1,
2: 2,
3: 3
}
function camelizeObject(obj) {
let newObj;
if (typeof obj === "object") {
newObj = {};
if (Array.isArray(obj)) newObj = [];
} else {
return obj;
}
Object.keys(obj).forEach((key) => {
const val = obj[key];
const camelKey = camelCase(key);
if (typeof val === "object") {
newObj[camelKey] = camelizeObject(val);
} else {
newObj[camelKey] = val;
}
});
return newObj;
}
const res = camelizeObject(obj4);
console.log("obj", (obj4))
console.log("res", (res))