aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMarak <[email protected]>2015-07-08 20:48:15 -0700
committerMarak <[email protected]>2015-07-08 20:48:15 -0700
commita118970baa9953a7fcbc9b01f7780046b3840ede (patch)
tree3e7c6a4582845b406c36f67830e86d4d28c3b198 /lib
parente47ec6d722c3b9562569d82958f025c600b0d26c (diff)
downloadfaker-a118970baa9953a7fcbc9b01f7780046b3840ede.tar.xz
faker-a118970baa9953a7fcbc9b01f7780046b3840ede.zip
[fix] [api] Add back commerce methods.
Diffstat (limited to 'lib')
-rw-r--r--lib/commerce.js120
-rw-r--r--lib/index.js3
2 files changed, 63 insertions, 60 deletions
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
diff --git a/lib/index.js b/lib/index.js
index 281befed..23ec1dee 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -76,6 +76,9 @@ function Faker (opts) {
var _Date = require('./date');
self.date = new _Date(self);
+ var Commerce = require('./commerce');
+ self.commerce = new Commerce(self);
+
// TODO: fix self.commerce = require('./commerce');
var _definitions = {