Skip to content

Commit

Permalink
added to node lib. all tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
josefsalyer committed Nov 4, 2012
1 parent 13fd54d commit 8f48f33
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 14 deletions.
37 changes: 28 additions & 9 deletions Faker.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ exports.Company = require('./lib/company');
exports.Lorem = require('./lib/lorem');
exports.Helpers = require('./lib/helpers');
exports.definitions = require('./lib/definitions');
exports.Finance = require('./lib/finance');
2 changes: 2 additions & 0 deletions lib/definitions.js

Large diffs are not rendered by default.

68 changes: 68 additions & 0 deletions lib/finance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
var Helpers = require('./helpers');
var definitions = require('./definitions');


exports.account = function(length){

if(!length) length = 8;

var template = '';

for(var i=0; i<length; i++)
{
template = template + '#';
}

return Helpers.replaceSymbolWithNumber(template);
};


exports.accountName = function(){

return [Helpers.randomize(definitions.account_type()), 'Account'].join(' ')
};

exports.mask = function(length, parens, elipsis){

//set defaults
if(!length) length = 4;
if(!parens) parens = true;
if(!elipsis) elipsis = true;

//create a template for length
var template = '';

for(var i=0; i<length; i++)
{
template = template + '#';
}

//generate random numbers
template = Helpers.replaceSymbolWithNumber(template);

//prefix with elipsis
if(elipsis) template = ['...', template].join('');

if(parens) template = ['(', template ,')'].join('');

return template;

};

//min and max take in minimum and maximum amounts, dec is the decimal place you want rounded to, symbol is $, €, £, etc
//NOTE: this returns a string representation of the value, if you want a number use parseFloat and no symbol
exports.amount = function(min, max, dec, symbol){

if(!min) min = 1
if(!max) max = 1000
if(!dec) dec = 2
if(!symbol) symbol = '' //default to nothing


return symbol + Math.round((Math.random() * (max - min) + min)*Math.pow(10,dec))/Math.pow(10,dec);

};

exports.transactionType = function(){
return Helpers.randomize(definitions.transaction_type());
};
15 changes: 14 additions & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,22 @@ exports.userCard = function(){
};
};

exports.createTransaction = function(){

return {
"amount" : Faker.Finance.amount(),
"date" : new Date(2012, 1, 2), //TODO: add a ranged date method
"business": Faker.Company.companyName(),
"name": [Faker.Finance.accountName(), Faker.Finance.mask()].join(' '),
"type" : exports.randomize(Faker.definitions.transaction_type()),
"account" : Faker.Finance.account()
};
};

String.prototype.capitalize = function(){ //v1.0
return this.replace(/\w+/g, function(a){
return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
});
};
};


6 changes: 3 additions & 3 deletions tests/browser_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
}
document.write(JSON.stringify(card));

//added tests for financial pieces


//tests financial additions
var transaction = Faker.Helpers.createTransaction()
document.write(JSON.stringify(transaction));

</script>
</head>
Expand Down
7 changes: 6 additions & 1 deletion tests/library_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ var definitions = require('../lib/definitions');

var Faker = require('../index');

var card = Faker.Helpers.createCard();
var card = new Faker.Helpers.createCard();

util.puts(JSON.stringify(card));

var transaction = Faker.Helpers.createTransaction();

util.puts(JSON.stringify(transaction));

0 comments on commit 8f48f33

Please sign in to comment.