Skip to content

Commit 21361ec

Browse files
committed
Improved readability in tconditionsMapper
chore: switched from for-loop to entries-iteration chore: moved utility functions isObject and isArray to outside of the conditionsMapper
1 parent 99d883f commit 21361ec

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

packages/react-form-renderer/src/files/conditions-mapper.js

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
several condition arrays.
88
*/
99

10-
export const conditionsMapper = ({conditions}) => {
11-
if (!conditions) return {};
10+
function isObject(obj) {
11+
return obj !== null && typeof obj === 'object' && !Array.isArray(obj);
12+
}
1213

13-
function isObject(obj) {
14-
return obj !== null && typeof obj === 'object' && !Array.isArray(obj);
15-
}
14+
function isArray(obj) {
15+
return Array.isArray(obj);
16+
}
1617

17-
function isArray(obj) {
18-
return Array.isArray(obj);
19-
}
18+
export const conditionsMapper = ({conditions}) => {
19+
if (!conditions) return {};
2020

2121
function traverse({obj, fnc, key}) {
2222
fnc && fnc({obj, key});
@@ -37,27 +37,23 @@ export const conditionsMapper = ({conditions}) => {
3737
}
3838

3939
function traverseArray({obj, fnc, key}) {
40-
for (var index = 0, len = obj.length; index < len; index++) {
41-
const item = obj[index];
40+
obj.forEach(([key, item]) => {
4241
traverse({
4342
obj: item,
4443
fnc,
45-
key: index,
44+
key,
4645
});
47-
}
46+
});
4847
}
4948

5049
function traverseObject({obj, fnc, key}) {
51-
for (var index in obj) {
52-
if (obj.hasOwnProperty(index)) {
53-
const item = obj[index];
54-
traverse({
55-
obj: item,
56-
fnc,
57-
key: index,
58-
});
59-
}
60-
}
50+
Object.entries(obj).forEach(([key, item]) => {
51+
traverse({
52+
obj: item,
53+
fnc,
54+
key,
55+
});
56+
});
6157
}
6258

6359
const indexedConditions = {};

0 commit comments

Comments
 (0)