@sports-alliance/sports-lib
Version:
A Library to for importing / exporting and processing GPX, TCX, FIT and JSON files from services such as Strava, Movescount, Garmin, Polar etc
53 lines (52 loc) • 1.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatAscentDescentDisplayValue = formatAscentDescentDisplayValue;
function formatWithDotGrouping(value) {
const rounded = Math.round(value);
const sign = rounded < 0 ? '-' : '';
const absoluteValueText = `${Math.abs(rounded)}`;
return `${sign}${absoluteValueText.replace(/\B(?=(\d{3})+(?!\d))/g, '.')}`;
}
function formatCompactWithDot(value) {
const rounded = Math.round(value);
const absoluteValue = Math.abs(rounded);
const sign = rounded < 0 ? '-' : '';
const suffixes = [
{ threshold: 1000000000, suffix: 'b' },
{ threshold: 1000000, suffix: 'm' },
{ threshold: 1000, suffix: 'k' },
];
for (const { threshold, suffix } of suffixes) {
if (absoluteValue >= threshold) {
const compactValue = absoluteValue / threshold;
const compactText = compactValue % 1 === 0
? compactValue.toFixed(0)
: compactValue.toFixed(1).replace(/\.0$/, '');
return `${sign}${compactText}${suffix}`;
}
}
return `${rounded}`;
}
function formatAscentDescentDisplayValue(value, options) {
if (!Number.isFinite(value)) {
return '';
}
const rounded = Math.round(value);
if (options === null || options === void 0 ? void 0 : options.compact) {
if (options.locale) {
return new Intl.NumberFormat(options.locale, {
notation: 'compact',
compactDisplay: 'short',
maximumFractionDigits: 1,
}).format(rounded);
}
return formatCompactWithDot(rounded);
}
if (options === null || options === void 0 ? void 0 : options.locale) {
return new Intl.NumberFormat(options.locale, {
useGrouping: true,
maximumFractionDigits: 0,
}).format(rounded);
}
return formatWithDotGrouping(rounded);
}