diff --git a/.eslintrc.js b/.eslintrc.js index 6bf880fc3..0f33f43f4 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -72,7 +72,8 @@ module.exports = { }, ], 'object-shorthand': 'error', - 'arrow-body-style': ["error", "as-needed"] + 'arrow-body-style': ["error", "as-needed"], + 'guard-for-in': 'error' }, overrides: [ // Configuration for translations files (i18next) diff --git a/src/api/common/utils.tsx b/src/api/common/utils.tsx index a02f0cfc2..efc5bd3fd 100644 --- a/src/api/common/utils.tsx +++ b/src/api/common/utils.tsx @@ -51,11 +51,13 @@ type GenericObject = { [key: string]: unknown }; export const toCamelCase = (obj: GenericObject): GenericObject => { const newObj: GenericObject = {}; for (const key in obj) { - if (key.includes('_')) { - const newKey = key.replace(/_([a-z])/g, (g) => g[1].toUpperCase()); - newObj[newKey] = obj[key]; - } else { - newObj[key] = obj[key]; + if (obj.hasOwnProperty(key)) { + if (key.includes('_')) { + const newKey = key.replace(/_([a-z])/g, (g) => g[1].toUpperCase()); + newObj[newKey] = obj[key]; + } else { + newObj[key] = obj[key]; + } } } return newObj; @@ -64,16 +66,18 @@ export const toCamelCase = (obj: GenericObject): GenericObject => { export const toSnakeCase = (obj: GenericObject): GenericObject => { const newObj: GenericObject = {}; for (const key in obj) { - let newKey = key.match(/([A-Z])/g) - ? key - .match(/([A-Z])/g)! - .reduce( - (str, c) => str.replace(new RegExp(c), '_' + c.toLowerCase()), - key - ) - : key; - newKey = newKey.substring(key.slice(0, 1).match(/([A-Z])/g) ? 1 : 0); - newObj[newKey] = obj[key]; + if (obj.hasOwnProperty(key)) { + let newKey = key.match(/([A-Z])/g) + ? key + .match(/([A-Z])/g)! + .reduce( + (str, c) => str.replace(new RegExp(c), '_' + c.toLowerCase()), + key + ) + : key; + newKey = newKey.substring(key.slice(0, 1).match(/([A-Z])/g) ? 1 : 0); + newObj[newKey] = obj[key]; + } } return newObj; };