-
Notifications
You must be signed in to change notification settings - Fork 32
Issue 924: add support for arrays in scientificMetadata #925
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
base: master
Are you sure you want to change the base?
Changes from all commits
71e7a84
30c3a11
07167cf
2073097
4f29306
a5e5380
548a4ac
acfc8f2
cf05544
46e00f3
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 |
---|---|---|
|
@@ -12,12 +12,36 @@ import { | |
} from "./interfaces/common.interface"; | ||
import { ScientificRelation } from "./scientific-relation.enum"; | ||
|
||
export const convertArrayToSI = ( | ||
inputValue: number[], | ||
inputUnit: string, | ||
): { valueSI: number[]; unitSI: string } => { | ||
try { | ||
const newUnit = unit(inputUnit).toSI().toJSON().unit; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can add a comment for this workaround, so that we can easier find to roll back these changes once upstream mathjs has fixed that behaviour ( and if possible reference a mathjs bug report as well) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #1005 bumped to version with fixed mathjs (see https://github.com/josdejong/mathjs/blob/develop/HISTORY.md#2024-01-12-1230) so we could drop the workaround now. |
||
if (inputValue && inputValue.length) { | ||
const value = Array.from( | ||
inputValue, | ||
(iValue) => unit(iValue, inputUnit).to(newUnit).toJSON().value, | ||
); | ||
return { valueSI: value, unitSI: newUnit }; | ||
} else { | ||
return { valueSI: inputValue, unitSI: newUnit }; | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
return { valueSI: inputValue, unitSI: inputUnit }; | ||
} | ||
}; | ||
|
||
export const convertToSI = ( | ||
inputValue: number, | ||
inputUnit: string, | ||
): { valueSI: number; unitSI: string } => { | ||
try { | ||
const quantity = unit(inputValue, inputUnit).toSI().toJSON(); | ||
// 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) | ||
.toJSON(); | ||
return { valueSI: Number(quantity.value), unitSI: quantity.unit }; | ||
} catch (error) { | ||
console.error(error); | ||
|
@@ -45,8 +69,19 @@ export const appendSIUnitToPhysicalQuantity = <T extends object>(object: T) => { | |
] as unknown as number; | ||
} | ||
}); | ||
|
||
if (value !== undefined && unit && unit.length > 0) { | ||
if ( | ||
value !== undefined && | ||
Array.isArray(value) && | ||
unit && | ||
unit.length > 0 | ||
) { | ||
const { valueSI, unitSI } = convertArrayToSI(value, unit); | ||
updatedObject[key as keyof T] = { | ||
...instance, | ||
valueSI, | ||
unitSI, | ||
}; | ||
} else if (value !== undefined && unit && unit.length > 0) { | ||
const { valueSI, unitSI } = convertToSI(value, unit); | ||
updatedObject[key as keyof T] = { | ||
...instance, | ||
|
Uh oh!
There was an error while loading. Please reload this page.