2020-04-14 22:46:27 +02:00
|
|
|
import {camelCase} from 'camel-case'
|
|
|
|
import {snakeCase} from 'snake-case'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms field names to camel case.
|
|
|
|
* @param object
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
export function objectToCamelCase(object) {
|
|
|
|
let parsedObject = {}
|
|
|
|
for (const m in object) {
|
|
|
|
parsedObject[camelCase(m)] = object[m]
|
2020-04-26 01:11:34 +02:00
|
|
|
// Call it again for nested objects
|
|
|
|
if(
|
|
|
|
typeof object[m] === 'object' &&
|
|
|
|
object[m] !== null
|
|
|
|
) {
|
|
|
|
object[m] = objectToCamelCase(object[m])
|
|
|
|
}
|
2020-04-14 22:46:27 +02:00
|
|
|
}
|
|
|
|
return parsedObject
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms field names to snake case - used before making an api request.
|
|
|
|
* @param object
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
export function objectToSnakeCase(object) {
|
|
|
|
let parsedObject = {}
|
|
|
|
for (const m in object) {
|
|
|
|
parsedObject[snakeCase(m)] = object[m]
|
2020-04-26 01:11:34 +02:00
|
|
|
// Call it again for nested objects
|
|
|
|
if(
|
|
|
|
typeof object[m] === 'object' &&
|
|
|
|
object[m] !== null &&
|
|
|
|
!(object[m] instanceof Date)
|
|
|
|
) {
|
|
|
|
object[m] = objectToSnakeCase(object[m])
|
|
|
|
}
|
2020-04-14 22:46:27 +02:00
|
|
|
}
|
|
|
|
return parsedObject
|
|
|
|
}
|