diff options
| author | rob.scott <[email protected]> | 2015-03-10 13:57:02 +0000 |
|---|---|---|
| committer | rob.scott <[email protected]> | 2015-03-10 13:57:02 +0000 |
| commit | 3418839d8b818485331ef7931dea1de26c751da2 (patch) | |
| tree | 7dedc0b4a35e0d1d22b3b3c22210d10b31a5b7a8 /lib/commerce.js | |
| parent | a39082f6f49fb326111bc2a0d9014c9dc65fe1fa (diff) | |
| download | faker-3418839d8b818485331ef7931dea1de26c751da2.tar.xz faker-3418839d8b818485331ef7931dea1de26c751da2.zip | |
Add Commerce functions from https://github.com/stympy/faker into javascript
Diffstat (limited to 'lib/commerce.js')
| -rw-r--r-- | lib/commerce.js | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/commerce.js b/lib/commerce.js new file mode 100644 index 00000000..20b736bc --- /dev/null +++ b/lib/commerce.js @@ -0,0 +1,81 @@ +var faker = require('../index'); + +var commerce = { + + color: function() { + return faker.random.array_element(faker.definitions.commerce.color); + }, + + department: function(max, fixedAmount) { + max = max || 3; + + var num = Math.floor((Math.random() * max) + 1); + if(fixedAmount) { + num = max; + } + + var categories = faker.commerce.categories(num); + + console.log("NUMBER: " + num); + + if(num > 1) { + return faker.commerce.mergeCategories(categories); + } + + return categories[0]; + }, + + productName: function() { + return faker.commerce.productAdjective() + " " + + faker.commerce.productMaterial() + " " + + faker.commerce.product(); + }, + + price: function(min, max, dec, symbol) { + min = min || 0; + max = max || 1000; + dec = dec || 2; + symbol = symbol || ''; + + if(min < 0 || max < 0) { + return symbol + 0.00; + } + + return symbol + (Math.round((Math.random() * (max - min) + min) * Math.pow(10, dec)) / Math.pow(10, dec)).toFixed(dec); + }, + + categories: function(num) { + var categories = []; + + do { + var category = faker.random.array_element(faker.definitions.commerce.department); + if(categories.indexOf(category) === -1) { + categories.push(category); + } + } while(categories.length < num); + + return categories; + }, + + mergeCategories: function(categories) { + var separator = faker.definitions.separator; + var commaSeparated = categories.slice(0, -1).join(', '); + + return [commaSeparated, categories[categories.length - 1]].join(separator); + }, + + productAdjective: function() { + return faker.random.array_element(faker.definitions.commerce.product_name.adjective); + }, + + productMaterial: function() { + return faker.random.array_element(faker.definitions.commerce.product_name.material); + }, + + product: function() { + return faker.random.array_element(faker.definitions.commerce.product_name.product); + } + +}; + +module.exports = commerce;
\ No newline at end of file |
