diff options
| author | Marak <[email protected]> | 2014-09-13 20:27:50 +0200 |
|---|---|---|
| committer | Marak <[email protected]> | 2014-09-13 20:27:50 +0200 |
| commit | 9f3fa0f8b7a8cc4f06fe545af02aca34e473c521 (patch) | |
| tree | dc3b59ba68063106907416f9127c8826eea1e007 | |
| parent | c39e254f2775aa4fcf48b05c9a61b78b74843830 (diff) | |
| parent | a5da426f33aab13d0946be2c14ae0dae7c302500 (diff) | |
| download | faker-9f3fa0f8b7a8cc4f06fe545af02aca34e473c521.tar.xz faker-9f3fa0f8b7a8cc4f06fe545af02aca34e473c521.zip | |
[api] Added basic financial generators Closes #32
Merge https://github.com/josefsalyer/Faker.js
| -rw-r--r-- | index.js | 1 | ||||
| -rw-r--r-- | lib/definitions.js | 4 | ||||
| -rw-r--r-- | lib/finance.js | 70 | ||||
| -rw-r--r-- | lib/helpers.js | 14 | ||||
| -rw-r--r-- | test/finance.unit.js | 197 | ||||
| -rw-r--r-- | test/helpers.unit.js | 13 |
6 files changed, 298 insertions, 1 deletions
@@ -29,3 +29,4 @@ exports.Tree = require('./lib/tree'); exports.Date = require('./lib/date'); exports.random = require('./lib/random'); exports.definitions = require('./lib/definitions'); +exports.Finance = require('./lib/finance'); diff --git a/lib/definitions.js b/lib/definitions.js index 36bde57d..5b805c23 100644 --- a/lib/definitions.js +++ b/lib/definitions.js @@ -1405,3 +1405,7 @@ exports.avatar_uri = []; for (var i = 0; i < avatarUri.length; i++) { exports.avatar_uri.push("https://s3.amazonaws.com/uifaces/faces/twitter/" + avatarUri[i]); }; + +exports.account_type = ["Checking","Savings","Money Market", "Investment", "Home Loan", "Credit Card", "Auto Loan", "Personal Loan"]; + +exports.transaction_type = ["deposit", "withdrawal", "payment", "invoice"];
\ No newline at end of file diff --git a/lib/finance.js b/lib/finance.js new file mode 100644 index 00000000..4bf50bff --- /dev/null +++ b/lib/finance.js @@ -0,0 +1,70 @@ +var Helpers = require('./helpers'); +var definitions = require('./definitions'); + +var finance = { + + account: function (length) { + + length = length || 8; + + var template = ''; + + for (var i = 0; i < length; i++) { + template = template + '#'; + } + length = null; + return Helpers.replaceSymbolWithNumber(template); + }, + + accountName: function () { + + return [Helpers.randomize(definitions.account_type), 'Account'].join(' '); + }, + + mask: function (length, parens, elipsis) { + + + //set defaults + length = (length == 0 || !length || typeof length == 'undefined') ? 4 : length; + parens = (parens === null) ? true : parens; + elipsis = (elipsis === null) ? true : elipsis; + + //create a template for length + var template = ''; + + for (var i = 0; i < length; i++) { + template = template + '#'; + } + + //prefix with elipsis + template = (elipsis) ? ['...', template].join('') : template; + + template = (parens) ? ['(', template, ')'].join('') : template; + + //generate random numbers + template = Helpers.replaceSymbolWithNumber(template); + + 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 + + amount: function (min, max, dec, symbol) { + + min = min || 0; + max = max || 1000; + dec = dec || 2; + symbol = symbol || ''; + + return symbol + (Math.round((Math.random() * (max - min) + min) * Math.pow(10, dec)) / Math.pow(10, dec)).toFixed(dec); + + }, + + transactionType: function () { + return Helpers.randomize(definitions.transaction_type); + } +}; + +module.exports = finance;
\ No newline at end of file diff --git a/lib/helpers.js b/lib/helpers.js index 7d146c8d..31819615 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -84,7 +84,8 @@ exports.createCard = function () { "sentences": faker.Lorem.sentences(), "paragraph": faker.Lorem.paragraph() } - ] + ], + "accountHistory": [faker.Helpers.createTransaction(), faker.Helpers.createTransaction(), faker.Helpers.createTransaction()] }; }; @@ -114,6 +115,16 @@ 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 @@ -122,3 +133,4 @@ String.prototype.capitalize = function () { //v1.0 }); }; */ + diff --git a/test/finance.unit.js b/test/finance.unit.js new file mode 100644 index 00000000..1a8978d6 --- /dev/null +++ b/test/finance.unit.js @@ -0,0 +1,197 @@ +if (typeof module !== 'undefined') { + var assert = require('assert'); + var sinon = require('sinon'); + var faker = require('../index'); +} + + +describe('finance.js', function () { + describe('account( length )', function () { + + it('should supply a default length if no length is passed', function () { + + var account = faker.Finance.account(); + + var expected = 8; + var actual = account.length; + + assert.equal(actual, expected, 'The expected default account length is ' + expected + ' but it was ' + actual); + + }); + + it('should supply a length if a length is passed', function () { + + var expected = 9; + + var account = faker.Finance.account(expected); + + var actual = account.length; + + assert.equal(actual, expected, 'The expected default account length is ' + expected + ' but it was ' + actual); + + }); + + it('should supply a default length if a zero is passed', function () { + + var expected = 8; + + var account = faker.Finance.account(0); + + var actual = account.length; + + assert.equal(actual, expected, 'The expected default account length is ' + expected + ' but it was ' + actual); + + }); + + }); + + describe('accountName()', function () { + + it("should return an account name", function () { + + var actual = faker.Finance.accountName(); + + assert.ok(actual); + + }); + + }); + + + describe('mask( length, parens, elipsis )', function () { + it("should set a default length", function () { + + var expected = 4; //default account mask length + + var mask = faker.Finance.mask(null, false, false); + + var actual = mask.length; + + assert.equal(actual, expected, 'The expected default mask length is ' + expected + ' but it was ' + actual); + + }); + + it("should set a specified length", function () { + + var expected = faker.random.number(20); + + expected = (expected == 0 || !expected || typeof expected == 'undefined') ? 4 : expected; + + var mask = faker.Finance.mask(expected, false, false); + + var actual = mask.length; //picks 4 if the random number generator picks 0 + + assert.equal(actual, expected, 'The expected default mask length is ' + expected + ' but it was ' + actual); + + }); + + it("should set a default length of 4 for a zero value", function () { + + var expected = 4; + + var mask = faker.Finance.mask(0, false, false); + + var actual = 4; //picks 4 if the random number generator picks 0 + + assert.equal(actual, expected, 'The expected default mask length is ' + expected + ' but it was ' + actual); + + }); + + + it("should by default include parentheses around a partial account number", function () { + + var expected = true; + + var mask = faker.Finance.mask(null, null, false); + + var regexp = new RegExp(/(\(\d{4}?\))/); + var actual = regexp.test(mask); + + assert.equal(actual, expected, 'The expected match for parentheses is ' + expected + ' but it was ' + actual); + + }); + + it("should by default include an elipsis", function () { + + var expected = true; + + var mask = faker.Finance.mask(null, false, null); + + var regexp = new RegExp(/(\.\.\.\d{4})/); + var actual = regexp.test(mask); + + assert.equal(actual, expected, 'The expected match for parentheses is ' + expected + ' but it was ' + actual); + + }); + + it("should work when random variables are passed into the arguments", function () { + + var length = faker.random.number(20); + var elipsis = (length % 2 === 0) ? true : false; + var parens = !elipsis; + + var mask = faker.Finance.mask(length, elipsis, parens); + assert.ok(mask); + + }); + + + }); + + describe('amount(min, max, dec, symbol)', function () { + + it("should use the default amounts when not passing arguments", function () { + var amount = faker.Finance.amount(); + + assert.ok(amount); + assert.equal((amount > 0), true, "the amount should be greater than 0"); + assert.equal((amount < 1001), true, "the amount should be greater than 0"); + + }); + + it("should use the defaul decimal location when not passing arguments", function () { + + var amount = faker.Finance.amount(); + + var decimal = '.'; + var expected = amount.length - 3; + var actual = amount.indexOf(decimal); + + assert.equal(actual, expected, 'The expected location of the decimal is ' + expected + ' but it was ' + actual + ' amount ' + amount); + }); + + //TODO: add support for more currency and decimal options + it("should not include a currency symbol by default", function () { + + var amount = faker.Finance.amount(); + + var regexp = new RegExp(/[0-9.]/); + + var expected = true; + var actual = regexp.test(amount); + + assert.equal(actual, expected, 'The expected match should not include a currency symbol'); + }); + + + it("it should handle negative amounts", function () { + + var amount = faker.Finance.amount(-200, -1); + + assert.ok(amount); + assert.equal((amount < 0), true, "the amount should be greater than 0"); + assert.equal((amount > -201), true, "the amount should be greater than 0"); + }); + + + }); + + describe('transactionType()', function () { + + it("should return a random transaction type", function () { + var transactionType = faker.Finance.transactionType(); + + assert.ok(transactionType); + }); + }); +});
\ No newline at end of file diff --git a/test/helpers.unit.js b/test/helpers.unit.js index 335c6b1a..5a5798aa 100644 --- a/test/helpers.unit.js +++ b/test/helpers.unit.js @@ -59,4 +59,17 @@ describe("helpers.js", function () { assert.ok(arr.indexOf(elem) !== -1); }); }); + + describe("createTransaction()", function() { + it("should create a random transaction", function() { + var transaction = faker.Helpers.createTransaction(); + assert.ok(transaction); + assert.ok(transaction.amount); + assert.ok(transaction.date); + assert.ok(transaction.business); + assert.ok(transaction.name); + assert.ok(transaction.type); + assert.ok(transaction.account); + }); + }); }); |
