diff options
| author | Marak <[email protected]> | 2015-07-06 09:16:55 -0700 |
|---|---|---|
| committer | Marak <[email protected]> | 2015-07-06 09:16:55 -0700 |
| commit | e1866cf2b1cd72b9e267e1258e197798642c73a8 (patch) | |
| tree | b60c75fe73bc63f5b06bb6ec7b4563bff77de29e | |
| parent | 760a2c5e92011cc60286e55447e6cc168c1b86c6 (diff) | |
| parent | 4f5cb5cddad356abad010b746dede809263b95c7 (diff) | |
| download | faker-e1866cf2b1cd72b9e267e1258e197798642c73a8.tar.xz faker-e1866cf2b1cd72b9e267e1258e197798642c73a8.zip | |
[merge] Commerce implementation
| -rw-r--r-- | index.js | 14 | ||||
| -rw-r--r-- | lib/commerce.js | 79 | ||||
| -rw-r--r-- | lib/internet.js | 2 | ||||
| -rw-r--r-- | lib/locales/en.js | 32 | ||||
| -rw-r--r-- | package.json | 3 | ||||
| -rw-r--r-- | test/all.functional.js | 4 | ||||
| -rw-r--r-- | test/commerce.unit.js | 112 |
7 files changed, 238 insertions, 8 deletions
@@ -30,6 +30,7 @@ exports.helpers = require('./lib/helpers'); exports.date = require('./lib/date'); exports.finance = require('./lib/finance'); exports.hacker = require('./lib/hacker'); +exports.commerce = require('./lib/commerce'); var locales = exports.locales = require('./lib/locales'); @@ -41,6 +42,8 @@ exports.localeFallback = "en"; exports.definitions = {}; +var _stringDefinitions = ["title", "separator"]; + var _definitions = { "name": ["first_name", "last_name", "prefix", "suffix", "title", "male_first_name", "male_last_name", "female_first_name", "female_last_name"], "address": ["city_prefix", "city_suffix", "street_suffix", "county", "country", "country_code", "state", "state_abbr", "street_prefix"], @@ -49,9 +52,18 @@ var _definitions = { "hacker": ["abbreviation", "adjective", "noun", "verb", "ingverb"], "phone_number": ["formats"], "finance": ["account_type", "transaction_type", "currency"], - "internet": ["avatar_uri", "domain_suffix", "free_email", "password"] + "internet": ["avatar_uri", "domain_suffix", "free_email", "password"], + "commerce": ["color", "department", "product_name", "price"] }; +_stringDefinitions.forEach(function(d) { + if (typeof locales[exports.locale][d] === "undefined") { + exports.definitions[d] = locales[exports.localeFallback][d]; + } else { + exports.definitions[d] = locales[exports.locale][d]; + } +}); + // Create a Getter for all definitions.foo.bar propetries Object.keys(_definitions).forEach(function(d){ if (typeof exports.definitions[d] === "undefined") { diff --git a/lib/commerce.js b/lib/commerce.js new file mode 100644 index 00000000..76ec4beb --- /dev/null +++ b/lib/commerce.js @@ -0,0 +1,79 @@ +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); + + 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 diff --git a/lib/internet.js b/lib/internet.js index 89be21f4..d70088d4 100644 --- a/lib/internet.js +++ b/lib/internet.js @@ -51,7 +51,7 @@ var internet = { }, domainWord: function () { - return faker.name.firstName().replace(/([^A-Z0-9._%+-])/ig, '').toLowerCase(); + return faker.name.firstName().replace(/([\\~#&*{}/:<>?|\"])/ig, '').toLowerCase(); }, ip: function () { diff --git a/lib/locales/en.js b/lib/locales/en.js index d79cb828..ef0123a1 100644 --- a/lib/locales/en.js +++ b/lib/locales/en.js @@ -7723,7 +7723,14 @@ en.commerce = { "Fantastic", "Practical", "Sleek", - "Awesome" + "Awesome", + "Generic", + "Handcrafted", + "Handmade", + "Licensed", + "Refined", + "Unbranded", + "Tasty" ], "material": [ "Steel", @@ -7732,18 +7739,37 @@ en.commerce = { "Plastic", "Cotton", "Granite", - "Rubber" + "Rubber", + "Metal", + "Soft", + "Fresh", + "Frozen" ], "product": [ "Chair", "Car", "Computer", + "Keyboard", + "Mouse", + "Bike", + "Ball", "Gloves", "Pants", "Shirt", "Table", "Shoes", - "Hat" + "Hat", + "Towels", + "Soap", + "Tuna", + "Chicken", + "Fish", + "Cheese", + "Bacon", + "Pizza", + "Salad", + "Sausages", + "Chips" ] } }; diff --git a/package.json b/package.json index 061c1d73..22274e24 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,7 @@ "version": "2.1.5", "contributors": [ "Marak Squires <[email protected]>", - "Matthew Bergman <[email protected]>", - "Tomasz Ducin <[email protected]>" + "Matthew Bergman <[email protected]>" ], "repository": { "type": "git", diff --git a/test/all.functional.js b/test/all.functional.js index 0aefec5f..72ca2eeb 100644 --- a/test/all.functional.js +++ b/test/all.functional.js @@ -22,7 +22,9 @@ var modules = { phone: ['phoneNumber'], - finance: ['account', 'accountName', 'mask', 'amount', 'transactionType', 'currencyCode', 'currencyName', 'currencySymbol'] + finance: ['account', 'accountName', 'mask', 'amount', 'transactionType', 'currencyCode', 'currencyName', 'currencySymbol'], + + commerce: ['color', 'department', 'productName', 'price'] }; describe("functional tests", function () { diff --git a/test/commerce.unit.js b/test/commerce.unit.js new file mode 100644 index 00000000..856c3be6 --- /dev/null +++ b/test/commerce.unit.js @@ -0,0 +1,112 @@ +if(typeof module !== 'undefined') { + var assert = require('assert'), + sinon = require('sinon'), + faker = require('../index'); +} + +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); + + assert.strictEqual(department.split(" ").length, 1); + assert.ok(faker.random.array_element.calledOnce); + + faker.random.array_element.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"); + }); + + 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); + + 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 () { + + var amount = faker.commerce.price(); + + var regexp = new RegExp(/[0-9.]/); + + var expected = true; + var actual = regexp.test(amount); + + assert.equal(actual, expected, 'The expected match should not include a currency symbol'); + }); + + it("it should handle negative amounts, but return 0", function () { + + var amount = faker.commerce.price(-200, -1); + + assert.ok(amount); + assert.equal((amount == 0.00), true, "the amount should equal 0"); + }); + }); + +});
\ No newline at end of file |
