commerce.js

  1. /**
  2. *
  3. * @namespace faker.commerce
  4. */
  5. var Commerce = function (faker) {
  6. var self = this;
  7. /**
  8. * color
  9. *
  10. * @method faker.commerce.color
  11. */
  12. self.color = function() {
  13. return faker.random.arrayElement(faker.definitions.commerce.color);
  14. };
  15. /**
  16. * department
  17. *
  18. * @method faker.commerce.department
  19. */
  20. self.department = function() {
  21. return faker.random.arrayElement(faker.definitions.commerce.department);
  22. };
  23. /**
  24. * productName
  25. *
  26. * @method faker.commerce.productName
  27. */
  28. self.productName = function() {
  29. return faker.commerce.productAdjective() + " " +
  30. faker.commerce.productMaterial() + " " +
  31. faker.commerce.product();
  32. };
  33. /**
  34. * price
  35. *
  36. * @method faker.commerce.price
  37. * @param {number} min
  38. * @param {number} max
  39. * @param {number} dec
  40. * @param {string} symbol
  41. *
  42. * @return {string}
  43. */
  44. self.price = function(min, max, dec, symbol) {
  45. min = min || 1;
  46. max = max || 1000;
  47. dec = dec === undefined ? 2 : dec;
  48. symbol = symbol || '';
  49. if (min < 0 || max < 0) {
  50. return symbol + 0.00;
  51. }
  52. var randValue = faker.random.number({ max: max, min: min });
  53. return symbol + (Math.round(randValue * Math.pow(10, dec)) / Math.pow(10, dec)).toFixed(dec);
  54. };
  55. /*
  56. self.categories = function(num) {
  57. var categories = [];
  58. do {
  59. var category = faker.random.arrayElement(faker.definitions.commerce.department);
  60. if(categories.indexOf(category) === -1) {
  61. categories.push(category);
  62. }
  63. } while(categories.length < num);
  64. return categories;
  65. };
  66. */
  67. /*
  68. self.mergeCategories = function(categories) {
  69. var separator = faker.definitions.separator || " &";
  70. // TODO: find undefined here
  71. categories = categories || faker.definitions.commerce.categories;
  72. var commaSeparated = categories.slice(0, -1).join(', ');
  73. return [commaSeparated, categories[categories.length - 1]].join(separator + " ");
  74. };
  75. */
  76. /**
  77. * productAdjective
  78. *
  79. * @method faker.commerce.productAdjective
  80. */
  81. self.productAdjective = function() {
  82. return faker.random.arrayElement(faker.definitions.commerce.product_name.adjective);
  83. };
  84. /**
  85. * productMaterial
  86. *
  87. * @method faker.commerce.productMaterial
  88. */
  89. self.productMaterial = function() {
  90. return faker.random.arrayElement(faker.definitions.commerce.product_name.material);
  91. };
  92. /**
  93. * product
  94. *
  95. * @method faker.commerce.product
  96. */
  97. self.product = function() {
  98. return faker.random.arrayElement(faker.definitions.commerce.product_name.product);
  99. };
  100. /**
  101. * productDescription
  102. *
  103. * @method faker.commerce.productDescription
  104. */
  105. self.productDescription = function() {
  106. return faker.random.arrayElement(faker.definitions.commerce.product_description);
  107. };
  108. return self;
  109. };
  110. module['exports'] = Commerce;