From 169d2d37f0d53030c2724b9cd260d2888e94fc10 Mon Sep 17 00:00:00 2001 From: Roland Date: Sat, 4 Apr 2026 17:56:08 -0600 Subject: [PATCH] Adding more dashboard unit examples. Adding tests for unit conversion. --- app/src/DashboardPage.js | 35 +++- app/src/components/calculators/mathConfig.js | 95 +++++++++ .../components/calculators/mathConfig.test.js | 183 ++++++++++++++++++ 3 files changed, 308 insertions(+), 5 deletions(-) create mode 100644 app/src/components/calculators/mathConfig.js create mode 100644 app/src/components/calculators/mathConfig.test.js diff --git a/app/src/DashboardPage.js b/app/src/DashboardPage.js index b8acc7a5..abbbbf78 100644 --- a/app/src/DashboardPage.js +++ b/app/src/DashboardPage.js @@ -35,7 +35,7 @@ import {useSearchFilter, useSearchRecentFiles, useWROLMode} from "./hooks/custom import {FileCards, FileSearchFilterButton} from "./components/Files"; import {DateSelectorButton} from "./components/DatesSelector"; import {useCalculators} from "./components/Calculators"; -import {evaluate} from "mathjs"; +import {classifyAndEvaluate} from "./components/calculators/mathConfig"; function FlagsMessages() { const {settings, fetchSettings} = React.useContext(SettingsContext); @@ -100,15 +100,33 @@ function FlagsMessages() { function EvaluateForm() { const helpContents = -

Enter an equation above.

+

Enter an equation or a value with units.

-
Examples:
+
Math:
2 + 3
sqrt(15)
+
8 * pi
+ +
Unit Conversions:
15 km to miles
65 degF to degC
+
1 gallon to liters
+
5 psi to atm
+ +
Auto-Convert (enter value + unit):
+
5 miles
+
100 degF
+
1 gallon
+
500 Wh
+ +
Radiation:
+
0.5 Sv
+
1 Gy to raddose
+
100 remdose to Sv
+ +
Data:
7 * 750 Mb
-
8 * pi
+
2 Tb to Gb
const inputRef = React.useRef(); @@ -123,7 +141,14 @@ function EvaluateForm() { } setEvaluatedValue(''); // Clear input temporarily so the user can tell something happened. try { - setEvaluatedValue(evaluate(inputValue).toString()); + const {primary, conversions} = classifyAndEvaluate(inputValue); + setEvaluatedValue(
+ {primary} + {conversions.length > 0 &&
+ Also: + {conversions.map((c, i) =>
{c}
)} +
} +
); } catch (error) { console.error(error); setEvaluatedValue(`Unable to evaluate: ${error.message}`); diff --git a/app/src/components/calculators/mathConfig.js b/app/src/components/calculators/mathConfig.js new file mode 100644 index 00000000..ba727f9a --- /dev/null +++ b/app/src/components/calculators/mathConfig.js @@ -0,0 +1,95 @@ +import {all, create} from "mathjs"; + +// Create a scoped math instance so custom units don't affect the global mathjs (used by RatioCalculator). +const math = create(all); + +// Register custom radiation units. +// "rad" is already a built-in radian unit, so we use distinct alphanumeric names. +math.createUnit('gray', {aliases: ['Gy'], prefixes: 'short'}); +math.createUnit('raddose', '0.01 gray'); +math.createUnit('sievert', {aliases: ['Sv'], prefixes: 'short'}); +math.createUnit('remdose', '0.01 sievert'); +math.createUnit('roentgen', '0.00877 gray'); +math.createUnit('becquerel', {definition: '1/s', aliases: ['Bq'], prefixes: 'short'}); +math.createUnit('curie', '3.7e10 becquerel', {aliases: ['Ci']}); + +// Map internal unit names to user-friendly display names. +const UNIT_DISPLAY_NAMES = { + 'raddose': 'rad', + 'remdose': 'rem', +}; + +// Conversion groups: when a user enters a bare unit value, show these related conversions. +const CONVERSION_GROUPS = [ + {units: ['meter', 'km', 'mile', 'foot', 'inch', 'yard']}, + {units: ['kg', 'lbm', 'oz', 'g', 'grain', 'tonne']}, + {units: ['liter', 'gallon', 'quart', 'cup', 'fluidounce', 'ml', 'tablespoon', 'teaspoon']}, + {units: ['degF', 'degC', 'K']}, + {units: ['m/s', 'km/h', 'mi/h']}, + {units: ['Pa', 'psi', 'atm', 'bar', 'mmHg']}, + {units: ['J', 'kJ', 'Wh', 'kWh', 'BTU']}, + {units: ['W', 'kW', 'hp']}, + {units: ['b', 'Kb', 'Mb', 'Gb', 'Tb']}, + {units: ['gray', 'raddose', 'roentgen']}, + {units: ['sievert', 'remdose']}, + {units: ['becquerel', 'curie']}, + {units: ['N', 'lbf', 'kip']}, +]; + +function formatUnit(unitValue) { + let str = math.format(unitValue, {notation: 'fixed', precision: 4}); + // Strip trailing zeros after decimal point. + str = str.replace(/(\.\d*?)0+(\s)/, '$1$2').replace(/\.(\s)/, '$1'); + // Replace internal unit names with friendly display names. + for (const [internal, display] of Object.entries(UNIT_DISPLAY_NAMES)) { + str = str.replace(new RegExp(`\\b${internal}\\b`, 'g'), display); + } + return str; +} + +function findConversionGroup(result) { + for (const group of CONVERSION_GROUPS) { + for (const u of group.units) { + try { + result.to(u); + return group; + } catch { + // Not compatible with this unit. + } + } + } + return null; +} + +export function classifyAndEvaluate(inputStr) { + const result = math.evaluate(inputStr); + + if (math.typeOf(result) !== 'Unit') { + return {primary: result.toString(), conversions: []}; + } + + // If user explicitly used "to" or "in" for conversion, just return the single result. + if (/\bto\b/i.test(inputStr) || /\bin\b/i.test(inputStr)) { + return {primary: formatUnit(result), conversions: []}; + } + + const primaryStr = formatUnit(result); + const conversions = []; + const group = findConversionGroup(result); + + if (group) { + for (const targetUnit of group.units) { + try { + const converted = result.to(targetUnit); + const formatted = formatUnit(converted); + if (formatted !== primaryStr) { + conversions.push(formatted); + } + } catch { + // Skip incompatible (e.g. compound unit mismatch). + } + } + } + + return {primary: primaryStr, conversions}; +} diff --git a/app/src/components/calculators/mathConfig.test.js b/app/src/components/calculators/mathConfig.test.js new file mode 100644 index 00000000..de6027d1 --- /dev/null +++ b/app/src/components/calculators/mathConfig.test.js @@ -0,0 +1,183 @@ +import {classifyAndEvaluate} from "./mathConfig"; + +describe('classifyAndEvaluate', () => { + + // Tests for every example shown in the EvaluateForm help text. + describe('help examples', () => { + + // Math examples + test('2 + 3', () => { + const result = classifyAndEvaluate('2 + 3'); + expect(result.primary).toBe('5'); + expect(result.conversions).toEqual([]); + }); + + test('sqrt(15)', () => { + const result = classifyAndEvaluate('sqrt(15)'); + expect(Number(result.primary)).toBeCloseTo(3.8730, 3); + expect(result.conversions).toEqual([]); + }); + + test('8 * pi', () => { + const result = classifyAndEvaluate('8 * pi'); + expect(Number(result.primary)).toBeCloseTo(25.1327, 3); + expect(result.conversions).toEqual([]); + }); + + // Unit conversion examples + test('15 km to miles', () => { + const result = classifyAndEvaluate('15 km to miles'); + expect(result.conversions).toEqual([]); + expect(result.primary).toMatch(/9\.32\d*/); + }); + + test('65 degF to degC', () => { + const result = classifyAndEvaluate('65 degF to degC'); + expect(result.conversions).toEqual([]); + expect(result.primary).toMatch(/18\.33\d*/); + }); + + test('1 gallon to liters', () => { + const result = classifyAndEvaluate('1 gallon to liters'); + expect(result.conversions).toEqual([]); + expect(result.primary).toMatch(/3\.78\d*/); + }); + + test('5 psi to atm', () => { + const result = classifyAndEvaluate('5 psi to atm'); + expect(result.conversions).toEqual([]); + expect(result.primary).toMatch(/0\.34\d*/); + }); + + // Auto-convert examples + test('5 miles auto-converts to other length units', () => { + const result = classifyAndEvaluate('5 miles'); + expect(result.conversions.length).toBeGreaterThan(0); + expect(result.conversions.some(c => c.includes('km'))).toBe(true); + expect(result.conversions.some(c => c.includes('meter'))).toBe(true); + expect(result.conversions.some(c => c.includes('foot'))).toBe(true); + }); + + test('100 degF auto-converts to degC and K', () => { + const result = classifyAndEvaluate('100 degF'); + expect(result.conversions.some(c => c.includes('degC'))).toBe(true); + expect(result.conversions.some(c => c.includes('K'))).toBe(true); + }); + + test('1 gallon auto-converts to other volume units', () => { + const result = classifyAndEvaluate('1 gallon'); + expect(result.conversions.some(c => c.includes('liter'))).toBe(true); + expect(result.conversions.some(c => c.includes('quart'))).toBe(true); + expect(result.conversions.some(c => c.includes('cup'))).toBe(true); + }); + + test('500 Wh auto-converts to other energy units', () => { + const result = classifyAndEvaluate('500 Wh'); + expect(result.conversions.some(c => c.includes('J'))).toBe(true); + expect(result.conversions.some(c => c.includes('kWh'))).toBe(true); + expect(result.conversions.some(c => c.includes('BTU'))).toBe(true); + }); + + // Radiation examples + test('0.5 Sv auto-converts to rem', () => { + const result = classifyAndEvaluate('0.5 Sv'); + expect(result.conversions.some(c => c.includes('rem'))).toBe(true); + }); + + test('1 Gy to raddose', () => { + const result = classifyAndEvaluate('1 Gy to raddose'); + expect(result.conversions).toEqual([]); + expect(result.primary).toMatch(/100/); + // Display name mapping: raddose shows as "rad". + expect(result.primary).toContain('rad'); + expect(result.primary).not.toContain('raddose'); + }); + + test('100 remdose to Sv', () => { + const result = classifyAndEvaluate('100 remdose to Sv'); + expect(result.conversions).toEqual([]); + expect(result.primary).toMatch(/1\s+Sv/); + }); + + // Data examples + test('7 * 750 Mb', () => { + const result = classifyAndEvaluate('7 * 750 Mb'); + // math.js simplifies 5250 Mb to 5.25 Gb. + expect(result.primary).toMatch(/5\.25\s+Gb/); + }); + + test('2 Tb to Gb', () => { + const result = classifyAndEvaluate('2 Tb to Gb'); + expect(result.conversions).toEqual([]); + expect(result.primary).toMatch(/2000/); + }); + }); + + describe('auto-conversion details', () => { + test('does not include the input unit in conversions', () => { + const result = classifyAndEvaluate('5 miles'); + expect(result.conversions).not.toContain(result.primary); + }); + + test('mass: 10 kg shows lbm, oz, g', () => { + const result = classifyAndEvaluate('10 kg'); + expect(result.conversions.some(c => c.includes('lbm'))).toBe(true); + expect(result.conversions.some(c => c.includes('g'))).toBe(true); + }); + + test('pressure: 1 atm shows psi, Pa, bar, mmHg', () => { + const result = classifyAndEvaluate('1 atm'); + expect(result.conversions.some(c => c.includes('psi'))).toBe(true); + expect(result.conversions.some(c => c.includes('Pa'))).toBe(true); + expect(result.conversions.some(c => c.includes('bar'))).toBe(true); + }); + + test('data: 1 Gb shows Mb', () => { + const result = classifyAndEvaluate('1 Gb'); + expect(result.conversions.some(c => c.includes('Mb'))).toBe(true); + }); + }); + + describe('radiation units', () => { + test('gray auto-converts to raddose and roentgen', () => { + const result = classifyAndEvaluate('1 gray'); + expect(result.conversions.some(c => c.includes('rad'))).toBe(true); + expect(result.conversions.some(c => c.includes('roentgen'))).toBe(true); + }); + + test('becquerel and curie conversion', () => { + const result = classifyAndEvaluate('1 curie'); + expect(result.conversions.some(c => c.includes('becquerel'))).toBe(true); + }); + + test('SI prefixes work with sievert (1 Sv = 1000 mSv)', () => { + const result = classifyAndEvaluate('1 sievert to mSv'); + expect(result.primary).toMatch(/1000/); + }); + + test('SI prefixes work with gray (1 mGy = 0.001 Gy)', () => { + const result = classifyAndEvaluate('1 mGy to gray'); + expect(result.primary).toMatch(/0\.001/); + }); + }); + + describe('display name formatting', () => { + test('raddose displays as rad in output', () => { + const result = classifyAndEvaluate('1 Gy to raddose'); + expect(result.primary).toContain('rad'); + expect(result.primary).not.toContain('raddose'); + }); + + test('remdose displays as rem in output', () => { + const result = classifyAndEvaluate('1 Sv to remdose'); + expect(result.primary).toContain('rem'); + expect(result.primary).not.toContain('remdose'); + }); + }); + + describe('error handling', () => { + test('invalid expression throws', () => { + expect(() => classifyAndEvaluate('foo bar baz')).toThrow(); + }); + }); +});