mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

Cannot use defined Unit using ° (U+00B0)

Open modellking opened this issue 4 years ago • 1 comments

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.

modellking avatar Nov 15 '21 10:11 modellking

You'll also have to override the isAlpha of the expression parser, math.expression.parse.isAlpha.

See #1268

josdejong avatar Nov 17 '21 15:11 josdejong