mathjs
mathjs copied to clipboard
Cannot use defined Unit using ° (U+00B0)
Documentation states that the only barrier to adding extended character support is overwriting the Unit.isValidAlpha method (https://mathjs.org/docs/datatypes/units.html#support-of-custom-characters-in-unit-names)
The following code produces an Error
const isAlphaOriginal = math.Unit.isValidAlpha
const isDegOrPercent = function (c) {
return c === "°" || c === "%"
}
math.Unit.isValidAlpha = function (c) {
return isAlphaOriginal(c) || isDegOrPercent(c)
}
math.createUnit('°', '1 deg')
math.createUnit('%', '0.01')
math.evaluate('12.7 ° to deg')
Error:
SyntaxError: Syntax error in part "° to deg"
This is reproducible in the demo JSBin (2021-11-15) and Version 9.4.1
Thank you for your library.
You'll also have to override the isAlpha of the expression parser, math.expression.parse.isAlpha.
See #1268