Skip to content

Commit 01b9261

Browse files
matthiaskdr-rompecabezas
authored andcommitted
Replace forEach loops with for...of loops
1 parent 4b05d1f commit 01b9261

File tree

4 files changed

+61
-60
lines changed

4 files changed

+61
-60
lines changed

biome.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
"linter": {
1111
"enabled": true,
1212
"rules": {
13-
"recommended": true,
14-
"complexity": {
15-
"noForEach": "off"
16-
}
13+
"recommended": true
1714
}
1815
},
1916
"javascript": {

debug_toolbar/static/debug_toolbar/js/history.js

+14-16
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ function difference(setA, setB) {
1414
* Create an array of dataset properties from a NodeList.
1515
*/
1616
function pluckData(nodes, key) {
17-
const data = [];
18-
nodes.forEach((obj) => {
19-
data.push(obj.dataset[key]);
20-
});
21-
return data;
17+
return [...nodes].map((obj) => obj.dataset[key]);
2218
}
2319

2420
function refreshHistory() {
@@ -31,12 +27,14 @@ function refreshHistory() {
3127
ajaxForm(formTarget)
3228
.then((data) => {
3329
// Remove existing rows first then re-populate with new data
34-
container.querySelectorAll("tr[data-store-id]").forEach((node) => {
30+
for (const node of container.querySelectorAll(
31+
"tr[data-store-id]"
32+
)) {
3533
node.remove();
36-
});
37-
data.requests.forEach((request) => {
34+
}
35+
for (const request of data.requests) {
3836
container.innerHTML = request.content + container.innerHTML;
39-
});
37+
}
4038
})
4139
.then(() => {
4240
const allIds = new Set(
@@ -54,18 +52,18 @@ function refreshHistory() {
5452
};
5553
})
5654
.then((refreshInfo) => {
57-
refreshInfo.newIds.forEach((newId) => {
55+
for (const newId of refreshInfo.newIds) {
5856
const row = container.querySelector(
5957
`tr[data-store-id="${newId}"]`
6058
);
6159
row.classList.add("flash-new");
62-
});
60+
}
6361
setTimeout(() => {
64-
container
65-
.querySelectorAll("tr[data-store-id]")
66-
.forEach((row) => {
67-
row.classList.remove("flash-new");
68-
});
62+
for (const row of container.querySelectorAll(
63+
"tr[data-store-id]"
64+
)) {
65+
row.classList.remove("flash-new");
66+
}
6967
}, 2000);
7068
});
7169
}

debug_toolbar/static/debug_toolbar/js/toolbar.js

+33-31
Original file line numberDiff line numberDiff line change
@@ -117,29 +117,31 @@ const djdt = {
117117
const openMe = this.textContent === toggleOpen;
118118
const name = this.dataset.toggleName;
119119
const container = document.getElementById(`${name}_${id}`);
120-
container.querySelectorAll(".djDebugCollapsed").forEach((e) => {
121-
$$.toggle(e, openMe);
122-
});
123-
container.querySelectorAll(".djDebugUncollapsed").forEach((e) => {
124-
$$.toggle(e, !openMe);
125-
});
126-
this.closest(".djDebugPanelContent")
127-
.querySelectorAll(`.djToggleDetails_${id}`)
128-
.forEach((e) => {
129-
if (openMe) {
130-
e.classList.add("djSelected");
131-
e.classList.remove("djUnselected");
132-
this.textContent = toggleClose;
133-
} else {
134-
e.classList.remove("djSelected");
135-
e.classList.add("djUnselected");
136-
this.textContent = toggleOpen;
137-
}
138-
const switch_ = e.querySelector(".djToggleSwitch");
139-
if (switch_) {
140-
switch_.textContent = this.textContent;
141-
}
142-
});
120+
for (const el of container.querySelectorAll(".djDebugCollapsed")) {
121+
$$.toggle(el, openMe);
122+
}
123+
for (const el of container.querySelectorAll(
124+
".djDebugUncollapsed"
125+
)) {
126+
$$.toggle(el, !openMe);
127+
}
128+
for (const el of this.closest(
129+
".djDebugPanelContent"
130+
).querySelectorAll(`.djToggleDetails_${id}`)) {
131+
if (openMe) {
132+
el.classList.add("djSelected");
133+
el.classList.remove("djUnselected");
134+
this.textContent = toggleClose;
135+
} else {
136+
el.classList.remove("djSelected");
137+
el.classList.add("djUnselected");
138+
this.textContent = toggleOpen;
139+
}
140+
const switch_ = el.querySelector(".djToggleSwitch");
141+
if (switch_) {
142+
switch_.textContent = this.textContent;
143+
}
144+
}
143145
});
144146

145147
$$.on(djDebug, "click", "#djHideToolBarButton", (event) => {
@@ -236,12 +238,12 @@ const djdt = {
236238
hidePanels() {
237239
const djDebug = getDebugElement();
238240
$$.hide(document.getElementById("djDebugWindow"));
239-
djDebug.querySelectorAll(".djdt-panelContent").forEach((e) => {
240-
$$.hide(e);
241-
});
242-
document.querySelectorAll("#djDebugToolbar li").forEach((e) => {
243-
e.classList.remove("djdt-active");
244-
});
241+
for (const el of djDebug.querySelectorAll(".djdt-panelContent")) {
242+
$$.hide(el);
243+
}
244+
for (const el of document.querySelectorAll("#djDebugToolbar li")) {
245+
el.classList.remove("djdt-active");
246+
}
245247
},
246248
ensureHandleVisibility() {
247249
const handle = document.getElementById("djDebugToolbarHandle");
@@ -340,10 +342,10 @@ const djdt = {
340342
const cookieArray = document.cookie.split("; ");
341343
const cookies = {};
342344

343-
cookieArray.forEach((e) => {
345+
for (const e of cookieArray) {
344346
const parts = e.split("=");
345347
cookies[parts[0]] = parts[1];
346-
});
348+
}
347349

348350
return cookies[key];
349351
},

debug_toolbar/static/debug_toolbar/js/utils.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,33 @@ const $$ = {
4040
return !element.classList.contains("djdt-hidden");
4141
},
4242
executeScripts(scripts) {
43-
scripts.forEach((script) => {
43+
for (const script of scripts) {
4444
const el = document.createElement("script");
4545
el.type = "module";
4646
el.src = script;
4747
el.async = true;
4848
document.head.appendChild(el);
49-
});
49+
}
5050
},
5151
applyStyles(container) {
5252
/*
5353
* Given a container element, apply styles set via data-djdt-styles attribute.
5454
* The format is data-djdt-styles="styleName1:value;styleName2:value2"
5555
* The style names should use the CSSStyleDeclaration camel cased names.
5656
*/
57-
container.querySelectorAll("[data-djdt-styles]").forEach((element) => {
57+
for (const element of container.querySelectorAll(
58+
"[data-djdt-styles]"
59+
)) {
5860
const styles = element.dataset.djdtStyles || "";
59-
styles.split(";").forEach((styleText) => {
61+
for (const styleText of styles.split(";")) {
6062
const styleKeyPair = styleText.split(":");
6163
if (styleKeyPair.length === 2) {
6264
const name = styleKeyPair[0].trim();
6365
const value = styleKeyPair[1].trim();
6466
element.style[name] = value;
6567
}
66-
});
67-
});
68+
}
69+
}
6870
},
6971
};
7072

@@ -111,14 +113,14 @@ function replaceToolbarState(newStoreId, data) {
111113
const djDebug = document.getElementById("djDebug");
112114
djDebug.setAttribute("data-store-id", newStoreId);
113115
// Check if response is empty, it could be due to an expired storeId.
114-
Object.keys(data).forEach((panelId) => {
116+
for (const panelId of Object.keys(data)) {
115117
const panel = document.getElementById(panelId);
116118
if (panel) {
117119
panel.outerHTML = data[panelId].content;
118120
document.getElementById(`djdt-${panelId}`).outerHTML =
119121
data[panelId].button;
120122
}
121-
});
123+
}
122124
}
123125

124126
function debounce(func, delay) {
@@ -129,7 +131,9 @@ function debounce(func, delay) {
129131
clearTimeout(timer);
130132
timer = setTimeout(() => {
131133
const result = func(...args);
132-
resolves.forEach((r) => r(result));
134+
for (const r of resolves) {
135+
r(result);
136+
}
133137
resolves = [];
134138
}, delay);
135139

0 commit comments

Comments
 (0)