diff options
| author | Marak <[email protected]> | 2015-07-08 20:48:15 -0700 |
|---|---|---|
| committer | Marak <[email protected]> | 2015-07-08 20:48:15 -0700 |
| commit | a118970baa9953a7fcbc9b01f7780046b3840ede (patch) | |
| tree | 3e7c6a4582845b406c36f67830e86d4d28c3b198 | |
| parent | e47ec6d722c3b9562569d82958f025c600b0d26c (diff) | |
| download | faker-a118970baa9953a7fcbc9b01f7780046b3840ede.tar.xz faker-a118970baa9953a7fcbc9b01f7780046b3840ede.zip | |
[fix] [api] Add back commerce methods.
| -rw-r--r-- | lib/commerce.js | 120 | ||||
| -rw-r--r-- | lib/index.js | 3 | ||||
| -rw-r--r-- | test/commerce.unit.js | 165 |
3 files changed, 145 insertions, 143 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 = { diff --git a/test/commerce.unit.js b/test/commerce.unit.js index ce12d3cc..7abcf888 100644 --- a/test/commerce.unit.js +++ b/test/commerce.unit.js @@ -1,6 +1,4 @@ -return; - -if(typeof module !== 'undefined') { +if (typeof module !== 'undefined') { var assert = require('assert'), sinon = require('sinon'), faker = require('../index'); @@ -8,107 +6,108 @@ if(typeof module !== 'undefined') { describe("commerce.js", function() { - describe("color()", function() { - it("returns random value from commerce.color array", function() { - var color = faker.commerce.color(); - assert.ok(faker.definitions.commerce.color.indexOf(color) !== -1); - }); - }); - - describe("department(max, fixedValue)", function() { - it("should use the default amounts when not passing arguments", function() { - var department = faker.commerce.department(); - - assert.ok(department.split(" ").length <= 4); - }); - - it("should return only one value if we specify a maximum of one", function() { - sinon.spy(faker.random, 'array_element'); - - var department = faker.commerce.department(1); + describe("color()", function() { + it("returns random value from commerce.color array", function() { + var color = faker.commerce.color(); + assert.ok(faker.definitions.commerce.color.indexOf(color) !== -1); + }); + }); - assert.strictEqual(department.split(" ").length, 1); - assert.ok(faker.random.array_element.calledOnce); + describe("department(max, fixedValue)", function() { - faker.random.array_element.restore(); - }); + it("should use the default amounts when not passing arguments", function() { + var department = faker.commerce.department(); - it("should return the maxiumum value if we specify the fixed value", function() { - sinon.spy(faker.random, 'array_element'); + assert.ok(department.split(" ").length <= 4); + }); - var department = faker.commerce.department(5, true); + it("should return only one value if we specify a maximum of one", function() { + sinon.spy(faker.random, 'array_element'); - console.log(department); + var department = faker.commerce.department(1); - // account for the separator - assert.strictEqual(department.split(" ").length, 6); - // Sometimes it will generate duplicates that aren't used in the final string, - // so we check if array_element has been called exactly or more than 5 times - assert.ok(faker.random.array_element.callCount >= 5); + assert.strictEqual(department.split(" ").length, 1); + assert.ok(faker.random.array_element.calledOnce); - faker.random.array_element.restore(); - }) + faker.random.array_element.restore(); }); - describe("productName()", function() { - it("returns name comprising of an adjective, material and product", function() { - sinon.spy(faker.random, 'array_element'); - sinon.spy(faker.commerce, 'productAdjective'); - sinon.spy(faker.commerce, 'productMaterial'); - sinon.spy(faker.commerce, 'product'); - var name = faker.commerce.productName(); - - assert.ok(name.split(' ').length >= 3); - assert.ok(faker.random.array_element.calledThrice); - assert.ok(faker.commerce.productAdjective.calledOnce); - assert.ok(faker.commerce.productMaterial.calledOnce); - assert.ok(faker.commerce.product.calledOnce); - - faker.random.array_element.restore(); - faker.commerce.productAdjective.restore(); - faker.commerce.productMaterial.restore(); - faker.commerce.product.restore(); - }); + it("should return the maxiumum value if we specify the fixed value", function() { + sinon.spy(faker.random, 'array_element'); + + var department = faker.commerce.department(5, true); + + console.log(department); + + // account for the separator + assert.strictEqual(department.split(" ").length, 6); + // Sometimes it will generate duplicates that aren't used in the final string, + // so we check if array_element has been called exactly or more than 5 times + assert.ok(faker.random.array_element.callCount >= 5); + + faker.random.array_element.restore(); + }) + }); + + describe("productName()", function() { + it("returns name comprising of an adjective, material and product", function() { + sinon.spy(faker.random, 'array_element'); + sinon.spy(faker.commerce, 'productAdjective'); + sinon.spy(faker.commerce, 'productMaterial'); + sinon.spy(faker.commerce, 'product'); + var name = faker.commerce.productName(); + + assert.ok(name.split(' ').length >= 3); + assert.ok(faker.random.array_element.calledThrice); + assert.ok(faker.commerce.productAdjective.calledOnce); + assert.ok(faker.commerce.productMaterial.calledOnce); + assert.ok(faker.commerce.product.calledOnce); + + faker.random.array_element.restore(); + faker.commerce.productAdjective.restore(); + faker.commerce.productMaterial.restore(); + faker.commerce.product.restore(); + }); + }); + + describe("price(min, max, dec, symbol", function() { + it("should use the default amounts when not passing arguments", function() { + var price = faker.commerce.price(); + + assert.ok(price); + assert.equal((price > 0), true, "the amount should be greater than 0"); + assert.equal((price < 1001), true, "the amount should be less than 1000"); }); - describe("price(min, max, dec, symbol", function() { - it("should use the default amounts when not passing arguments", function() { - var price = faker.commerce.price(); - - assert.ok(price); - assert.equal((price > 0), true, "the amount should be greater than 0"); - assert.equal((price < 1001), true, "the amount should be less than 1000"); - }); - - it("should use the default decimal location when not passing arguments", function() { - var price = faker.commerce.price(); + it("should use the default decimal location when not passing arguments", function() { + var price = faker.commerce.price(); - var decimal = "."; - var expected = price.length - 3; - var actual = price.indexOf(decimal); + var decimal = "."; + var expected = price.length - 3; + var actual = price.indexOf(decimal); - assert.equal(actual, expected, "The expected location of the decimal is " + expected + " but it was " + actual + " amount " + price); - }); + assert.equal(actual, expected, "The expected location of the decimal is " + expected + " but it was " + actual + " amount " + price); + }); - it("should not include a currency symbol by default", function () { + it("should not include a currency symbol by default", function () { - var amount = faker.commerce.price(); + var amount = faker.commerce.price(); - var regexp = new RegExp(/[0-9.]/); + var regexp = new RegExp(/[0-9.]/); - var expected = true; - var actual = regexp.test(amount); + var expected = true; + var actual = regexp.test(amount); - assert.equal(actual, expected, 'The expected match should not include a currency symbol'); - }); + assert.equal(actual, expected, 'The expected match should not include a currency symbol'); + }); - it("it should handle negative amounts, but return 0", function () { + it("it should handle negative amounts, but return 0", function () { - var amount = faker.commerce.price(-200, -1); + var amount = faker.commerce.price(-200, -1); - assert.ok(amount); - assert.equal((amount == 0.00), true, "the amount should equal 0"); - }); + assert.ok(amount); + assert.equal((amount == 0.00), true, "the amount should equal 0"); }); + }); });
\ No newline at end of file |
