-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-object-keys.mjs
39 lines (34 loc) · 1 KB
/
convert-object-keys.mjs
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
function snake_case(obj) {
return Object.fromEntries
(
Object.entries(obj).map(([key, value]) => {
const newKey = key.replace(/[A-Z]/g, (letter) => '_' + letter.toLowerCase());
return [newKey, value]
})
)
}
function camelCase(obj) {
return Object.fromEntries
(
Object.entries(obj).map(([key, value]) => {
const newKey = key.replace(/[-_][a-z]/g, (letter) => letter.toUpperCase().replace(/-|_/g, ''))
return [newKey, value]
})
)
}
const obj = {
fullName: 'Mahdi Rezazadeh',
birthDate: '1996/12/28'
}
// const snake_case_obj = snake_case(obj);
// console.log('snake_case_obj: ', snake_case_obj);
// const newSample = {
// 'full-name': 'Mahdi Rez',
// 'birth-date': '12313 123123'
// }
// const camelCaseObj = camelCase(newSample);
// console.log('camelCaseObj: ', camelCaseObj);
console.log(mahdi);
var mahdi = function ali() {
return 'mahdi function'
}