Files
gpx.studio/website/src/lib/units/units.ts
T

166 lines
5.0 KiB
TypeScript
Raw Normal View History

2026-10-31 18:08:22 +01:00
import { settings } from '$lib/state/settings';
import { i18n } from '$lib/i18n/i18n.svelte';
2025-10-17 23:54:45 +02:00
import { get } from 'svelte/store';
2026-10-31 18:08:22 +01:00
import {
convertDistance,
convertDistanceToKilometers,
convertElevation,
convertTemperature,
convertVelocity,
secondsToHHMMSS,
} from '$lib/units/conversions';
export * from '$lib/units/conversions';
2024-04-24 16:12:50 +02:00
2024-05-04 15:10:30 +02:00
const { distanceUnits, velocityUnits, temperatureUnits } = settings;
2024-04-24 16:12:50 +02:00
// Get a string representation of the value with units
export function getDistanceWithUnits(value: number, convert: boolean = true) {
2025-02-02 11:17:22 +01:00
if (convert) {
return getConvertedDistance(value).toFixed(2) + ' ' + getDistanceUnits();
} else {
return value.toFixed(2) + ' ' + getDistanceUnits();
}
2024-04-24 16:12:50 +02:00
}
export function getVelocityWithUnits(value: number, convert: boolean = true) {
2025-10-17 23:54:45 +02:00
if (get(velocityUnits) === 'speed') {
2025-02-02 11:17:22 +01:00
if (convert) {
return getConvertedVelocity(value).toFixed(2) + ' ' + getVelocityUnits();
} else {
return value.toFixed(2) + ' ' + getVelocityUnits();
}
} else {
if (convert) {
return secondsToHHMMSS(getConvertedVelocity(value)) + ' ' + getVelocityUnits();
} else {
return secondsToHHMMSS(value) + ' ' + getVelocityUnits();
}
}
2024-04-24 16:12:50 +02:00
}
export function getElevationWithUnits(value: number, convert: boolean = true) {
2025-02-02 11:17:22 +01:00
if (convert) {
return getConvertedElevation(value).toFixed(0) + ' ' + getElevationUnits();
} else {
return value.toFixed(0) + ' ' + getElevationUnits();
}
2024-04-24 16:12:50 +02:00
}
export function getHeartRateWithUnits(value: number) {
2025-02-02 11:17:22 +01:00
return value.toFixed(0) + ' ' + getHeartRateUnits();
2024-04-24 16:12:50 +02:00
}
export function getCadenceWithUnits(value: number) {
2025-02-02 11:17:22 +01:00
return value.toFixed(0) + ' ' + getCadenceUnits();
2024-04-24 16:12:50 +02:00
}
export function getPowerWithUnits(value: number) {
2025-02-02 11:17:22 +01:00
return value.toFixed(0) + ' ' + getPowerUnits();
2024-04-24 16:12:50 +02:00
}
export function getTemperatureWithUnits(value: number, convert: boolean = true) {
2025-02-02 11:17:22 +01:00
if (convert) {
return getConvertedTemperature(value).toFixed(0) + ' ' + getTemperatureUnits();
} else {
return value.toFixed(0) + ' ' + getTemperatureUnits();
}
2024-04-24 16:12:50 +02:00
}
// Get the units
2025-10-17 23:54:45 +02:00
export function getDistanceUnits(targetDistanceUnits = get(distanceUnits)) {
2025-02-02 11:17:22 +01:00
switch (targetDistanceUnits) {
case 'metric':
2025-06-21 21:07:36 +02:00
return i18n._('units.kilometers');
2025-02-02 11:17:22 +01:00
case 'imperial':
2025-06-21 21:07:36 +02:00
return i18n._('units.miles');
2025-02-02 11:17:22 +01:00
case 'nautical':
2025-06-21 21:07:36 +02:00
return i18n._('units.nautical_miles');
2025-02-02 11:17:22 +01:00
}
2024-04-24 16:12:50 +02:00
}
2024-08-26 03:51:05 -07:00
export function getVelocityUnits(
2025-10-17 23:54:45 +02:00
targetVelocityUnits = get(velocityUnits),
targetDistanceUnits = get(distanceUnits)
2024-08-26 03:51:05 -07:00
) {
2025-02-02 11:17:22 +01:00
if (targetVelocityUnits === 'speed') {
switch (targetDistanceUnits) {
case 'metric':
2025-06-21 21:07:36 +02:00
return i18n._('units.kilometers_per_hour');
2025-02-02 11:17:22 +01:00
case 'imperial':
2025-06-21 21:07:36 +02:00
return i18n._('units.miles_per_hour');
2025-02-02 11:17:22 +01:00
case 'nautical':
2025-06-21 21:07:36 +02:00
return i18n._('units.knots');
2025-02-02 11:17:22 +01:00
}
} else {
switch (targetDistanceUnits) {
case 'metric':
2025-06-21 21:07:36 +02:00
return i18n._('units.minutes_per_kilometer');
2025-02-02 11:17:22 +01:00
case 'imperial':
2025-06-21 21:07:36 +02:00
return i18n._('units.minutes_per_mile');
2025-02-02 11:17:22 +01:00
case 'nautical':
2025-06-21 21:07:36 +02:00
return i18n._('units.minutes_per_nautical_mile');
2025-02-02 11:17:22 +01:00
}
}
2024-04-24 16:12:50 +02:00
}
2025-10-17 23:54:45 +02:00
export function getElevationUnits(targetDistanceUnits = get(distanceUnits)) {
2025-02-02 11:17:22 +01:00
switch (targetDistanceUnits) {
case 'metric':
2025-06-21 21:07:36 +02:00
return i18n._('units.meters');
2025-02-02 11:17:22 +01:00
case 'imperial':
2025-06-21 21:07:36 +02:00
return i18n._('units.feet');
2025-02-02 11:17:22 +01:00
case 'nautical':
// See https://github.com/gpxstudio/gpx.studio/pull/66#issuecomment-2306568997
2025-06-21 21:07:36 +02:00
return i18n._('units.meters');
2025-02-02 11:17:22 +01:00
}
2024-04-24 16:12:50 +02:00
}
export function getHeartRateUnits() {
2025-06-21 21:07:36 +02:00
return i18n._('units.heartrate');
2024-04-24 16:12:50 +02:00
}
export function getCadenceUnits() {
2025-06-21 21:07:36 +02:00
return i18n._('units.cadence');
2024-04-24 16:12:50 +02:00
}
export function getPowerUnits() {
2025-06-21 21:07:36 +02:00
return i18n._('units.power');
2024-04-24 16:12:50 +02:00
}
export function getTemperatureUnits() {
2025-10-17 23:54:45 +02:00
return get(temperatureUnits) === 'celsius'
2025-06-21 21:07:36 +02:00
? i18n._('units.celsius')
: i18n._('units.fahrenheit');
2024-04-24 16:12:50 +02:00
}
2026-10-31 18:08:22 +01:00
// Convert only the value, to the units of the settings by default
2025-10-17 23:54:45 +02:00
export function getConvertedDistance(value: number, targetDistanceUnits = get(distanceUnits)) {
2026-10-31 18:08:22 +01:00
return convertDistance(value, targetDistanceUnits);
2024-04-24 16:12:50 +02:00
}
2025-10-19 14:15:52 +02:00
export function getConvertedDistanceToKilometers(
value: number,
sourceDistanceUnits = get(distanceUnits)
) {
2026-10-31 18:08:22 +01:00
return convertDistanceToKilometers(value, sourceDistanceUnits);
2025-10-19 14:15:52 +02:00
}
2025-10-17 23:54:45 +02:00
export function getConvertedElevation(value: number, targetDistanceUnits = get(distanceUnits)) {
2026-10-31 18:08:22 +01:00
return convertElevation(value, targetDistanceUnits);
2024-04-24 16:12:50 +02:00
}
2024-08-26 03:51:05 -07:00
export function getConvertedVelocity(
2025-02-02 11:17:22 +01:00
value: number,
2025-10-17 23:54:45 +02:00
targetVelocityUnits = get(velocityUnits),
targetDistanceUnits = get(distanceUnits)
2024-08-26 03:51:05 -07:00
) {
2026-10-31 18:08:22 +01:00
return convertVelocity(value, targetVelocityUnits, targetDistanceUnits);
2024-04-24 16:12:50 +02:00
}
export function getConvertedTemperature(
value: number,
targetTemperatureUnits = get(temperatureUnits)
) {
2026-10-31 18:08:22 +01:00
return convertTemperature(value, targetTemperatureUnits);
2024-08-26 03:51:05 -07:00
}