-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add Å to SI accepted units #1442
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 GitHub Actions / eslint
|
||
import { createUnit, Unit } from 'mathjs'; | ||
Check warning on line 3 in src/common/utils.spec.ts GitHub Actions / eslint
Check warning on line 3 in src/common/utils.spec.ts GitHub Actions / eslint
Check failure on line 3 in src/common/utils.spec.ts GitHub Actions / eslint
|
||
|
||
|
||
describe('convertToSI', () => { | ||
Check failure on line 6 in src/common/utils.spec.ts GitHub Actions / eslint
|
||
it('should convert a known unit to SI successfully', () => { | ||
Check failure on line 7 in src/common/utils.spec.ts GitHub Actions / eslint
|
||
const result = convertToSI(1, 'cm'); | ||
Check failure on line 8 in src/common/utils.spec.ts GitHub Actions / eslint
|
||
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'); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works since mathjs is only imported in a single file. However if we need to handle units from multiple files then we should refactor this so that the module setup code only gets called once.