mirror of
https://github.com/librespeed/speedtest.git
synced 2026-08-03 22:28:24 +00:00
fix: avoid mutating original server array and add null guard
Address code review findings: - Sort a shallow copy instead of mutating the caller's array - Add null guard on server.name to handle malformed entries - Use original SPEEDTEST_SERVERS index for classic UI option values
This commit is contained in:
parent
4ec3a755fa
commit
42d700cf08
2 changed files with 24 additions and 20 deletions
|
|
@ -231,18 +231,20 @@ function populateDropdown(servers) {
|
|||
}
|
||||
|
||||
// Sort servers by country, then by city within the same country
|
||||
servers.sort((a, b) => {
|
||||
const commaA = a.name.lastIndexOf(",");
|
||||
const commaB = b.name.lastIndexOf(",");
|
||||
const countryA = commaA >= 0 ? a.name.substring(commaA + 1).trim() : a.name;
|
||||
const countryB = commaB >= 0 ? b.name.substring(commaB + 1).trim() : b.name;
|
||||
const cityA = commaA >= 0 ? a.name.substring(0, commaA).trim() : "";
|
||||
const cityB = commaB >= 0 ? b.name.substring(0, commaB).trim() : "";
|
||||
const sorted = [...servers].sort((a, b) => {
|
||||
const nameA = a.name || "";
|
||||
const nameB = b.name || "";
|
||||
const commaA = nameA.lastIndexOf(",");
|
||||
const commaB = nameB.lastIndexOf(",");
|
||||
const countryA = commaA >= 0 ? nameA.substring(commaA + 1).trim() : nameA;
|
||||
const countryB = commaB >= 0 ? nameB.substring(commaB + 1).trim() : nameB;
|
||||
const cityA = commaA >= 0 ? nameA.substring(0, commaA).trim() : "";
|
||||
const cityB = commaB >= 0 ? nameB.substring(0, commaB).trim() : "";
|
||||
return countryA.localeCompare(countryB) || cityA.localeCompare(cityB);
|
||||
});
|
||||
|
||||
// Populate the list to choose from
|
||||
servers.forEach((server) => {
|
||||
sorted.forEach((server) => {
|
||||
const item = document.createElement("li");
|
||||
const link = document.createElement("a");
|
||||
link.href = "#";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue