gentooboontoo / js-quantities

JavaScript library for quantity calculation and unit conversion
http://gentooboontoo.github.io/js-quantities/
MIT License
396 stars 102 forks source link

Fails to parse mAh (milliamp hours) #25

Closed bombledmonk closed 9 years ago

bombledmonk commented 9 years ago

The function fails to parse units of amp hours or milliamp hours. Qty('1 Ah'); //exception QtyError: Unit not recognized Qty.parse('1 Ah') // null

I haven't quite dissected where it is failing as my experience with the library is minimal. It may be that the units are incompatible as is, but I'm not sure yet, nor as to the reason why.

Qty('1 A h') parses to "1 A*h" which is certainly satisfactory, but can someone with a bit deeper knowledge elaborate on why the title values do not work?

rage-shadowman commented 9 years ago

This would require adding "Ah" as a unit. We can't simply guess what the combined units mean or we would have issues with things like "mm" (millimeters? or meter*meter?).

gentooboontoo commented 9 years ago

Disambiguating theses cases could be supported by choosing a conventional way to parse them.

For cases represented by joined characters with no separators (other cases are not ambiguous), the first character could be always considered as a prefix (if compatible of course) and the remaining characters would be parsed as raw units. If this convention is followed, we have:

'mm': milli, meter 'm m': meter, meter

'mN: milli, newton 'Nm: newton, meter 'm*N': meter, newton 'm N': meter, newton 'mmN': milli, meter, newton 'mNm': milli, newton, meter

'Ah', // ampere, hour 'hA', // hecto, ampere 'mAh', // milli, ampere, hour

Like so, it seems to follow the principle of least surprise.

rage-shadowman commented 9 years ago

To play devil's advocate here, I would argue that hecto isn't very commonly used and thus hA not being hour*ampere might be more surprising than you would expect (and the same goes for yocto, maybe less so for deci).

rage-shadowman commented 9 years ago

However, when I first started using this library, I was also surprised that this guess-work was not happening. But after looking into it and seeing the ambiguity I decided that wasn't such an issue for me.

gentooboontoo commented 9 years ago

Two arguments in favor of considering "hA" as hecto, ampere:

That said, I really appreciate your opinions @rage-shadowman. It is a great help.

rage-shadowman commented 9 years ago

Q: Does ruby-units pars "Ah" as ampere*hour?

If ruby-units does the guess work in this manner, then I agree with you (especially since I see no mention of "Ah" specifically in the ruby-units source). I do not have a running ruby environment to test it out on though.

gentooboontoo commented 9 years ago

No. Ironically it fails to parse it. But it is not surprising because js-quantities regexps were originally the same than Ruby-units ones. However, I would like to fix it because it is a bug.

rage-shadowman commented 9 years ago

Are you sure they consider it a bug in ruby-units? How would you define "hrs" then? hour or hour*second? There are likely many less obvious examples of ambiguity due to all of the aliases that are allowed for each unit. When I looked into making this change I gave up after I found a few that I couldn't state empirically should always go one way.

rage-shadowman commented 9 years ago

I do agree that "Ah" is a commonly used unit and thus may deserve its own unit definition. But I personally think that guess-work which results in direct answers to ambiguous questions is a bad idea.

bombledmonk commented 9 years ago

I don't really have any authority in this matter, but given the conversation it does seem rage-shadowman's suggestion to give "Ah" its own definition is the solution with the least risk.