Skip to content

Commit

Permalink
Merge pull request #262 from scurker/fix-from-cents
Browse files Browse the repository at this point in the history
fix methods not returning correct values when using fromCents
  • Loading branch information
scurker committed Jul 18, 2020
2 parents bc03eaf + ee2edb9 commit 508fba4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/currency.js
Expand Up @@ -56,10 +56,15 @@ function parse(value, opts, useRounding = true) {
let v = 0
, { decimal, errorOnInvalid, precision: decimals, fromCents } = opts
, precision = pow(decimals)
, isNumber = typeof value === 'number';
, isNumber = typeof value === 'number'
, isCurrency = value instanceof currency;

if (isNumber || value instanceof currency) {
v = (isNumber ? value : value.value);
if (isCurrency && fromCents) {
return value.intValue;
}

if (isNumber || isCurrency) {
v = isCurrency ? value.value : value;
} else if (typeof value === 'string') {
let regex = new RegExp('[^-\\d' + decimal + ']', 'g')
, decimalString = new RegExp('\\' + decimal, 'g');
Expand Down Expand Up @@ -111,7 +116,7 @@ currency.prototype = {
*/
add(number) {
let { intValue, _settings, _precision } = this;
return currency((intValue += parse(number, _settings)) / _precision, _settings);
return currency((intValue += parse(number, _settings)) / (_settings.fromCents ? 1 : _precision), _settings);
},

/**
Expand All @@ -121,7 +126,7 @@ currency.prototype = {
*/
subtract(number) {
let { intValue, _settings, _precision } = this;
return currency((intValue -= parse(number, _settings)) / _precision, _settings);
return currency((intValue -= parse(number, _settings)) / (_settings.fromCents ? 1 : _precision), _settings);
},

/**
Expand All @@ -131,7 +136,7 @@ currency.prototype = {
*/
multiply(number) {
let { intValue, _settings } = this;
return currency((intValue *= number) / pow(_settings.precision), _settings);
return currency((intValue *= number) / (_settings.fromCents ? 1 : pow(_settings.precision)), _settings);
},

/**
Expand Down
31 changes: 31 additions & 0 deletions test/test.js
Expand Up @@ -548,3 +548,34 @@ test('should truncate decimals from a string when using fromCents option', t =>
t.is(c3.value, 0.123);
t.is(c3.intValue, 123);
});

test('should handle add with fromCents option', t => {
let c1 = currency(12345, { fromCents: true });
let c2 = currency(123, { fromCents: true });

t.is(c1.add(123).value, 124.68);
t.is(c1.add(c2).value, 124.68);
});

test('should handle subtract with fromCents option', t => {
let c1 = currency(12345, { fromCents: true });
let c2 = currency(123, { fromCents: true });

t.is(c1.subtract(123).value, 122.22);
t.is(c1.subtract(c2).value, 122.22);
});

test('should handle multiply with fromCents option', t => {
let c1 = currency(12345, { fromCents: true });
t.is(c1.multiply(2).value, 246.90);
});

test('should handle divide with fromCents option', t => {
let c1 = currency(12345, { fromCents: true });
t.is(c1.divide(2).value, 61.72);
});

test('should handle distribute with fromCents option', t => {
var values = currency(1.00).distribute(4);
t.deepEqual(values.map(v => v.value), [.25, .25, .25, .25]);
});

0 comments on commit 508fba4

Please sign in to comment.