You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

106 lines
2.3 KiB

import {Value} from "sass";
export enum Size {
Mobile = "mobile",
Tablet = "tablet",
Desktop = "desktop",
Any = "any",
}
export interface ValuesWithTime extends Values {
time: number
}
export interface Values {
time?: number
calories?: number
distance?: number
level?: number
}
export type ColorName = "gray" | "green" | "blue" | "red" | "yellow" | "indigo";
export function diffLinearValues<T extends Values>(a: Values, b: Values): T {
const c: Values = {};
if (a.time) c.time = a.time - (b.time || 0);
if (a.calories) c.calories = a.calories - (b.calories || 0);
if (a.distance) c.distance = a.distance - (b.distance || 0);
if (a.level) c.level = a.level;
return c as T;
}
export function formatValue(raw: number, type: keyof Values): string {
if (type === "time") {
const minutes = Math.floor(raw / 60).toString(10).padStart(2, "0");
const seconds = (raw % 60).toString(10).padStart(2, "0");
return `${minutes}'${seconds}"`;
}
if (type === "calories") {
return `${raw} kcal`;
}
if (type === "distance") {
if (raw >= 100) {
return `${(raw / 1000).toFixed(1)} km`
} else {
return `${raw} m`;
}
}
if (type === "level") {
return `Lvl. ${raw}`;
}
return "";
}
export function valuesToString(v: Values): string {
let str: string[] = [];
if (v.time) {
const min = Math.floor(v.time / 60);
const sec = v.time % 60;
if (min > 0) str.push(`${min}min`);
if (sec > 0) str.push(`${sec}sek`);
}
if (v.distance) {
str.push(`${v.distance / 1000}km`);
}
if (v.calories) {
str.push(`${v.calories}kcal`);
}
return str.join(" ");
}
const parseRegex = new RegExp("([0-9.]+)\\s?(min|sek|km|kcal|cal)", "g");
export function stringToValues(str: string): Values {
const v: Values = {};
const matches = str.matchAll(parseRegex);
for (const m of matches) {
if (m.length === 2) {
continue;
}
switch (m[2]) {
case "sek":
v.time = (v.time || 0) + parseInt(m[1]);
break;
case "min":
v.time = (v.time || 0) + (parseInt(m[1]) * 60);
break;
case "km":
v.distance = (v.distance || 0) + Math.round(parseFloat(m[1]) * 1000);
break;
case "kcal":
case "cal":
v.calories = (v.calories || 0) + parseInt(m[1]);
break;
}
}
return v;
}