Skip to content

Commit

Permalink
Merge 8579e4c into 28f9bab
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-hq committed Mar 13, 2017
2 parents 28f9bab + 8579e4c commit 4218e29
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@
/node_modules/*
/nbproject/
/bower_components/*
*.iml
39 changes: 35 additions & 4 deletions lib/money.js
Expand Up @@ -84,8 +84,6 @@ Money.fromInteger = function (amount, currency) {
};

Money.fromDecimal = function (amount, currency) {
var multipliers = [1, 10, 100, 1000];

if (isObject(amount)) {
if (amount.amount === undefined || amount.currency === undefined)
throw new TypeError('Missing required parameters amount,currency');
Expand All @@ -106,8 +104,41 @@ Money.fromDecimal = function (amount, currency) {
throw new Error("The currency " + currency.code + " supports only "
+ currency.decimal_digits + " decimal digits");

var integerAmount = amount * multipliers[currency.decimal_digits];
return new Money(Math.round(integerAmount), currency);
var precisionMultiplier = Math.pow(10, currency.decimal_digits);
var integerAmount = amount * precisionMultiplier;

return new Money(integerAmount, currency);
};

Money.fromDecimalRounded = function (amount, currency, rounder) {
if (isObject(amount)) {
if (amount.amount === undefined || amount.currency === undefined)
throw new TypeError('Missing required parameters amount,currency');

rounder = currency;
currency = amount.currency;
amount = amount.amount;
}

if (isString(currency))
currency = currencies[currency];

if (!isPlainObject(currency))
throw new TypeError('Invalid currency');

if (rounder === undefined)
throw new TypeError('Missing required parameter roundType');

if (['round', 'floor', 'ceil'].indexOf(rounder) === -1 && typeof rounder !== 'function')
throw new TypeError('Invalid parameter rounder');

if (isString(rounder))
rounder = Math[rounder];

var precisionMultiplier = Math.pow(10, currency.decimal_digits);
var roundedIntegerAmount = rounder(amount * precisionMultiplier);

return new Money(roundedIntegerAmount, currency);
};

/**
Expand Down
6 changes: 3 additions & 3 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "js-money",
"description": "JavaScript implementation of the Money value object.",
"version": "0.6.1",
"version": "1.0.1",
"main": "./lib",
"author": {
"name": "David Kalosi",
Expand All @@ -14,10 +14,10 @@
},
"repository": {
"type": "git",
"url": "git://github.com/davidkalosi/js-money.git"
"url": "git://github.com/HotelQuickly/js-money.git"
},
"bugs": {
"url": "https://github.com/davidkalosi/js-money/issues"
"url": "https://github.com/HotelQuickly/js-money/issues"
},
"dependencies": {
"lodash": "4.x.x"
Expand Down
21 changes: 21 additions & 0 deletions test/money.test.js
Expand Up @@ -48,6 +48,27 @@ describe('Money', function () {
}).to.throw(Error);
});

it('should create a new instance from decimal using `.fromDecimalRounded()` even if too many decimal places', function () {
var money = Money.fromDecimalRounded(10.01, Money.EUR, 'ceil');
var money1 = Money.fromDecimalRounded({amount: 10.01, currency: 'EUR'}, Math.ceil);
var money2 = Money.fromDecimalRounded(10.0101, Money.EUR, Math.ceil);
var money3 = Money.fromDecimalRounded(10.0199, Money.EUR, Math.ceil);
var money4 = Money.fromDecimalRounded(10.0199, Money.EUR, Math.floor);
var money5 = Money.fromDecimalRounded(10.0199, Money.EUR, Math.round);
var money6 = Money.fromDecimalRounded(10.0199, Money.EUR, function (amount) {
return Math.round(amount)
});

expect(money.amount).to.equal(1001);
expect(money.currency).to.equal('EUR');
expect(money1.amount).to.equal(1001);
expect(money2.amount).to.equal(1002);
expect(money3.amount).to.equal(1002);
expect(money4.amount).to.equal(1001);
expect(money5.amount).to.equal(1002);
expect(money6.amount).to.equal(1002);
});

it('should create a new instance from string currency', function () {
var money = new Money(1042, 'EUR');

Expand Down

0 comments on commit 4218e29

Please sign in to comment.