goodeggs / goodeggs-money

Reliable money math with BigNumber wrapped inside Cents
MIT License
3 stars 0 forks source link

Encourage functional use #1

Open hurrymaplelad opened 9 years ago

hurrymaplelad commented 9 years ago

I think we should encourage pure functional use of this library. For example:

# Prefer this
total = Cents.percent(Cents.sum(10, 25), 0.5, transform: 'round')

# Over this
total = new Cents(10).plus(new Cents(25)).percent(0.5, transform: 'round').toNumber()

We commonly serialize and deserialize cents values as numbers. I believe the interfaces between modules should be defined in terms of Numbers, not Cents objects, so that dependencies are explicit and so that individual modules can choose more appropriate libraries (like higher precision or big number) as needed.

In that world, new Cents() and .toNumber() are boilerplate to write and a small performance overhead.