Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/humanize-distance/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"gitHead": "644c72e0d08f8daf93b44eaf0deec88a1e16f414",
"scripts": {
"tsc": "tsc"
},
"peerDependencies": {
"react-intl": "^5.24.6"
}
}
70 changes: 56 additions & 14 deletions packages/humanize-distance/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,74 @@
import { IntlShape } from "react-intl";

function roundToOneDecimalPlace(number: number): number {
return Math.round(number * 10) / 10;
}

export function humanizeDistanceStringImperial(
meters: number,
abbreviate?: boolean
abbreviate?: boolean,
intl?: IntlShape
): string {
const feet = meters * 3.28084;
if (feet < 528)
return Math.round(feet) + (abbreviate === true ? " ft" : " feet");
return Math.round(feet / 528) / 10 + (abbreviate === true ? " mi" : " miles");

let unit;
let value;
let unitIfNoIntl;

if (feet < 528) {
unit = "foot";
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I right in thinking that formatMessage will re-write foot?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will also use the correct decimal separator.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh cool!

unitIfNoIntl = abbreviate ? "ft" : "feet";
value = Math.round(feet);
} else {
unit = "mile";
unitIfNoIntl = abbreviate ? "mi" : "miles";
value = roundToOneDecimalPlace(feet / 5280);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably clean this up a bit if we used these values as the default case for those lets

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, addressed in 9dcf6e7.

}

return intl
? intl.formatNumber(value, {
unit,
unitDisplay: abbreviate ? "short" : "long",
style: "unit"
})
: `${value} ${unitIfNoIntl}`;
}

export function humanizeDistanceStringMetric(meters: number): string {
export function humanizeDistanceStringMetric(
meters: number,
intl?: IntlShape
): string {
const km = meters / 1000;
let unit = "kilometer";
let shortUnit = "km";
let value;
if (km > 100) {
// 100 km => 999999999 km
return `${km.toFixed(0)} km`;
}
if (km > 1) {
value = Math.round(km);
} else if (km > 1) {
// 1.1 km => 99.9 km
return `${km.toFixed(1)} km`;
value = roundToOneDecimalPlace(km);
} else {
unit = "meter";
shortUnit = "m";
value = Math.round(meters);
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably clean this up a bit if we used these values as the default case for those lets

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 9dcf6e7.

// 1m => 999m
return `${meters.toFixed(0)} m`;

return intl
? intl.formatNumber(value, {
unit,
unitDisplay: "short",
style: "unit"
})
: `${value} ${shortUnit}`;
}

export function humanizeDistanceString(
meters: number,
outputMetricUnits = false
outputMetricUnits = false,
intl?: IntlShape
): string {
return outputMetricUnits
? humanizeDistanceStringMetric(meters)
: humanizeDistanceStringImperial(meters);
? humanizeDistanceStringMetric(meters, intl)
: humanizeDistanceStringImperial(meters, null, intl);
}