Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented "allocate" function, as described by Martin Fowler #1

Merged
merged 2 commits into from
Feb 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
.idea
node_modules

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignoring eclipse IDE project files

.project
.settings/.jsdtscope
.settings/org.eclipse.wst.jsdt.ui.superType.container
.settings/org.eclipse.wst.jsdt.ui.superType.name
50 changes: 50 additions & 0 deletions bigmoney.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,56 @@
return Money(this.val.times.apply(this.val, arguments), this.currency, this.options);
}

/**
* Allocates amounts of money in an array so that you won't loose cents
* @param ratios {Number} How many parts you want to divide the initial amount.
* @returns {*}
*/

function sum(a, b) {
return a + b;
}

function ones(len) {
var arr = [];
for (var i = 0; i < len; i++) {
arr.push(1);
};

return arr;
}

Money.prototype.allocate = function(ratios) {
if(typeof ratios === 'undefined') {
return [this];
} else if(typeof ratios === 'number') {
ratios = ones(ratios);
}

var amount = this,
remainder = amount,
total = ratios.reduce(sum),
results = [],
current = 0;

ratios.forEach(function(ratio, index) {
results.push(amount.times(ratio).div(total));
remainder = remainder.minus(results[index]);
});

while(!remainder.eq(0)) {
results[current] = results[current++].plus(0.01 * remainder.val.s);

if(current >= results.length) {
current = 0;
}

remainder = remainder.plus(0.01 * remainder.val.s * -1);
}

return results;
}

/**
* Convert to over currency
* @param to {String} Destination currency. Default is a settings.base currency.
Expand Down
18 changes: 17 additions & 1 deletion examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,20 @@ console.log( usd.plus(100).valueOf() ); //234.85
console.log( usd.minus(100).valueOf() ); //34.85
console.log( usd.times(2).valueOf() ); //269.7
console.log( usd.div(2).valueOf() ); //67.43
console.log( usd.mod(1).valueOf() ); //0.85
console.log( usd.mod(1).valueOf() ); //0.85

//allocates money
new Money(100).allocate(3).forEach(function(payment) {
//five payments of "same value"
console.log(payment.format());
});

new Money(100).allocate([1, 1, 1]).forEach(function(payment) {
//three equal payments, same as ".allocate(3);" (above example)
console.log(payment.format());
});

new Money(100).allocate([1, 2, 1]).forEach(function(payment) {
//three payments, 25% then 50% then 25%
console.log(payment.format());
});
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bigmoney.js",
"version": "0.0.1",
"version": "0.0.2",
"description": "Library for handling the money values. Based on big.js library for arbitrary-precision decimal arithmetic. Support multiple currencies.",
"main": "bigmoney.js",
"directories": {
Expand All @@ -17,6 +17,11 @@
"type": "git",
"url": "https://github.com/demchenkoe/bigmoney.js"
},
"contributors": [{
"name" : "Renato Gama",
"email" : "renato@gammasoft.com.br",
"url" : "http://github.com/renatoargh"
}],
"keywords": [
"money",
"currency",
Expand Down