Skip to content

Commit f979be2

Browse files
committedMay 19, 2015
add ability to add two money of different currencies
1 parent a4e9d80 commit f979be2

File tree

4 files changed

+101
-24
lines changed

4 files changed

+101
-24
lines changed
 

‎lib/money.js

+53-14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ var BigDecimal = require('big.js');
1616
* @public
1717
*/
1818
function Money(amount, currency, time) {
19+
//TODO add ability to instantiate money with varargs
20+
1921
//set a date when this money has been created
2022
//Note!: Money value flactuate with time
2123
//
@@ -171,13 +173,31 @@ Money.prototype.divide = Money.prototype.divideBy = function(divisor) {
171173
* @returns {Money} a new money instance which hold the result of operation
172174
*/
173175
Money.prototype.add =
174-
Money.prototype.plus = function(other) {
175-
//check if they have same currency
176-
if (_.isEqual(this.currency, other.currency)) {
177-
return new Money(this.amount.plus(other.amount), this.currency);
178-
} else {
179-
//TODO handle diffent currency
180-
return this;
176+
Money.prototype.plus = function(other, done) {
177+
//this is of money instance context
178+
var self = this;
179+
180+
//other money instance has the same
181+
//currency as this money instance
182+
if (_.isEqual(self.currency, other.currency)) {
183+
done(null, new Money(self.amount.plus(other.amount), self.currency));
184+
}
185+
186+
//other have differnt currency to this
187+
//money instance
188+
else {
189+
async.waterfall([
190+
function convertOtherToThisMoneyCurrency(next) {
191+
other.convertTo(self.currency, next);
192+
},
193+
function sum(exchanged, next) {
194+
next(null,
195+
new Money(
196+
self.amount.plus(exchanged.amount),
197+
self.currency)
198+
);
199+
}
200+
], done);
181201
}
182202
};
183203

@@ -192,14 +212,33 @@ Money.prototype.add =
192212
* @private
193213
*/
194214
Money.prototype.subtract =
195-
Money.prototype.minus = function(other) {
196-
//check if they have same currency
197-
if (_.isEqual(this.currency, other.currency)) {
198-
return new Money(this.amount.minus(other.amount), this.currency);
199-
} else {
200-
//TODO handle diffent currency
201-
return this;
215+
Money.prototype.minus = function(other, done) {
216+
//this if of money instance context
217+
var self = this;
218+
219+
//other money instance has the same
220+
//currency as this money instance
221+
if (_.isEqual(self.currency, other.currency)) {
222+
done(null, new Money(self.amount.minus(other.amount), self.currency));
202223
}
224+
225+
//other have differnt currency to this
226+
//money instance
227+
else {
228+
async.waterfall([
229+
function convertOtherToThisMoneyCurrency(next) {
230+
other.convertTo(self.currency, next);
231+
},
232+
function subtract(exchanged, next) {
233+
next(null,
234+
new Money(
235+
self.amount.minus(exchanged.amount),
236+
self.currency)
237+
);
238+
}
239+
], done);
240+
}
241+
203242
};
204243

205244

‎moneyjs.png

1.85 KB
Loading

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
2-
"name": "MoneyJS",
2+
"name": "moneyjs",
33
"version": "0.1.0",
44
"description": "Nodejs library to represent monetary amounts",
55
"keywords": [
66
"money",
7+
"monetary",
78
"currency",
89
"convert",
910
"coin",

‎test/money.spec.js

+46-9
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,27 @@ describe('Money', function() {
101101
it('should be able to perform addition on two money instance of the same currency', function(done) {
102102
var price = new Money(12, Money.USD);
103103
var tax = new Money(2, Money.USD);
104-
var priceAfterTax = price.plus(tax);
104+
price.plus(tax, function(error, priceAfterTax) {
105105

106-
expect(priceAfterTax.amount.toString()).to.be.equal('14');
107-
expect(price.currency.code).to.be.equal('USD');
106+
expect(priceAfterTax.amount.toString()).to.be.equal('14');
107+
expect(price.currency.code).to.be.equal('USD');
108108

109-
done();
109+
done(error, priceAfterTax);
110+
});
110111

111112
});
112113

114+
113115
it('should be able to perform subtraction on two money instance of the same currency', function(done) {
114-
var price = new Money(12, Money.USD);
116+
var profit = new Money(12, Money.USD);
115117
var tax = new Money(2, Money.USD);
116-
var netProfit = price.minus(tax);
118+
profit.minus(tax, function(error, netProfit) {
117119

118-
expect(netProfit.amount.toString()).to.be.equal('10');
119-
expect(netProfit.currency.code).to.be.equal('USD');
120+
expect(netProfit.amount.toString()).to.be.equal('10');
121+
expect(netProfit.currency.code).to.be.equal('USD');
120122

121-
done();
123+
done(error, netProfit);
124+
});
122125

123126
});
124127
});
@@ -304,6 +307,40 @@ describe('Money', function() {
304307

305308
});
306309

310+
311+
it('should be able to perform addition on two money instance of the different currency', function(done) {
312+
var price = new Money(1, Money.KES);
313+
var tax = new Money(1800, Money.TZS);
314+
price.plus(tax, function(error, priceAfterTax) {
315+
316+
expect(error).to.be.null;
317+
expect(priceAfterTax).to.not.be.null;
318+
319+
expect(priceAfterTax.amount.toString()).to.be.equal('91');
320+
expect(price.currency.code).to.be.equal('KES');
321+
322+
done(error, priceAfterTax);
323+
});
324+
325+
});
326+
327+
328+
it('should be able to perform subtraction on two money instance of the different currency', function(done) {
329+
var profit = new Money(950, Money.KES);
330+
var tax = new Money(1800, Money.TZS);
331+
profit.minus(tax, function(error, netProfit) {
332+
333+
expect(error).to.be.null;
334+
expect(netProfit).to.not.be.null;
335+
336+
expect(netProfit.amount.toString()).to.be.equal('860');
337+
expect(netProfit.currency.code).to.be.equal('KES');
338+
339+
done(error, netProfit);
340+
});
341+
342+
});
343+
307344
});
308345

309346

0 commit comments

Comments
 (0)
Please sign in to comment.