@opentripplanner/humanize-distance
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -1,26 +0,48 @@ | ||
export function humanizeDistanceStringImperial(meters, abbreviate) { | ||
function roundToOneDecimalPlace(number) { | ||
return Math.round(number * 10) / 10; | ||
} | ||
export function humanizeDistanceStringImperial(meters, abbreviate, intl) { | ||
var 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"); | ||
var unit = "mile"; | ||
var unitIfNoIntl = abbreviate ? "mi" : "miles"; | ||
var value = roundToOneDecimalPlace(feet / 5280); | ||
if (feet < 528) { | ||
unit = "foot"; | ||
unitIfNoIntl = abbreviate ? "ft" : "feet"; | ||
value = Math.round(feet); | ||
} | ||
return intl ? intl.formatNumber(value, { | ||
style: "unit", | ||
unit: unit, | ||
unitDisplay: abbreviate ? "short" : "long" | ||
}) : "".concat(value, " ").concat(unitIfNoIntl); | ||
} | ||
export function humanizeDistanceStringMetric(meters) { | ||
export function humanizeDistanceStringMetric(meters, intl) { | ||
var km = meters / 1000; | ||
var unit = "meter"; | ||
var shortUnit = "m"; | ||
var value = Math.round(meters); | ||
if (km > 100) { | ||
// 100 km => 999999999 km | ||
return "".concat(km.toFixed(0), " km"); | ||
if (km > 1) { | ||
unit = "kilometer"; | ||
shortUnit = "km"; | ||
value = km > 100 ? // 100 km and over | ||
Math.round(km) : // 1.1 km => 99.9 km | ||
roundToOneDecimalPlace(km); | ||
} | ||
if (km > 1) { | ||
// 1.1 km => 99.9 km | ||
return "".concat(km.toFixed(1), " km"); | ||
} // 1m => 999m | ||
return "".concat(meters.toFixed(0), " m"); | ||
return intl ? intl.formatNumber(value, { | ||
style: "unit", | ||
unit: unit, | ||
unitDisplay: "short" | ||
}) : "".concat(value, " ").concat(shortUnit); | ||
} | ||
export function humanizeDistanceString(meters) { | ||
var outputMetricUnits = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
return outputMetricUnits ? humanizeDistanceStringMetric(meters) : humanizeDistanceStringImperial(meters); | ||
var intl = arguments.length > 2 ? arguments[2] : undefined; | ||
return outputMetricUnits ? humanizeDistanceStringMetric(meters, intl) : humanizeDistanceStringImperial(meters, null, intl); | ||
} | ||
//# sourceMappingURL=index.js.map |
@@ -1,3 +0,5 @@ | ||
export declare function humanizeDistanceStringImperial(meters: number, abbreviate?: boolean): string; | ||
export declare function humanizeDistanceStringMetric(meters: number): string; | ||
export declare function humanizeDistanceString(meters: number, outputMetricUnits?: boolean): string; | ||
import { IntlShape } from "react-intl"; | ||
export declare function humanizeDistanceStringImperial(meters: number, abbreviate?: boolean, intl?: IntlShape): string; | ||
export declare function humanizeDistanceStringMetric(meters: number, intl?: IntlShape): string; | ||
export declare function humanizeDistanceString(meters: number, outputMetricUnits?: boolean, intl?: IntlShape): string; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.humanizeDistanceString = exports.humanizeDistanceStringMetric = exports.humanizeDistanceStringImperial = void 0; | ||
function roundToOneDecimalPlace(number) { | ||
return Math.round(number * 10) / 10; | ||
} | ||
function humanizeDistanceStringImperial(meters, abbreviate, intl) { | ||
var feet = meters * 3.28084; | ||
var unit = "mile"; | ||
var unitIfNoIntl = abbreviate ? "mi" : "miles"; | ||
var value = roundToOneDecimalPlace(feet / 5280); | ||
if (feet < 528) { | ||
unit = "foot"; | ||
unitIfNoIntl = abbreviate ? "ft" : "feet"; | ||
value = Math.round(feet); | ||
} | ||
return intl | ||
? intl.formatNumber(value, { | ||
style: "unit", | ||
unit: unit, | ||
unitDisplay: abbreviate ? "short" : "long" | ||
}) | ||
: value + " " + unitIfNoIntl; | ||
} | ||
exports.humanizeDistanceStringImperial = humanizeDistanceStringImperial; | ||
function humanizeDistanceStringMetric(meters, intl) { | ||
var km = meters / 1000; | ||
var unit = "meter"; | ||
var shortUnit = "m"; | ||
var value = Math.round(meters); | ||
if (km > 1) { | ||
unit = "kilometer"; | ||
shortUnit = "km"; | ||
value = | ||
km > 100 | ||
? // 100 km and over | ||
Math.round(km) | ||
: // 1.1 km => 99.9 km | ||
roundToOneDecimalPlace(km); | ||
} | ||
return intl | ||
? intl.formatNumber(value, { | ||
style: "unit", | ||
unit: unit, | ||
unitDisplay: "short" | ||
}) | ||
: value + " " + shortUnit; | ||
} | ||
exports.humanizeDistanceStringMetric = humanizeDistanceStringMetric; | ||
function humanizeDistanceString(meters, outputMetricUnits, intl) { | ||
if (outputMetricUnits === void 0) { outputMetricUnits = false; } | ||
return outputMetricUnits | ||
? humanizeDistanceStringMetric(meters, intl) | ||
: humanizeDistanceStringImperial(meters, null, intl); | ||
} | ||
exports.humanizeDistanceString = humanizeDistanceString; | ||
function humanizeDistanceStringImperial(meters, abbreviate) { | ||
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"); | ||
} | ||
function humanizeDistanceStringMetric(meters) { | ||
const km = meters / 1000; | ||
if (km > 100) { | ||
// 100 km => 999999999 km | ||
return `${km.toFixed(0)} km`; | ||
} | ||
if (km > 1) { | ||
// 1.1 km => 99.9 km | ||
return `${km.toFixed(1)} km`; | ||
} // 1m => 999m | ||
return `${meters.toFixed(0)} m`; | ||
} | ||
function humanizeDistanceString(meters, outputMetricUnits = false) { | ||
return outputMetricUnits ? humanizeDistanceStringMetric(meters) : humanizeDistanceStringImperial(meters); | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@opentripplanner/humanize-distance", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Pretty print a string for a certain distance", | ||
@@ -15,3 +15,6 @@ "main": "lib/index.js", | ||
"tsc": "tsc" | ||
}, | ||
"peerDependencies": { | ||
"react-intl": "^5.24.6" | ||
} | ||
} |
@@ -0,23 +1,60 @@ | ||
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 = "mile"; | ||
let unitIfNoIntl = abbreviate ? "mi" : "miles"; | ||
let value = roundToOneDecimalPlace(feet / 5280); | ||
if (feet < 528) { | ||
unit = "foot"; | ||
unitIfNoIntl = abbreviate ? "ft" : "feet"; | ||
value = Math.round(feet); | ||
} | ||
return intl | ||
? intl.formatNumber(value, { | ||
style: "unit", | ||
unit, | ||
unitDisplay: abbreviate ? "short" : "long" | ||
}) | ||
: `${value} ${unitIfNoIntl}`; | ||
} | ||
export function humanizeDistanceStringMetric(meters: number): string { | ||
export function humanizeDistanceStringMetric( | ||
meters: number, | ||
intl?: IntlShape | ||
): string { | ||
const km = meters / 1000; | ||
if (km > 100) { | ||
// 100 km => 999999999 km | ||
return `${km.toFixed(0)} km`; | ||
} | ||
let unit = "meter"; | ||
let shortUnit = "m"; | ||
let value = Math.round(meters); | ||
if (km > 1) { | ||
// 1.1 km => 99.9 km | ||
return `${km.toFixed(1)} km`; | ||
unit = "kilometer"; | ||
shortUnit = "km"; | ||
value = | ||
km > 100 | ||
? // 100 km and over | ||
Math.round(km) | ||
: // 1.1 km => 99.9 km | ||
roundToOneDecimalPlace(km); | ||
} | ||
// 1m => 999m | ||
return `${meters.toFixed(0)} m`; | ||
return intl | ||
? intl.formatNumber(value, { | ||
style: "unit", | ||
unit, | ||
unitDisplay: "short" | ||
}) | ||
: `${value} ${shortUnit}`; | ||
} | ||
@@ -27,7 +64,8 @@ | ||
meters: number, | ||
outputMetricUnits = false | ||
outputMetricUnits = false, | ||
intl?: IntlShape | ||
): string { | ||
return outputMetricUnits | ||
? humanizeDistanceStringMetric(meters) | ||
: humanizeDistanceStringImperial(meters); | ||
? humanizeDistanceStringMetric(meters, intl) | ||
: humanizeDistanceStringImperial(meters, null, intl); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
12676
12
196
1