-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
33 lines (30 loc) · 924 Bytes
/
index.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
const toCase = (stringToCaseFn) => (value) => {
const convertFn = toCase(stringToCaseFn)
if (value === null) {
return null
} else if (typeof value === 'string') {
return stringToCaseFn(value)
} else if (value.length !== undefined) {
return value.map(convertFn)
} else if (typeof value === 'object') {
const object = {}
Object.keys(value).forEach((key) => {
let _value = value[key]
if (typeof _value === 'object') {
_value = convertFn(value[key])
}
object[convertFn(key)] = _value
})
return object
} else {
return value
}
}
const snakeStringToCamel = (str) => str.replace(/_(\w)/g, (m) => m[1].toUpperCase())
const snakeToCamel = toCase(snakeStringToCamel)
const camelStringToSnake = (str) => str.replace(/[A-Z]/g, m => `_${m.toLowerCase()}`)
const camelToSnake = toCase(camelStringToSnake)
module.exports = {
snakeToCamel,
camelToSnake,
}