Skip to content

Commit c6651a2

Browse files
Merge pull request #67 from rootstrap/chore/sonar_eslint
chore: guard-for-in
2 parents 92d09e7 + d12e86f commit c6651a2

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ module.exports = {
7272
},
7373
],
7474
'object-shorthand': 'error',
75-
'arrow-body-style': ["error", "as-needed"]
75+
'arrow-body-style': ["error", "as-needed"],
76+
'guard-for-in': 'error'
7677
},
7778
overrides: [
7879
// Configuration for translations files (i18next)

src/api/common/utils.tsx

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ type GenericObject = { [key: string]: unknown };
5151
export const toCamelCase = (obj: GenericObject): GenericObject => {
5252
const newObj: GenericObject = {};
5353
for (const key in obj) {
54-
if (key.includes('_')) {
55-
const newKey = key.replace(/_([a-z])/g, (g) => g[1].toUpperCase());
56-
newObj[newKey] = obj[key];
57-
} else {
58-
newObj[key] = obj[key];
54+
if (obj.hasOwnProperty(key)) {
55+
if (key.includes('_')) {
56+
const newKey = key.replace(/_([a-z])/g, (g) => g[1].toUpperCase());
57+
newObj[newKey] = obj[key];
58+
} else {
59+
newObj[key] = obj[key];
60+
}
5961
}
6062
}
6163
return newObj;
@@ -64,16 +66,18 @@ export const toCamelCase = (obj: GenericObject): GenericObject => {
6466
export const toSnakeCase = (obj: GenericObject): GenericObject => {
6567
const newObj: GenericObject = {};
6668
for (const key in obj) {
67-
let newKey = key.match(/([A-Z])/g)
68-
? key
69-
.match(/([A-Z])/g)!
70-
.reduce(
71-
(str, c) => str.replace(new RegExp(c), '_' + c.toLowerCase()),
72-
key
73-
)
74-
: key;
75-
newKey = newKey.substring(key.slice(0, 1).match(/([A-Z])/g) ? 1 : 0);
76-
newObj[newKey] = obj[key];
69+
if (obj.hasOwnProperty(key)) {
70+
let newKey = key.match(/([A-Z])/g)
71+
? key
72+
.match(/([A-Z])/g)!
73+
.reduce(
74+
(str, c) => str.replace(new RegExp(c), '_' + c.toLowerCase()),
75+
key
76+
)
77+
: key;
78+
newKey = newKey.substring(key.slice(0, 1).match(/([A-Z])/g) ? 1 : 0);
79+
newObj[newKey] = obj[key];
80+
}
7781
}
7882
return newObj;
7983
};

0 commit comments

Comments
 (0)