From 6c24cc66b202bbee4c1f36e77ad4093761e62b9d Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 26 Apr 2020 15:04:58 +0200 Subject: [PATCH] Fix parsing nested models --- src/helpers/case.js | 48 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/src/helpers/case.js b/src/helpers/case.js index e8c83aa0..50ab6c12 100644 --- a/src/helpers/case.js +++ b/src/helpers/case.js @@ -10,12 +10,23 @@ export function objectToCamelCase(object) { let parsedObject = {} for (const m in object) { parsedObject[camelCase(m)] = object[m] + + // Recursive processing + // Prevent processing for some cases + if(object[m] === null) { + 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 + } + // Call it again for nested objects - if( - typeof object[m] === 'object' && - object[m] !== null - ) { - object[m] = objectToCamelCase(object[m]) + if(typeof object[m] === 'object') { + parsedObject[camelCase(m)] = objectToCamelCase(object[m]) } } return parsedObject @@ -30,14 +41,31 @@ export function objectToSnakeCase(object) { let parsedObject = {} for (const m in object) { parsedObject[snakeCase(m)] = object[m] - // Call it again for nested objects + + // Recursive processing + // Prevent processing for some cases if( - typeof object[m] === 'object' && - object[m] !== null && - !(object[m] instanceof Date) + object[m] === null || + (object[m] instanceof Date) ) { - object[m] = objectToSnakeCase(object[m]) + 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 + if(typeof object[m] === 'object') { + parsedObject[snakeCase(m)] = objectToSnakeCase(object[m]) } } + + + console.log('end', parsedObject, object) + return parsedObject }