scurker / currency.js

A javascript library for handling currencies

Home Page:https://currency.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature Request] comparison and "default" to value

QAnders opened this issue · comments

Love the module and I want to start with a huge Thank you for your efforts!

I'd love two addons in functionality, the first being to "default" to .value if omitted, meaning if I do this it would compare c1.value === c2.value:

var c1 = currency(2.52).subtract(.01);
var c2 = currency(2.50).add(.01);
c1=== c2; // => true

Another one we frequently use is various comparisons that becomes a bit long and "messy" sometimes because of the .value and that we have to "prepare" the currency objects.
(I am aware of the babel add-on but we'd like to keep our dependency stack as small as possible).

Ideally, I'd like to see something like:

if (currency(1.24).gt(1.23) && currency(1.23).lt(1.24)) {  // Maybe something like `.gteq()` (greater than or equal) and `.lteq()` as well...?
  console.log('1.24 is greater than 1.23 and 1.23 is less than 1.24);
}

if (currency(1.23).eq(1.23)) {
  console.log('1.23 equals 1.23);
}

if (currency(1.24).neq(1.23)) {
  console.log('1.24 does not equal 1.23);
}

Just some thoughts... Love the module either way!!! 🥇

commented

@QAnders Thanks for your feedback!

I would normally suggest the babel plugin to do what you're doing, but it looks like you've already looked into that. I've considered trying to utilize some other javascript features to do what you're suggesting but I haven't decided if it would be backwards compatible breaking. This is something I'd like to investigate to see if its possible to natively allow for value comparisons without adding too much size to the library.