Skip to content

Commit

Permalink
Rewrite Array.forEach to for ... of
Browse files Browse the repository at this point in the history
  • Loading branch information
jake-low committed Feb 20, 2025
1 parent 36b697e commit a4666f9
Show file tree
Hide file tree
Showing 20 changed files with 77 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ export default class UserActivityTimelineWidget extends Component {
// descriptions into a Map (tracking the total count of the dups), so we
// can keep the timeline tidy and show a count instead of simply repeating
// the same action multiple times for a challenge.
groupedByDate.forEach((dateGroup) => {
for (const dateGroup of groupedByDate) {
// Group activities on this day by challenge
const groupedByChallenge = _toPairs(_groupBy(dateGroup[1], "challengeId"));

// Consolidate duplicate activities for each challenge into Map
groupedByChallenge.forEach((challengeGroup) => {
for (const challengeGroup of groupedByChallenge) {
challengeGroup[1] = challengeGroup[1].reduce(
(challengeEntries, entry) =>
challengeEntries.set(
Expand All @@ -98,9 +98,9 @@ export default class UserActivityTimelineWidget extends Component {
),
new Map(),
);
});
}
dateGroup[1] = groupedByChallenge;
});
}

// Sort date groups in descending date order.
const latestEntries = _reverse(_sortBy(groupedByDate, (pairs) => parseISO(pairs[0]).getTime()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,9 @@ async function convertAndBundleGeoJson(challenge) {
data.features = [];

if (allLines?.length) {
allLines.forEach((taskLine) => {
JSON.parse(taskLine).features.forEach((feature) => {
data.features.push(feature);
});
});
for (const taskLine of allLines) {
data.features.push(...JSON.parse(taskLine).features);
}
}
} else if (challenge.localGeoJSON) {
data = JSON.parse(challenge.localGeoJSON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ const mapDispatchToProps = (dispatch, ownProps) => ({
deleteChallenges: (challengeIds, callback = () => null) => {
const projectId = ownProps.match.params.projectId;

challengeIds.forEach((id) => {
for (const id of challengeIds) {
dispatch(deleteChallenge(id));
});
}

callback(projectId);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export const WithWidgetWorkspacesInternal = function (
// Generate a simple layout if none provided, with one widget per row
if (configuration.layout.length === 0) {
let nextY = 0;
configuration.widgets.forEach((widgetConf) => {
for (const widgetConf of configuration.widgets) {
configuration.layout.push({
i: generateWidgetId(),
x: 0,
Expand All @@ -144,11 +144,11 @@ export const WithWidgetWorkspacesInternal = function (
});

nextY += widgetConf.defaultHeight;
});
}
} else {
// A layout was provided. If heights and/or widths were omitted or don't meet
// current minimums, fill them in from the widget descriptors
configuration.layout.forEach((widgetLayout, index) => {
for (const [index, widgetLayout] of configuration.layout.entries()) {
if (!configuration.widgets || !configuration.widgets[index]) {
return;
}
Expand All @@ -172,7 +172,7 @@ export const WithWidgetWorkspacesInternal = function (
) {
widgetLayout.h = descriptor.minHeight;
}
});
}
}

// Make sure workspace is upgraded to latest data model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,39 +344,39 @@ export class ActiveTaskControls extends Component {
? this.props.getUserAppSetting(this.props.user, "isEditMode")
: false;
if (!_isEmpty(this.props.activeKeyboardShortcuts?.[hiddenShortcutGroup]) && editMode) {
hiddenShortcuts.forEach((shortcut) => {
for (const shortcut of hiddenShortcuts) {
this.props.deactivateKeyboardShortcut(
hiddenShortcutGroup,
shortcut,
this.handleKeyboardShortcuts,
);
});
}
} else if (
_isEmpty(this.props.activeKeyboardShortcuts?.[hiddenShortcutGroup]) &&
this.props.keyboardShortcutGroups &&
this.props.activateKeyboardShortcut &&
!editMode
) {
hiddenShortcuts.forEach((shortcut) => {
for (const shortcut of hiddenShortcuts) {
this.props.activateKeyboardShortcut(
hiddenShortcutGroup,
_pick(this.props.keyboardShortcutGroups.taskCompletion, shortcut),
this.handleKeyboardShortcuts,
);
});
}
}
}
}

componentWillUnmount() {
if (!_isEmpty(this.props.activeKeyboardShortcuts?.[hiddenShortcutGroup])) {
hiddenShortcuts.forEach((shortcut) => {
for (const shortcut of hiddenShortcuts) {
this.props.deactivateKeyboardShortcut(
hiddenShortcutGroup,
shortcut,
this.handleKeyboardShortcuts,
);
});
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/components/TaskPane/TaskMap/TaskMap.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ export const TaskMapContent = (props) => {
});

if (intraLayerMatches.length > 0) {
orderedFeatureLayers(intraLayerMatches).forEach((match) => {
for (const match of orderedFeatureLayers(intraLayerMatches)) {
candidateLayers.set(match.description, match);
});
}
}
}
});
Expand Down
8 changes: 4 additions & 4 deletions src/components/Widgets/TaskBundleWidget/TaskBundleWidget.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ export default class TaskBundleWidget extends Component {

if (selectedArray.length > 1) {
if (AsCooperativeWork(this.props.task).isCooperative()) {
selectedArray.forEach((item) => {
for (const item of selectedArray) {
if (!AsCooperativeWork(item).isCooperative()) {
bundleTypeMismatch = "cooperative";
}
});
}
} else {
selectedArray.forEach((item) => {
for (const item of selectedArray) {
if (AsCooperativeWork(item).isCooperative()) {
bundleTypeMismatch = "notCooperative";
}
});
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/interactions/Task/AsMappableTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ export class AsMappableTask {

let allProperties = {};

features.forEach((feature) => {
for (const feature of features) {
if (feature?.properties) {
allProperties = Object.assign(allProperties, feature.properties);
}
});
}

return allProperties;
}
Expand All @@ -117,11 +117,11 @@ export class AsMappableTask {

let allProperties = [];

features.forEach((feature) => {
for (const feature of features) {
if (feature?.properties) {
allProperties.push(feature);
}
});
}

return allProperties;
}
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Inbox/NotificationHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import _map from "lodash/map";
import { useCallback, useMemo, useState } from "react";

export const useNotificationSelection = (notifications) => {
notifications.forEach((notification) => {
for (const notification of notifications) {
if (!notification.taskId && notification.challengeId) {
notification.taskId = notification.challengeName;
}
});
}

const [groupByTask, setGroupByTask] = useState(true);
const [selectedNotifications, setSelectedNotifications] = useState(new Set());
Expand Down
9 changes: 4 additions & 5 deletions src/pages/Profile/UserSettings/NotificationSettingsSchema.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,10 @@ export const jsSchema = (intl) => {

// items are generated as array from all subscription and count types
const notificationObject = {};
items
.filter((item) => Boolean(item.name))
.forEach((item) => {
notificationObject[item.name] = item;
});

for (const item of items.filter((item) => Boolean(item.name))) {
notificationObject[item.name] = item;
}

return {
$schema: "http://json-schema.org/draft-07/schema#",
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Profile/UserSettings/UserSettings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ class UserSettings extends Component {
editableUser.customBasemaps,
(data) => _isEmpty(_trim(data.name)) || _isEmpty(_trim(data.url)),
);
editableUser.customBasemaps.forEach((data) => {
for (const data of editableUser.customBasemaps) {
if (!data.id) {
data.id = -1;
}
});
}
}

editableUser.normalizeDefaultBasemap(LayerSources, editableUser.customBasemaps);
Expand Down
8 changes: 4 additions & 4 deletions src/services/Challenge/Challenge.js
Original file line number Diff line number Diff line change
Expand Up @@ -1212,9 +1212,9 @@ export const moveChallenges = function (challengeIds, toProjectId) {
})
.execute()
.then(() => {
challengeIds.forEach((id) => {
for (const id of challengeIds) {
fetchChallenge(id)(dispatch);
});
}
})
.catch((error) => {
if (isSecurityError(error)) {
Expand Down Expand Up @@ -1376,7 +1376,7 @@ const removeChallengeKeywords = function (challengeId, oldKeywords = []) {
const reduceChallengesFurther = function (mergedState, oldState, challengeEntities) {
// The generic reduction will merge arrays and objects, but for some fields
// we want to simply overwrite with the latest data.
challengeEntities.forEach((entity) => {
for (const entity of challengeEntities) {
// Until we implement undelete, ignore deleted challenges.
if (entity.deleted) {
delete mergedState[entity.id];
Expand Down Expand Up @@ -1414,7 +1414,7 @@ const reduceChallengesFurther = function (mergedState, oldState, challengeEntiti
if (Array.isArray(entity.presets)) {
mergedState[entity.id].presets = entity.presets;
}
});
}
};

// redux reducers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ describe("isCooperative", () => {
});

test("returns true for valid, active types", () => {
_values(CooperativeType).forEach((cooperativeType) => {
for (const cooperativeType of Object.values(CooperativeType)) {
if (cooperativeType !== CooperativeType.none) {
expect(isCooperative(cooperativeType)).toBe(true);
}
});
}
});
});
8 changes: 5 additions & 3 deletions src/services/Editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ export const osmObjectParams = function (
) {
const allTasks = Array.isArray(task) ? task : [task];
let objects = [];
allTasks.forEach((task) => {
for (const task of allTasks) {
if (task.geometries?.features) {
objects = objects.concat(
_compact(
Expand Down Expand Up @@ -395,7 +395,7 @@ export const osmObjectParams = function (
),
);
}
});
}

return objects.join(joinSeparator);
};
Expand Down Expand Up @@ -663,7 +663,9 @@ export const sendJOSMCommand = function (uri) {
const executeJOSMBatch = async function (commands, transmissionDelay = 1000) {
// For Safari we execute all the commands immediately
if (window.safari) {
commands.forEach((command) => command());
for (const command of commands) {
command();
}
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/services/Mapillary/Mapillary.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ export const nextMapillaryPageUrl = function (resultContext) {
console.log("Link Header:", linkHeader);

if (linkHeader) {
linkHeader.split(",").forEach((link) => {
for (const link of linkHeader.split(",")) {
const match = link.match(/<([^>]+)>\s*rel="([^"]+)"/);
if (match) {
const url = match[1];
const rel = match[2];
links[rel] = { url };
}
});
}
}

console.log("Parsed Links:", links);
Expand Down
Loading

0 comments on commit a4666f9

Please sign in to comment.