Mathjs: Customize supported characters for units

Created on 30 Sep 2018  Â·  1Comment  Â·  Source: josdejong/mathjs

Following the instructions in the documentation on overriding the 'math.expression.parse.isAlpha' function, I have successfully customised supported characters for variables.

Can I do the same for units? The issue I'm running into is syntax errors when defining aliases for certain currencies with non-alpha symbols (i.e '£' for GBP & '€' for EUR).

Here's my code:

math.createUnit('USD')
math.createUnit('EUR', {definition: '1.18 USD', aliases: ['€']})
math.eval('10 USD in €')

I get "SyntaxError: Unexpected "€" in "€" at index 0"

feature help wanted

Most helpful comment

Good point!

Right now Unit has its own parseUnit function which only allows a-zA-Z0-9. I think it should use the the math.expression.parse.isAlpha instead, to make the behavior consistent.

Anyone interested in implementing this change? I don't think it's very complicated.

>All comments

Good point!

Right now Unit has its own parseUnit function which only allows a-zA-Z0-9. I think it should use the the math.expression.parse.isAlpha instead, to make the behavior consistent.

Anyone interested in implementing this change? I don't think it's very complicated.

Was this page helpful?
0 / 5 - 0 ratings