Skip to content

Commit

Permalink
Merge b53d0f7 into bc03eaf
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwood committed Jul 17, 2020
2 parents bc03eaf + b53d0f7 commit 5fb05f6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ currency.prototype = {
*/
add(number) {
let { intValue, _settings, _precision } = this;
return currency((intValue += parse(number, _settings)) / _precision, _settings);
let cleanSettings = Object.assign({}, _settings, { fromCents: false });
return currency(
(intValue += parse(number, _settings)) / _precision,
cleanSettings
);
},

/**
Expand All @@ -121,7 +125,11 @@ currency.prototype = {
*/
subtract(number) {
let { intValue, _settings, _precision } = this;
return currency((intValue -= parse(number, _settings)) / _precision, _settings);
let cleanSettings = Object.assign({}, _settings, { fromCents: false });
return currency(
(intValue -= parse(number, _settings)) / _precision,
cleanSettings
);
},

/**
Expand All @@ -131,7 +139,11 @@ currency.prototype = {
*/
multiply(number) {
let { intValue, _settings } = this;
return currency((intValue *= number) / pow(_settings.precision), _settings);
let cleanSettings = Object.assign({}, _settings, { fromCents: false });
return currency(
(intValue *= number) / pow(_settings.precision),
cleanSettings
);
},

/**
Expand All @@ -141,7 +153,11 @@ currency.prototype = {
*/
divide(number) {
let { intValue, _settings } = this;
return currency(intValue /= parse(number, _settings, false), _settings);
let cleanSettings = Object.assign({}, _settings, { fromCents: false });
return currency(
(intValue /= parse(number, cleanSettings, false)),
cleanSettings
);
},

/**
Expand All @@ -155,9 +171,10 @@ currency.prototype = {
, distribution = []
, split = Math[intValue >= 0 ? 'floor' : 'ceil'](intValue / count)
, pennies = Math.abs(intValue - (split * count));
let cleanSettings = Object.assign({}, _settings, { fromCents: false });

for (; count !== 0; count--) {
let item = currency(split / _precision, _settings);
let item = currency(split / _precision, cleanSettings);

// Add any left over pennies
pennies-- > 0 && (item = intValue >= 0 ? item.add(1 / _precision) : item.subtract(1 / _precision));
Expand Down
52 changes: 52 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,33 @@ test('should add floating point value', t => {
t.not(parseFloat(value), 2.51+.01, 'currency(2.51).add(.01) does not equal 2.5199999999999996');
});

test('should maintain fromCents setting when adding', t => {
var value = currency(251, { fromCents: true }).add(1);

t.is(
parseFloat(value),
2.52,
'currency(251, { fromCents: true }).add(1) equals decimal value 2.52'
);
});

test('should subtract floating point', t => {
var value = currency(2.52).subtract(.01);

t.is(parseFloat(value), 2.51, 'currency(2.52).subtract(.01) equals decimal value 2.51');
t.not(parseFloat(value), 2.52-.01, 'currency(2.52).subtract(.01) does not equal 2.5100000000000002');
});

test('should maintain fromCents setting when subtracting', t => {
var value = currency(251, { fromCents: true }).subtract(1);

t.is(
parseFloat(value),
2.50,
'currency(251, { fromCents: true }).subtract(1) equals decimal value 2.50'
);
});

test('should round half up', t => {
var value1 = currency(17.955)
, value2 = currency(17.855)
Expand Down Expand Up @@ -125,6 +145,15 @@ test('currency multiplication with precision', t => {
t.is(parseFloat(value), 4.107, 'currency(1.369).multiply(3) is 4.107');
});

test("currency multiplication with fromCents", (t) => {
var value = currency(123, { fromCents: true }).multiply(2);
t.is(
parseFloat(value),
2.46,
"currency(123, { fromCents: true }).multiply(2) is 2.46"
);
});

test('currency division', t => {
var value = currency(9.87).divide(2);
t.is(parseFloat(value), 4.94, 'currency(9.87).divide(2) is 4.94');
Expand All @@ -135,6 +164,15 @@ test('currency division with precision', t => {
t.is(parseFloat(value), 1.369, 'currency(4.107).divide(3) is 1.369');
});

test("currency division with fromCents", (t) => {
var value = currency(987, { fromCents: true }).divide(2);
t.is(
parseFloat(value),
4.94,
"currency(987, { fromCents: true }).divide(2) is 4.94"
);
});

test('should parse negative values', t => {
var pos = currency(1.23),
neg = currency(-1.23),
Expand Down Expand Up @@ -191,6 +229,20 @@ test('should create non-equal distribution with a negative penny', t => {
t.is(total, -0.01, 'sum of values matches our original amount');
});

test('should create distribution with fromCents', t => {
var values = currency(123, { fromCents: true }).distribute(2);

t.is(parseFloat(values[0]), 0.62, 'first value is not 0.62');
t.is(parseFloat(values[1]), 0.61, 'second value is not 0.61');

var total = 0;
for(var i = 0; i < values.length; i++) {
total += parseFloat(values[i]);
}

t.is(total, 1.23, 'sum of values matches our original amount');
});

test('should get dollar value', t => {
var value = currency(1.23);

Expand Down

0 comments on commit 5fb05f6

Please sign in to comment.