Skip to content

Commit 10407d6

Browse files
committed
Refactor: Normalize target names at parse time using target_id field
Eliminates 6 of 7 normalizeTargetName() calls by normalizing target names once at the data source (parseFilename/parseDevFilename functions) instead of repeatedly at consumption points. Changes: - Rename raw_target to target_id for semantic clarity - Add normalization in parseDevFilename() and parseFilename() - Remove redundant normalizeTargetName() calls throughout (6 eliminated) - Keep one necessary call for FC.CONFIG.target (external data source) Benefits: - Single normalization per filename parse (better performance) - Impossible to forget normalization (always available in result.target_id) - Clearer code intent and improved maintainability - Net reduction of 4 lines (17 insertions, 21 deletions)
1 parent eccb158 commit 10407d6

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

tabs/firmware_flasher.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ TABS.firmware_flasher.initialize = function (callback) {
8787
return null;
8888
}
8989

90+
var rawMatch = match[3]; // e.g., "TBS-LUCID-H7-WING" or "TBS_LUCID_H7_WING"
9091
return {
91-
raw_target: match[3],
92-
target: match[3].replace(/_/g, " ").replace(/-/g, " "), // "/g" to replace all
92+
target_id: normalizeTargetName(rawMatch), // Normalized: "TBS_LUCID_H7_WING"
93+
target: rawMatch.replace(/_/g, " ").replace(/-/g, " "), // Display: "TBS LUCID H7 WING"
9394
format: match[9],
9495
version: match[1]+match[2],
9596
major: match[1]
@@ -107,9 +108,10 @@ TABS.firmware_flasher.initialize = function (callback) {
107108

108109
//GUI.log("non dev: match[2]: " + match[2] + " match[3]: " + match[3]);
109110

111+
var rawMatch = match[2]; // e.g., "MATEKF405" or "MATEK-F405"
110112
return {
111-
raw_target: match[2],
112-
target: match[2].replace(/_/g, " ").replace(/-/g, " "), // "/g" to replace all
113+
target_id: normalizeTargetName(rawMatch), // Normalized: "MATEKF405"
114+
target: rawMatch.replace(/_/g, " ").replace(/-/g, " "), // Display: "MATEKF405"
113115
format: match[3],
114116
};
115117
}
@@ -165,9 +167,8 @@ TABS.firmware_flasher.initialize = function (callback) {
165167
if ((!showDevReleases && release.prerelease) || !result) {
166168
return;
167169
}
168-
var normalizedTarget = normalizeTargetName(result.raw_target);
169-
if($.inArray(normalizedTarget, unsortedTargets) == -1) {
170-
unsortedTargets.push(normalizedTarget);
170+
if($.inArray(result.target_id, unsortedTargets) == -1) {
171+
unsortedTargets.push(result.target_id);
171172
}
172173
});
173174
});
@@ -179,9 +180,8 @@ TABS.firmware_flasher.initialize = function (callback) {
179180
var result = parseDevFilename(asset.name);
180181

181182
if (result) {
182-
var normalizedTarget = normalizeTargetName(result.raw_target);
183-
if ($.inArray(normalizedTarget, unsortedTargets) == -1) {
184-
unsortedTargets.push(normalizedTarget);
183+
if ($.inArray(result.target_id, unsortedTargets) == -1) {
184+
unsortedTargets.push(result.target_id);
185185
}
186186
}
187187
});
@@ -225,14 +225,13 @@ TABS.firmware_flasher.initialize = function (callback) {
225225
"version" : release.tag_name,
226226
"url" : asset.browser_download_url,
227227
"file" : asset.name,
228-
"raw_target": result.raw_target,
228+
"target_id" : result.target_id,
229229
"target" : result.target,
230230
"date" : formattedDate,
231231
"notes" : release.body,
232232
"status" : release.prerelease ? "release-candidate" : "stable"
233233
};
234-
var normalizedTarget = normalizeTargetName(result.raw_target);
235-
releases[normalizedTarget].push(descriptor);
234+
releases[result.target_id].push(descriptor);
236235
});
237236
});
238237

@@ -280,14 +279,13 @@ TABS.firmware_flasher.initialize = function (callback) {
280279
"version" : release.tag_name,
281280
"url" : asset.browser_download_url,
282281
"file" : asset.name,
283-
"raw_target": result.raw_target,
282+
"target_id" : result.target_id,
284283
"target" : result.target,
285284
"date" : formattedDate,
286285
"notes" : release.body,
287286
"status" : release.prerelease ? "nightly" : "stable"
288287
};
289-
var normalizedTarget = normalizeTargetName(result.raw_target);
290-
releases[normalizedTarget].push(descriptor);
288+
releases[result.target_id].push(descriptor);
291289
});
292290
});
293291
}
@@ -300,10 +298,9 @@ TABS.firmware_flasher.initialize = function (callback) {
300298
descriptors.forEach(function(descriptor){
301299
if($.inArray(target, selectTargets) == -1) {
302300
selectTargets.push(target);
303-
var normalizedTarget = normalizeTargetName(descriptor.raw_target);
304301
var select_e =
305302
$("<option value='{0}'>{1}</option>".format(
306-
normalizedTarget,
303+
descriptor.target_id,
307304
descriptor.target
308305
)).data('summary', descriptor);
309306
boards_e.append(select_e);
@@ -353,9 +350,8 @@ TABS.firmware_flasher.initialize = function (callback) {
353350
versions_e.append($("<option value='0'>{0} {1}</option>".format(i18n.getMessage('firmwareFlasherOptionLabelSelectFirmwareVersionFor'), target)));
354351
}
355352

356-
var normalizedTarget = normalizeTargetName(target);
357-
if (typeof TABS.firmware_flasher.releases[normalizedTarget]?.forEach === 'function') {
358-
TABS.firmware_flasher.releases[normalizedTarget].forEach(function(descriptor) {
353+
if (typeof TABS.firmware_flasher.releases[target]?.forEach === 'function') {
354+
TABS.firmware_flasher.releases[target].forEach(function(descriptor) {
359355
var select_e =
360356
$("<option value='{0}'>{0} - {1} - {2} ({3})</option>".format(
361357
descriptor.version,

0 commit comments

Comments
 (0)