-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
44 lines (38 loc) · 1.04 KB
/
utils.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
function jsonStringify(data) {
return Array.isArray(data)
? '[' + data.map((e) => JSON.stringify(e)).join(',\n ') + ']'
: JSON.stringify(data)
}
// function renameKeys(data) {
// return data.map((obj) =>
// Object.entries(obj).map(([key, value]) => ({ [camelCase(key)]: value }))
// )
// }
function renameKeys(data) {
return data.map((obj) =>
Object.entries(obj).reduce((obj2, [key, value]) => {
obj2[camelCase(key)] = value
return obj2
}, {})
)
}
function camelCase(key) {
return key.substring(0, 1).toLowerCase() + key.substring(1)
}
function headerName(key) {
return (key.charAt(0).toUpperCase() + key.slice(1))
.split(/(?=[A-Z])/)
.join(' ')
}
function cleanProps(obj) {
for (const prop in obj) {
if (
obj[prop] === undefined ||
obj[prop] === null ||
obj[prop].length === 0
)
delete obj[prop]
}
return obj
}
export { jsonStringify, renameKeys, cleanProps, headerName }