feat: added support for more units

This commit is contained in:
Leyla Becker 2026-02-22 19:24:02 -06:00
parent df26f0243b
commit 750bfb912f
3 changed files with 102 additions and 6 deletions

View file

@ -43,7 +43,10 @@ function unitLabel(unit, plural) {
'day': plural ? 'days' : 'day',
'week': plural ? 'weeks' : 'week',
'month': plural ? 'months' : 'month',
'year': plural ? 'years' : 'year',
'second': plural ? 'seconds' : 'sec',
'psi': 'PSI', 'kPa': 'kPa', 'bar': 'bar',
'pH': 'pH',
'parts by volume': 'parts by volume',
'parts by weight': 'parts by weight',
};
@ -108,6 +111,10 @@ function toMetricValue(value, unit) {
case 'inch': return { value: value * 2.54, unit: 'cm' };
case 'cm': return { value, unit: 'cm' };
case 'mm': return { value, unit: 'mm' };
// Pressure
case 'psi': return { value: value * 6.89476, unit: 'kPa' };
case 'kPa': return { value, unit: 'kPa' };
case 'bar': return { value: value * 100, unit: 'kPa' };
default: return { value, unit };
}
}
@ -137,6 +144,10 @@ function toImperialValue(value, unit) {
case 'cm': return { value: value / 2.54, unit: 'inch' };
case 'mm': return { value: value / 25.4, unit: 'inch' };
case 'inch': return { value, unit: 'inch' };
// Pressure
case 'kPa': return { value: value / 6.89476, unit: 'psi' };
case 'bar': return { value: value * 14.5038, unit: 'psi' };
case 'psi': return { value, unit: 'psi' };
default: return { value, unit };
}
}
@ -317,6 +328,7 @@ function collapsedTime(value, unit) {
if (unit === 'day') return formatValueUnit(value, 'day');
if (unit === 'week') return formatValueUnit(value, 'week');
if (unit === 'month') return formatValueUnit(value, 'month');
if (unit === 'year') return formatValueUnit(value, 'year');
if (unit === 'second') return formatValueUnit(value, 'second');
const mins = toMinutes(value, unit);
if (mins === null) return formatValueUnit(value, unit);
@ -327,6 +339,7 @@ function expandedTime(value, unit) {
if (unit === 'day') return formatValueUnit(value, 'day');
if (unit === 'week') return formatValueUnit(value, 'week');
if (unit === 'month') return formatValueUnit(value, 'month');
if (unit === 'year') return formatValueUnit(value, 'year');
if (unit === 'second') return formatValueUnit(value, 'second');
const mins = toMinutes(value, unit);
if (mins === null) return formatValueUnit(value, unit);
@ -350,7 +363,7 @@ function generateTexts(measurement) {
const { type, amount, unit, approximate } = measurement;
// Non-convertible units
if (unit === 'parts by volume' || unit === 'parts by weight') {
if (unit === 'parts by volume' || unit === 'parts by weight' || unit === 'pH') {
const text = formatMeasurementText(amount, unit, approximate);
return { defaultText: text, altText: text };
}