Skip to content

Commit

Permalink
Fix aomount and price argument dec for case = 0. Add unit tests. Mino…
Browse files Browse the repository at this point in the history
…r codestyle fix.
  • Loading branch information
alexpts committed Feb 11, 2017
1 parent 1df4877 commit 434b317
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
8 changes: 5 additions & 3 deletions lib/commerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ var Commerce = function (faker) {
* @param {number} max
* @param {number} dec
* @param {string} symbol
*
* @return {string}
*/
self.price = function(min, max, dec, symbol) {
min = min || 0;
max = max || 1000;
dec = dec || 2;
dec = dec === undefined ? 2 : dec;
symbol = symbol || '';

if(min < 0 || max < 0) {
if (min < 0 || max < 0) {
return symbol + 0.00;
}

Expand Down Expand Up @@ -109,7 +111,7 @@ var Commerce = function (faker) {
*/
self.product = function() {
return faker.random.arrayElement(faker.definitions.commerce.product_name.product);
}
};

return self;
};
Expand Down
26 changes: 12 additions & 14 deletions lib/finance.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/**
*
* @namespace faker.finance
*/
var Finance = function (faker) {
Expand All @@ -23,7 +22,7 @@ var Finance = function (faker) {
}
length = null;
return Helpers.replaceSymbolWithNumber(template);
}
};

/**
* accountName
Expand All @@ -33,7 +32,7 @@ var Finance = function (faker) {
self.accountName = function () {

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

/**
* mask
Expand All @@ -45,7 +44,6 @@ var Finance = function (faker) {
*/
self.mask = function (length, parens, elipsis) {


//set defaults
length = (length == 0 || !length || typeof length == 'undefined') ? 4 : length;
parens = (parens === null) ? true : parens;
Expand All @@ -67,8 +65,7 @@ var Finance = function (faker) {
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
Expand All @@ -81,6 +78,8 @@ var Finance = function (faker) {
* @param {number} max
* @param {number} dec
* @param {string} symbol
*
* @return {string}
*/
self.amount = function (min, max, dec, symbol) {

Expand All @@ -91,8 +90,7 @@ var Finance = function (faker) {
var randValue = faker.random.number({ max: max, min: min, precision: Math.pow(10, -dec) });

return symbol + randValue.toFixed(dec);

}
};

/**
* transactionType
Expand All @@ -101,7 +99,7 @@ var Finance = function (faker) {
*/
self.transactionType = function () {
return Helpers.randomize(faker.definitions.finance.transaction_type);
}
};

/**
* currencyCode
Expand All @@ -110,7 +108,7 @@ var Finance = function (faker) {
*/
self.currencyCode = function () {
return faker.random.objectElement(faker.definitions.finance.currency)['code'];
}
};

/**
* currencyName
Expand All @@ -119,7 +117,7 @@ var Finance = function (faker) {
*/
self.currencyName = function () {
return faker.random.objectElement(faker.definitions.finance.currency, 'key');
}
};

/**
* currencySymbol
Expand All @@ -133,7 +131,7 @@ var Finance = function (faker) {
symbol = faker.random.objectElement(faker.definitions.finance.currency)['symbol'];
}
return symbol;
}
};

/**
* bitcoinAddress
Expand All @@ -149,7 +147,7 @@ var Finance = function (faker) {
address += faker.random.alphaNumeric().toUpperCase();

return address;
}
}
};
};

module['exports'] = Finance;
17 changes: 17 additions & 0 deletions test/commerce.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@ describe("commerce.js", function() {
assert.ok(amount);
assert.equal((amount == 0.00), true, "the amount should equal 0");
});

it("it should handle argument dec", function () {

var price = faker.commerce.price(100, 100, 1);

assert.ok(price);
assert.strictEqual(price , '100.0', "the price should be equal 100.0");
});

it("it should handle argument dec = 0", function () {

var price = faker.commerce.price(100, 100, 0);

assert.ok(price);
assert.strictEqual(price , '100', "the price should be equal 100");
});

});

});
18 changes: 17 additions & 1 deletion test/finance.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,22 @@ describe('finance.js', function () {
});


it("it should handle argument dec", function () {

var amount = faker.finance.amount(100, 100, 1);

assert.ok(amount);
assert.strictEqual(amount , '100.0', "the amount should be equal 100.0");
});

it("it should handle argument dec = 0", function () {

var amount = faker.finance.amount(100, 100, 0);

assert.ok(amount);
assert.strictEqual(amount , '100', "the amount should be equal 100");
});

});

describe('transactionType()', function () {
Expand All @@ -201,7 +217,7 @@ describe('finance.js', function () {

assert.ok(currencyCode.match(/[A-Z]{3}/));
});
})
});

describe("bitcoinAddress()", function(){
it("returns a random bitcoin address", function(){
Expand Down

0 comments on commit 434b317

Please sign in to comment.