From 3418839d8b818485331ef7931dea1de26c751da2 Mon Sep 17 00:00:00 2001 From: "rob.scott" Date: Tue, 10 Mar 2015 13:57:02 +0000 Subject: Add Commerce functions from https://github.com/stympy/faker into javascript --- lib/commerce.js | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 lib/commerce.js (limited to 'lib/commerce.js') 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 -- cgit v1.2.3 From 75caacff0bacf3cfbe180c58ff980f965ac8a975 Mon Sep 17 00:00:00 2001 From: "rob.scott" Date: Thu, 12 Mar 2015 10:22:10 +0000 Subject: removes a console.log when generating a price --- lib/commerce.js | 2 -- 1 file changed, 2 deletions(-) (limited to 'lib/commerce.js') diff --git a/lib/commerce.js b/lib/commerce.js index 20b736bc..76ec4beb 100644 --- a/lib/commerce.js +++ b/lib/commerce.js @@ -16,8 +16,6 @@ var commerce = { var categories = faker.commerce.categories(num); - console.log("NUMBER: " + num); - if(num > 1) { return faker.commerce.mergeCategories(categories); } -- cgit v1.2.3 From eaf5c65cfae0a6636555884c18e6c955fae7887e Mon Sep 17 00:00:00 2001 From: Marak Date: Tue, 7 Jul 2015 16:22:50 -0700 Subject: [refactor] [dist] Allow for node to require individual locales ( to avoid the default behavior of requiring all locale data. #125 #167 --- lib/commerce.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'lib/commerce.js') diff --git a/lib/commerce.js b/lib/commerce.js index 76ec4beb..8cdcf7d5 100644 --- a/lib/commerce.js +++ b/lib/commerce.js @@ -1,4 +1,4 @@ -var faker = require('../index'); +var faker = require('./index'); var commerce = { @@ -56,7 +56,9 @@ var commerce = { }, mergeCategories: function(categories) { - var separator = faker.definitions.separator; + var separator = faker.definitions.separator || " &"; + // TODO: find undefined here + categories = categories || faker.definitions.commerce.categories; var commaSeparated = categories.slice(0, -1).join(', '); return [commaSeparated, categories[categories.length - 1]].join(separator); -- cgit v1.2.3 From a118970baa9953a7fcbc9b01f7780046b3840ede Mon Sep 17 00:00:00 2001 From: Marak Date: Wed, 8 Jul 2015 20:48:15 -0700 Subject: [fix] [api] Add back commerce methods. --- lib/commerce.js | 120 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 60 insertions(+), 60 deletions(-) (limited to 'lib/commerce.js') diff --git a/lib/commerce.js b/lib/commerce.js index 8cdcf7d5..755bf828 100644 --- a/lib/commerce.js +++ b/lib/commerce.js @@ -1,81 +1,81 @@ -var faker = require('./index'); +var Commerce = function (faker) { + var self = this; -var commerce = { + self.color = function() { + return faker.random.array_element(faker.definitions.commerce.color); + }; - color: function() { - return faker.random.array_element(faker.definitions.commerce.color); - }, + self.department = function(max, fixedAmount) { + max = max || 3; - department: function(max, fixedAmount) { - max = max || 3; + var num = Math.floor((Math.random() * max) + 1); + if (fixedAmount) { + num = max; + } - var num = Math.floor((Math.random() * max) + 1); - if(fixedAmount) { - num = max; - } + var categories = faker.commerce.categories(num); - var categories = faker.commerce.categories(num); + if(num > 1) { + return faker.commerce.mergeCategories(categories); + } - if(num > 1) { - return faker.commerce.mergeCategories(categories); - } + return categories[0]; + }; - return categories[0]; - }, + self.productName = function() { + return faker.commerce.productAdjective() + " " + + faker.commerce.productMaterial() + " " + + faker.commerce.product(); + }; - productName: function() { - return faker.commerce.productAdjective() + " " + - faker.commerce.productMaterial() + " " + - faker.commerce.product(); - }, + self.price = function(min, max, dec, symbol) { + min = min || 0; + max = max || 1000; + dec = dec || 2; + symbol = symbol || ''; - 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; + } - 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); + }; - return symbol + (Math.round((Math.random() * (max - min) + min) * Math.pow(10, dec)) / Math.pow(10, dec)).toFixed(dec); - }, + self.categories = function(num) { + var categories = []; - 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); - 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; + }; - return categories; - }, + self.mergeCategories = function(categories) { + var separator = faker.definitions.separator || " &"; + // TODO: find undefined here + categories = categories || faker.definitions.commerce.categories; + var commaSeparated = categories.slice(0, -1).join(', '); - mergeCategories: function(categories) { - var separator = faker.definitions.separator || " &"; - // TODO: find undefined here - categories = categories || faker.definitions.commerce.categories; - var commaSeparated = categories.slice(0, -1).join(', '); + return [commaSeparated, categories[categories.length - 1]].join(separator + " "); + }; - return [commaSeparated, categories[categories.length - 1]].join(separator); - }, + self.productAdjective = function() { + return faker.random.array_element(faker.definitions.commerce.product_name.adjective); + }; - productAdjective: function() { - return faker.random.array_element(faker.definitions.commerce.product_name.adjective); - }, + self.productMaterial = function() { + return faker.random.array_element(faker.definitions.commerce.product_name.material); + }; - 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); - } + self.product = function() { + return faker.random.array_element(faker.definitions.commerce.product_name.product); + } + return self; }; -module.exports = commerce; \ No newline at end of file +module['exports'] = Commerce; \ No newline at end of file -- cgit v1.2.3 From edb7d482a40e98ce25cd6ba296818be14f291aa1 Mon Sep 17 00:00:00 2001 From: Marak Date: Wed, 8 Jul 2015 21:18:48 -0700 Subject: [fix] Comment out a few methods sections of commerce section. Needs some work. #183 --- lib/commerce.js | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'lib/commerce.js') diff --git a/lib/commerce.js b/lib/commerce.js index 755bf828..276f1698 100644 --- a/lib/commerce.js +++ b/lib/commerce.js @@ -6,6 +6,9 @@ var Commerce = function (faker) { }; self.department = function(max, fixedAmount) { + + return faker.random.array_element(faker.definitions.commerce.department); + /* max = max || 3; var num = Math.floor((Math.random() * max) + 1); @@ -20,6 +23,7 @@ var Commerce = function (faker) { } return categories[0]; + */ }; self.productName = function() { @@ -41,6 +45,7 @@ var Commerce = function (faker) { return symbol + (Math.round((Math.random() * (max - min) + min) * Math.pow(10, dec)) / Math.pow(10, dec)).toFixed(dec); }; + /* self.categories = function(num) { var categories = []; @@ -54,6 +59,8 @@ var Commerce = function (faker) { return categories; }; + */ + /* self.mergeCategories = function(categories) { var separator = faker.definitions.separator || " &"; // TODO: find undefined here @@ -62,6 +69,7 @@ var Commerce = function (faker) { return [commaSeparated, categories[categories.length - 1]].join(separator + " "); }; + */ self.productAdjective = function() { return faker.random.array_element(faker.definitions.commerce.product_name.adjective); -- cgit v1.2.3 From 4c1b454b8e3c8d87f2569a2ce0d7730dc31ee821 Mon Sep 17 00:00:00 2001 From: Marak Date: Wed, 8 Jul 2015 23:56:22 -0700 Subject: [api] [refactor] Rename `array_element` and `object_element` to camelCase. Set default max random number to 99999. Added default arguments to some methods. --- lib/commerce.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'lib/commerce.js') diff --git a/lib/commerce.js b/lib/commerce.js index 276f1698..ec91edb1 100644 --- a/lib/commerce.js +++ b/lib/commerce.js @@ -2,12 +2,12 @@ var Commerce = function (faker) { var self = this; self.color = function() { - return faker.random.array_element(faker.definitions.commerce.color); + return faker.random.arrayElement(faker.definitions.commerce.color); }; self.department = function(max, fixedAmount) { - return faker.random.array_element(faker.definitions.commerce.department); + return faker.random.arrayElement(faker.definitions.commerce.department); /* max = max || 3; @@ -50,7 +50,7 @@ var Commerce = function (faker) { var categories = []; do { - var category = faker.random.array_element(faker.definitions.commerce.department); + var category = faker.random.arrayElement(faker.definitions.commerce.department); if(categories.indexOf(category) === -1) { categories.push(category); } @@ -72,15 +72,15 @@ var Commerce = function (faker) { */ self.productAdjective = function() { - return faker.random.array_element(faker.definitions.commerce.product_name.adjective); + return faker.random.arrayElement(faker.definitions.commerce.product_name.adjective); }; self.productMaterial = function() { - return faker.random.array_element(faker.definitions.commerce.product_name.material); + return faker.random.arrayElement(faker.definitions.commerce.product_name.material); }; self.product = function() { - return faker.random.array_element(faker.definitions.commerce.product_name.product); + return faker.random.arrayElement(faker.definitions.commerce.product_name.product); } return self; -- cgit v1.2.3