UnitMath icon indicating copy to clipboard operation
UnitMath copied to clipboard

Simplify the API of the formatter

Open josdejong opened this issue 3 years ago • 2 comments

Looking at the docs of the custom formatter, https://github.com/ericman314/UnitMath#custom-formatter , I have the feeling that the formatter option can be simpler, by removing the possibility to pass additional arguments to it via toString. I think you can achieve that already by changing:

unit('3.14159 rad').toString({ formatter: funnyFormatter }, '$', '_'

to

unit('3.14159 rad').toString({ formatter: value => funnyFormatter(value, '$', '_') }

// or 

const funnyFormatter$ = value => funnyFormatter(value, '$', '_')
unit('3.14159 rad').toString({ formatter: funnyFormatter }

josdejong avatar Dec 12 '21 09:12 josdejong

I'm not sure. I definitely agree that the documentation on formatter is too complicated though. I don't remember if there was a reason we had to support passing user args into the formatter. Maybe when I wrote it I had just learned about JavaScript's spread operator and was looking for a nail to hit with that hammer. I may just leave it undocumented.

ericman314 avatar Dec 12 '21 21:12 ericman314

👍 not a big deal, I just felt like the API could be a bit simpler without sacrificing anything.

josdejong avatar Dec 13 '21 07:12 josdejong

I ended up taking your advice in 1.0.0-rc2 and removed the additional arguments to the custom formatter, under the principle of "you aren't gonna need it."

ericman314 avatar Feb 19 '23 07:02 ericman314

👍

josdejong avatar Feb 20 '23 08:02 josdejong