-
Notifications
You must be signed in to change notification settings - Fork 39
Humanize distance intl #358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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"; | ||
| unitIfNoIntl = abbreviate ? "ft" : "feet"; | ||
| value = Math.round(feet); | ||
| } else { | ||
| unit = "mile"; | ||
| unitIfNoIntl = abbreviate ? "mi" : "miles"; | ||
| value = roundToOneDecimalPlace(feet / 5280); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
There was a problem hiding this comment.
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
formatMessagewill re-writefoot?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is correct.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh cool!