@hlfan commented on this pull request.
> + const miSize = 1609.344;
+ const ftSize = 0.3048;
+
function formatTotalDistance(m) {
- if (m < 1000) {
- return OSM.i18n.t("javascripts.directions.distance_m", { distance:
Math.round(m) });
- } else if (m < 10000) {
- return OSM.i18n.t("javascripts.directions.distance_km", { distance: (m /
1000.0).toFixed(1) });
+ if (distanceUnits === "km") {
+ return format(m, "m", m / 1000, "km");
} else {
- return OSM.i18n.t("javascripts.directions.distance_km", { distance:
Math.round(m / 1000) });
+ return format(m / ftSize, "ft", m / miSize, "mi");
+ }
+
+ function format(minorValue, minorName, majorValue, majorName) {
+ const scope = "javascripts.directions.distance_in_units";
+
+ if (minorValue < 1000) {
+ return OSM.i18n.t(minorName, { scope, distance: Math.round(minorValue)
});
+ } else if (majorValue < 10) {
+ return OSM.i18n.t(majorName, { scope, distance: majorValue.toFixed(1)
});
+ } else {
+ return OSM.i18n.t(majorName, { scope, distance: Math.round(majorValue)
});
+ }
}
}
function formatStepDistance(m) {
if (m < 5) {
return "";
- } else if (m < 200) {
- return OSM.i18n.t("javascripts.directions.distance_m", { distance:
String(Math.round(m / 10) * 10) });
- } else if (m < 1500) {
- return OSM.i18n.t("javascripts.directions.distance_m", { distance:
String(Math.round(m / 100) * 100) });
- } else if (m < 5000) {
- return OSM.i18n.t("javascripts.directions.distance_km", { distance:
String(Math.round(m / 100) / 10) });
+ } else if (distanceUnits === "km") {
+ return format(m, "m", m / 1000, "km");
} else {
- return OSM.i18n.t("javascripts.directions.distance_km", { distance:
String(Math.round(m / 1000)) });
+ return format(m / ftSize, "ft", m / miSize, "mi");
+ }
+
+ function format(minorValue, minorName, majorValue, majorName) {
+ const scope = "javascripts.directions.distance_in_units";
+
+ if (minorValue < 200) {
+ return OSM.i18n.t(minorName, { scope, distance: Math.round(minorValue
/ 10) * 10 });
+ } else if (minorValue < 1500) {
+ return OSM.i18n.t(minorName, { scope, distance: Math.round(minorValue
/ 100) * 100 });
+ } else if (majorValue < 5) {
+ return OSM.i18n.t(majorName, { scope, distance: majorValue.toFixed(1)
});
+ } else {
+ return OSM.i18n.t(majorName, { scope, distance: Math.round(majorValue)
});
+ }
}
}
function formatHeight(m) {
- return OSM.i18n.t("javascripts.directions.distance_m", { distance:
Math.round(m) });
+ const scope = "javascripts.directions.distance_in_units";
+
+ if (distanceUnits === "km") {
+ return OSM.i18n.t("m", { scope, distance: Math.round(m) });
+ } else {
+ const ft = m / ftSize;
+ return OSM.i18n.t("ft", { scope, distance: Math.round(ft) });
+ }
}
I think separating the unit-agnostic logic from the unit-specific stuff could
make the functions more straightforward:
```js
const common = {
scope: "javascripts.directions.distance_in_units",
_formatTotal(minorValue, minorName, majorValue, majorName) {
const scope = this.scope;
if (minorValue < 1000) return OSM.i18n.t(minorName, { scope, distance:
Math.round(minorValue) });
if (majorValue < 10) return OSM.i18n.t(majorName, { scope, distance:
majorValue.toFixed(1) });
return OSM.i18n.t(majorName, { scope, distance: Math.round(majorValue) });
},
_formatStep(minorValue, minorName, majorValue, majorName) {
const scope = this.scope;
if (minorValue < 200) return OSM.i18n.t(minorName, { scope, distance:
Math.round(minorValue / 10) * 10 });
if (minorValue < 1500) return OSM.i18n.t(minorName, { scope, distance:
Math.round(minorValue / 100) * 100 });
if (majorValue < 5) return OSM.i18n.t(majorName, { scope, distance:
majorValue.toFixed(1) });
return OSM.i18n.t(majorName, { scope, distance: Math.round(majorValue) });
},
_formatHeight(value, name) {
return OSM.i18n.t(name, { scope: this.scope, distance: Math.round(value) });
},
formatTime(s) {
const m = Math.round(s / 60);
const h = Math.floor(m / 60);
return h + ":" + `${m - (h * 60)}`.padStart(2, 0);
}
};
const formatters = {
km: {
...common,
formatTotal(m) { return this._formatTotal(m, "m", m / 1000, "km"); },
formatStep(m) { return this._formatStep(m, "m", m / 1000, "km"); },
formatHeight(m) { return this._formatHeight(m, "m"); }
},
mi: {
...common,
ftSize: 0.3048,
miSize: 1609.344,
formatTotal(m) { return this._formatTotal(m / this.ftSize, "ft", m /
this.miSize, "mi"); },
formatStep(m) { return this._formatStep(m / this.ftSize, "ft", m /
this.miSize, "mi"); },
formatHeight(m) { return this._formatHeight(m / this.ftSize, "ft"); }
}
};
```
Then, set `currentFormatter = formatters.mi` in the
`#directions_distance_units_mi.change` handler and use the `formatStep`,
`formatTotal`, etc.. directly from `currentFormatter`.
--
Reply to this email directly or view it on GitHub:
https://github.com/openstreetmap/openstreetmap-website/pull/5915#pullrequestreview-2759955517
You are receiving this because you are subscribed to this thread.
Message ID:
<openstreetmap/openstreetmap-website/pull/5915/review/2759955...@github.com>
_______________________________________________
rails-dev mailing list
rails-dev@openstreetmap.org
https://lists.openstreetmap.org/listinfo/rails-dev