Skip to content

Commit

Permalink
fix(equality): allow different currencies for .eq and .ne
Browse files Browse the repository at this point in the history
Testing for equality should not throw whe the currencies are different.
  • Loading branch information
richardschneider committed Nov 17, 2016
1 parent f2a44c6 commit 46b14ea
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/money.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ Money.prototype.compare = function compare(that) {
};

Money.prototype.eq = function eq(that) {
return this.compare(that) === 0;
return this.currency === that.currency && this.compare(that) === 0;
};

Money.prototype.ne = function ne(that) {
return this.compare(that) !== 0;
return this.currency !== that.currency || this.compare(that) !== 0;
};

Money.prototype.lt = function lt(that) {
Expand Down
18 changes: 18 additions & 0 deletions test/money.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,24 @@ describe('Money', () => {
new Money(1, 'NZD').isNegative().should.equal(false);
});

it('should return false when currencies are different and testing equality', () => {
let a = new Money(30, 'NZD');
let b = new Money(30, 'NZD');
let c = new Money(30, 'USD');

a.eq(b).should.equal(true);
a.eq(c).should.equal(false);
});

it('should return true when currencies are different and testing inequality', () => {
let a = new Money(30, 'NZD');
let b = new Money(30, 'NZD');
let c = new Money(30, 'USD');

a.ne(b).should.equal(false);
a.ne(c).should.equal(true);
});

});

describe('Serialisation', () => {
Expand Down

0 comments on commit 46b14ea

Please sign in to comment.