Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#303 added equals method to currency object #310

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/04-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ Divides the current currency instance by `number`.
currency(123.45).divide(2); // => "61.73"
```

### equals

`currency.equals( number|currency )`

Compares the current currency's value to another currency or `number` value.

```js
currency(123.45).equals(currency(123.45)); // => true
currency(10).equals(10); // => true
```

### distribute

`currency.distribute( number )`
Expand Down
15 changes: 15 additions & 0 deletions src/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,21 @@ currency.prototype = {
return currency(intValue /= parse(number, _settings, false), _settings);
},

/**
* Compares value to another currency instance's value
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How should this handle amounts of different currencies? (e.g. comparing dollars to euros?)

* @param {number|currency} foreign
* @returns {boolean}
*/
equals(foreign) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by comment: "foreign" has a specific connotation in dealing with currencies (e.g. "foreign exchange rate") which falsely implies that this may deal with currency conversions. Perhaps another variable name like other would be less ambiguous?

let { value } = this;
if (foreign instanceof currency) {
let { value: foreignValue } = foreign;

return value === foreignValue;
}
return foreign === value;
},

/**
* Takes the currency amount and distributes the values evenly. Any extra pennies
* left over from the distribution will be stacked onto the first set of entries.
Expand Down
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,4 +555,15 @@ test('should handle fractional cents', t => {
var values = currency(1234.56, { fromCents: true });
t.is(values.intValue, 1235);
t.is(values.value, 12.35);
});

test('equals', t => {
let c1 = currency(2.52).subtract(.01);
let c2 = currency(2.50).add(.01);
let c3 = currency(10);

t.is(c1.equals(c2), true);
t.is(c3.equals(c1), false);
t.is(c3.equals(10), true);
t.is(c3.equals(10.0), true);
});