Skip to content
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

Merged
merged 4 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});
14 changes: 11 additions & 3 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Logger } from "@nestjs/common";
import { inspect } from "util";
import { DateTime } from "luxon";
import { format, unit } from "mathjs";
import { format, unit, Unit, createUnit } from "mathjs";
import { Expression, FilterQuery, Model, PipelineStage } from "mongoose";
import { DatasetType } from "src/datasets/dataset-type.enum";
import {
Expand All @@ -13,14 +13,22 @@ import {
} from "./interfaces/common.interface";
import { ScientificRelation } from "./scientific-relation.enum";

// add Å to mathjs accepted units as equivalent to angstrom
const isAlphaOriginal = Unit.isValidAlpha
Unit.isValidAlpha = function (c) {
return isAlphaOriginal(c) || c == 'Å'
}
createUnit('Å', '1 angstrom')
Copy link
Contributor

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.


export const convertToSI = (
inputValue: number,
inputUnit: string,
): { valueSI: number; unitSI: string } => {
try {
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, inputUnit)
.to(unit(inputUnit).toSI().toJSON().unit)
const quantity = unit(inputValue, normalizedUnit)
.to(unit(normalizedUnit).toSI().toJSON().unit)
.toJSON();
return { valueSI: Number(quantity.value), unitSI: quantity.unit };
} catch (error) {
Expand Down
Loading