[openstreetmap/openstreetmap-website] Refactor distance unit handling in directions output (PR #7183)

Tom Hughes notifications at github.com
Sun Jun 28 08:54:31 UTC 2026


@tomhughes commented on this pull request.



> @@ -14,109 +14,85 @@ function RouteOutput(map) {
     weight: 12
   });
 
-  let distanceUnits = "km_m";
-  let downloadURL = null;
-
-  function translateDistanceUnits(m) {
-    if (distanceUnits === "mi_ft") {
-      return [m / 0.3048, "ft", m / 1609.344, "mi"];
-    } else if (distanceUnits === "mi_yd") {
-      return [m / 0.9144, "yd", m / 1609.344, "mi"];
-    } else {
-      return [m, "m", m / 1000, "km"];
+  class UnitFormatter {
+    constructor(units) {
+      [[this.minorName, this.minorScale], [this.majorName, this.majorScale]] = Object.entries(units);

Is this right? MDN says `Object.entries` iterates properties in order of creation which I assume means in the order their written for multiple properties in a constant but in that case the major units are first?

In any case I'm not sure that an interface that relies on the order of keys in a dictionary is very intuitive...

> @@ -14,109 +14,85 @@ function RouteOutput(map) {
     weight: 12
   });
 
-  let distanceUnits = "km_m";
-  let downloadURL = null;
-
-  function translateDistanceUnits(m) {
-    if (distanceUnits === "mi_ft") {
-      return [m / 0.3048, "ft", m / 1609.344, "mi"];
-    } else if (distanceUnits === "mi_yd") {
-      return [m / 0.9144, "yd", m / 1609.344, "mi"];
-    } else {
-      return [m, "m", m / 1000, "km"];
+  class UnitFormatter {
+    constructor(units) {
+      [[this.minorName, this.minorScale], [this.majorName, this.majorScale]] = Object.entries(units);
+    };

Is there something special about a constructor in javascript that means it needs a trailing semicolon but other methods don't?

>      }
-  }
+    totalDistance(m) {

Can we put blank lines between the methods please to make this a bit more readable?

-- 
Reply to this email directly or view it on GitHub:
https://github.com/openstreetmap/openstreetmap-website/pull/7183#pullrequestreview-4587283215
You are receiving this because you are subscribed to this thread.

Message ID: <openstreetmap/openstreetmap-website/pull/7183/review/4587283215 at github.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstreetmap.org/pipermail/rails-dev/attachments/20260628/d4e3b40d/attachment.htm>


More information about the rails-dev mailing list