Skip to content

Commit

Permalink
Added testing for SI unit conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
Yves Ulrich Tittes committed Oct 21, 2024
1 parent b3a1ab2 commit 7b47fe5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
49 changes: 49 additions & 0 deletions src/common/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Currently only covers converToSI
import { convertToSI } from './utils';

Check failure on line 2 in src/common/utils.spec.ts

View workflow job for this annotation

GitHub Actions / eslint

Replace `'./utils'` with `"./utils"`

Check failure on line 2 in src/common/utils.spec.ts

View workflow job for this annotation

GitHub Actions / eslint

Strings must use doublequote
import { createUnit, Unit } from 'mathjs';

Check warning on line 3 in src/common/utils.spec.ts

View workflow job for this annotation

GitHub Actions / eslint

'createUnit' is defined but never used

Check warning on line 3 in src/common/utils.spec.ts

View workflow job for this annotation

GitHub Actions / eslint

'Unit' is defined but never used

Check failure on line 3 in src/common/utils.spec.ts

View workflow job for this annotation

GitHub Actions / eslint

Replace `'mathjs';⏎` with `"mathjs";`

Check failure on line 3 in src/common/utils.spec.ts

View workflow job for this annotation

GitHub Actions / eslint

Strings must use doublequote


describe('convertToSI', () => {

Check failure on line 6 in src/common/utils.spec.ts

View workflow job for this annotation

GitHub Actions / eslint

Replace `'convertToSI'` with `"convertToSI"`

Check failure on line 6 in src/common/utils.spec.ts

View workflow job for this annotation

GitHub Actions / eslint

Strings must use doublequote
it('should convert a known unit to SI successfully', () => {

Check failure on line 7 in src/common/utils.spec.ts

View workflow job for this annotation

GitHub Actions / eslint

Replace `'should·convert·a·known·unit·to·SI·successfully'` with `"should·convert·a·known·unit·to·SI·successfully"`

Check failure on line 7 in src/common/utils.spec.ts

View workflow job for this annotation

GitHub Actions / eslint

Strings must use doublequote
const result = convertToSI(1, 'cm');

Check failure on line 8 in src/common/utils.spec.ts

View workflow job for this annotation

GitHub Actions / eslint

Replace `'cm'` with `"cm"`

Check failure on line 8 in src/common/utils.spec.ts

View workflow job for this annotation

GitHub Actions / eslint

Strings must use doublequote
expect(result.valueSI).toBeCloseTo(0.01);
expect(result.unitSI).toEqual('m');
});

it('should convert angstrom to SI successfully', () => {
const result = convertToSI(1, 'Å');
expect(result.valueSI).toBeCloseTo(1e-10);
expect(result.unitSI).toEqual('m');
});

it('should handle different versions of Å in unicode', () => {
const inputUnit = '\u212B'; // Old unicode representation of "Å", is not boolean equal to the one we added.
const result = convertToSI(1, inputUnit);
expect(result.valueSI).toBeCloseTo(1e-10);
expect(result.unitSI).toEqual('m');
});

it('should return the input value and unit if conversion fails', () => {
const result = convertToSI(1, 'invalidUnit');
expect(result.valueSI).toEqual(1);
expect(result.unitSI).toEqual('invalidUnit');
});

it('should convert SI units correctly', () => {
const result = convertToSI(1000, 'g');
expect(result.valueSI).toBeCloseTo(1);
expect(result.unitSI).toEqual('kg');
});

it('should handle already normalized units', () => {
const result = convertToSI(1, 'm');
expect(result.valueSI).toEqual(1);
expect(result.unitSI).toEqual('m');
});

it('should handle negative values properly', () => {
const result = convertToSI(-5, 'cm');
expect(result.valueSI).toBeCloseTo(-0.05);
expect(result.unitSI).toEqual('m');
});
});
3 changes: 1 addition & 2 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ export const convertToSI = (
inputUnit: string,
): { valueSI: number; unitSI: string } => {
try {
// catch and normalize the different versions of Å in unicode
const normalizedUnit = inputUnit.normalize('NFC');
const normalizedUnit = inputUnit.normalize('NFC'); // catch and normalize the different versions of Å in unicode
// Workaround related to a bug reported at https://github.com/josdejong/mathjs/issues/3097 and https://github.com/josdejong/mathjs/issues/2499
const quantity = unit(inputValue, normalizedUnit)
.to(unit(normalizedUnit).toSI().toJSON().unit)
Expand Down

0 comments on commit 7b47fe5

Please sign in to comment.