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) {
|
2020-04-30 23:45:22 +02:00
|
|
|
|
|
|
|
// When calling recursively, this can be called without being and object or array in which case we just return the value
|
|
|
|
if (typeof object !== 'object') {
|
|
|
|
return object
|
|
|
|
}
|
|
|
|
|
2020-04-14 22:46:27 +02:00
|
|
|
let parsedObject = {}
|
|
|
|
for (const m in object) {
|
|
|
|
parsedObject[camelCase(m)] = object[m]
|
2020-04-26 15:04:58 +02:00
|
|
|
|
|
|
|
// Recursive processing
|
|
|
|
// Prevent processing for some cases
|
2020-09-05 22:35:52 +02:00
|
|
|
if (object[m] === null) {
|
2020-04-26 15:04:58 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call it again for arrays
|
|
|
|
if (Array.isArray(object[m])) {
|
|
|
|
parsedObject[camelCase(m)] = object[m].map(o => objectToCamelCase(o))
|
|
|
|
// Because typeof [] === 'object' is true for arrays, we leave the loop here to prevent converting arrays to objects.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-26 01:11:34 +02:00
|
|
|
// Call it again for nested objects
|
2020-09-05 22:35:52 +02:00
|
|
|
if (typeof object[m] === 'object') {
|
2020-04-26 15:04:58 +02:00
|
|
|
parsedObject[camelCase(m)] = objectToCamelCase(object[m])
|
2020-04-26 01:11:34 +02:00
|
|
|
}
|
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) {
|
2020-04-30 23:45:22 +02:00
|
|
|
|
|
|
|
// When calling recursively, this can be called without being and object or array in which case we just return the value
|
|
|
|
if (typeof object !== 'object') {
|
|
|
|
return object
|
|
|
|
}
|
|
|
|
|
2020-04-14 22:46:27 +02:00
|
|
|
let parsedObject = {}
|
|
|
|
for (const m in object) {
|
|
|
|
parsedObject[snakeCase(m)] = object[m]
|
2020-04-26 15:04:58 +02:00
|
|
|
|
|
|
|
// Recursive processing
|
|
|
|
// Prevent processing for some cases
|
2020-09-05 22:35:52 +02:00
|
|
|
if (
|
2020-04-26 15:04:58 +02:00
|
|
|
object[m] === null ||
|
|
|
|
(object[m] instanceof Date)
|
2020-04-26 01:11:34 +02:00
|
|
|
) {
|
2020-04-26 15:04:58 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call it again for arrays
|
|
|
|
if (Array.isArray(object[m])) {
|
|
|
|
parsedObject[snakeCase(m)] = object[m].map(o => objectToSnakeCase(o))
|
|
|
|
// Because typeof [] === 'object' is true for arrays, we leave the loop here to prevent converting arrays to objects.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call it again for nested objects
|
2020-09-05 22:35:52 +02:00
|
|
|
if (typeof object[m] === 'object') {
|
2020-04-26 15:04:58 +02:00
|
|
|
parsedObject[snakeCase(m)] = objectToSnakeCase(object[m])
|
2020-04-26 01:11:34 +02:00
|
|
|
}
|
2020-04-14 22:46:27 +02:00
|
|
|
}
|
2020-04-26 15:04:58 +02:00
|
|
|
|
2020-04-14 22:46:27 +02:00
|
|
|
return parsedObject
|
|
|
|
}
|