From d92b012aefd60fdc919d03a147dc0f28ababf0cd Mon Sep 17 00:00:00 2001 From: Tyler Date: Sun, 22 Oct 2017 11:27:30 -0700 Subject: [PATCH] add direction abbr options with tests --- lib/address.js | 40 +++++++++++++++++------- lib/index.js | 2 +- lib/locales/en/address/direction.js | 16 +++++----- lib/locales/en/address/direction_abbr.js | 10 ++++++ lib/locales/en/address/index.js | 1 + test/address.unit.js | 30 ++++++++++++++++-- 6 files changed, 75 insertions(+), 24 deletions(-) create mode 100644 lib/locales/en/address/direction_abbr.js diff --git a/lib/address.js b/lib/address.js index 5d98554ed..21e14ff65 100644 --- a/lib/address.js +++ b/lib/address.js @@ -231,46 +231,62 @@ function Address (faker) { * direction * * @method faker.address.direction + * @param {Boolean} useAbbr return direction abbreviation. defaults to false */ - this.direction = function () { - return faker.random.arrayElement(faker.definitions.address.direction); + this.direction = function (useAbbr) { + if (typeof useAbbr === 'undefined' || useAbbr === false) { + return faker.random.arrayElement(faker.definitions.address.direction); + } + return faker.random.arrayElement(faker.definitions.address.direction_abbr); } this.direction.schema = { - "description": "Generates a direction.", - "sampleResults": ["NW", "S", "SW", "E"] + "description": "Generates a direction. Use optional useAbbr bool to return abbrevation", + "sampleResults": ["Northwest", "South", "SW", "E"] }; /** * cardinal direction * * @method faker.address.cardinalDirection + * @param {Boolean} useAbbr return direction abbreviation. defaults to false */ - this.cardinalDirection = function () { + this.cardinalDirection = function (useAbbr) { + if (typeof useAbbr === 'undefined' || useAbbr === false) { + return ( + faker.random.arrayElement(faker.definitions.address.direction.slice(0, 4)) + ); + } return ( - faker.random.arrayElement(faker.definitions.address.direction.slice(0, 4)) + faker.random.arrayElement(faker.definitions.address.direction_abbr.slice(0, 4)) ); } this.cardinalDirection.schema = { - "description": "Generates a cardinal direction.", - "sampleResults": ["N", "S", "E", "W"] + "description": "Generates a cardinal direction. Use optional useAbbr boolean to return abbrevation", + "sampleResults": ["North", "South", "E", "W"] }; /** * ordinal direction * * @method faker.address.ordinalDirection + * @param {Boolean} useAbbr return direction abbreviation. defaults to false */ - this.ordinalDirection = function () { + this.ordinalDirection = function (useAbbr) { + if (typeof useAbbr === 'undefined' || useAbbr === false) { + return ( + faker.random.arrayElement(faker.definitions.address.direction.slice(4, 8)) + ); + } return ( - faker.random.arrayElement(faker.definitions.address.direction.slice(4, 8)) + faker.random.arrayElement(faker.definitions.address.direction_abbr.slice(4, 8)) ); } this.ordinalDirection.schema = { - "description": "Generates an ordinal direction.", - "sampleResults": ["NW", "SE", "SW", "NE"] + "description": "Generates an ordinal direction. Use optional useAbbr boolean to return abbrevation", + "sampleResults": ["Northwest", "Southeast", "SW", "NE"] }; return this; diff --git a/lib/index.js b/lib/index.js index 08f45b781..cd1a6cda9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -101,7 +101,7 @@ function Faker (opts) { var _definitions = { "name": ["first_name", "last_name", "prefix", "suffix", "gender", "title", "male_first_name", "female_first_name", "male_middle_name", "female_middle_name", "male_last_name", "female_last_name"], - "address": ["city_prefix", "city_suffix", "street_suffix", "county", "country", "country_code", "state", "state_abbr", "street_prefix", "postcode", "direction"], + "address": ["city_prefix", "city_suffix", "street_suffix", "county", "country", "country_code", "state", "state_abbr", "street_prefix", "postcode", "direction", "direction_abbr"], "company": ["adjective", "noun", "descriptor", "bs_adjective", "bs_noun", "bs_verb", "suffix"], "lorem": ["words"], "hacker": ["abbreviation", "adjective", "noun", "verb", "ingverb", "phrase"], diff --git a/lib/locales/en/address/direction.js b/lib/locales/en/address/direction.js index 775ab0ca1..3d7ff3125 100644 --- a/lib/locales/en/address/direction.js +++ b/lib/locales/en/address/direction.js @@ -1,10 +1,10 @@ module["exports"] = [ - "N", - "E", - "S", - "W", - "NE", - "NW", - "SE", - "SW" + "North", + "East", + "South", + "West", + "Northeast", + "Northwest", + "Southeast", + "Southwest" ]; diff --git a/lib/locales/en/address/direction_abbr.js b/lib/locales/en/address/direction_abbr.js new file mode 100644 index 000000000..775ab0ca1 --- /dev/null +++ b/lib/locales/en/address/direction_abbr.js @@ -0,0 +1,10 @@ +module["exports"] = [ + "N", + "E", + "S", + "W", + "NE", + "NW", + "SE", + "SW" +]; diff --git a/lib/locales/en/address/index.js b/lib/locales/en/address/index.js index 3a127b4d8..05b869c51 100644 --- a/lib/locales/en/address/index.js +++ b/lib/locales/en/address/index.js @@ -18,3 +18,4 @@ address.street_name = require("./street_name"); address.street_address = require("./street_address"); address.default_country = require("./default_country"); address.direction = require("./direction"); +address.direction_abbr = require("./direction_abbr"); diff --git a/test/address.unit.js b/test/address.unit.js index ff3bc6e58..287758501 100644 --- a/test/address.unit.js +++ b/test/address.unit.js @@ -310,9 +310,17 @@ describe("address.js", function () { describe("direction()", function () { it("returns random direction", function () { - sinon.stub(faker.address, 'direction').returns('N'); + sinon.stub(faker.address, 'direction').returns('North'); var direction = faker.address.direction(); + assert.equal(direction, 'North'); + faker.address.direction.restore(); + }) + + it("returns abbreviation when useAbbr is true", function () { + sinon.stub(faker.address, 'direction').returns('N'); + var direction = faker.address.direction(true); + assert.equal(direction, 'N'); faker.address.direction.restore(); }) @@ -320,9 +328,17 @@ describe("address.js", function () { describe("ordinalDirection()", function () { it("returns random ordinal direction", function () { - sinon.stub(faker.address, 'ordinalDirection').returns('W'); + sinon.stub(faker.address, 'ordinalDirection').returns('West'); var ordinalDirection = faker.address.ordinalDirection(); + assert.equal(ordinalDirection, 'West'); + faker.address.ordinalDirection.restore(); + }) + + it("returns abbreviation when useAbbr is true", function () { + sinon.stub(faker.address, 'ordinalDirection').returns('W'); + var ordinalDirection = faker.address.ordinalDirection(true); + assert.equal(ordinalDirection, 'W'); faker.address.ordinalDirection.restore(); }) @@ -330,9 +346,17 @@ describe("address.js", function () { describe("cardinalDirection()", function () { it("returns random cardinal direction", function () { - sinon.stub(faker.address, 'cardinalDirection').returns('NW'); + sinon.stub(faker.address, 'cardinalDirection').returns('Northwest'); var cardinalDirection = faker.address.cardinalDirection(); + assert.equal(cardinalDirection, 'Northwest'); + faker.address.cardinalDirection.restore(); + }) + + it("returns abbreviation when useAbbr is true", function () { + sinon.stub(faker.address, 'cardinalDirection').returns('NW'); + var cardinalDirection = faker.address.cardinalDirection(true); + assert.equal(cardinalDirection, 'NW'); faker.address.cardinalDirection.restore(); })