From 4179b203e380811272ef7e75ce11d62458cd7e75 Mon Sep 17 00:00:00 2001 From: Tyler Date: Sat, 21 Oct 2017 14:21:58 -0700 Subject: add ordinal and cardinal direction generator to address namespace --- lib/address.js | 27 ++++++++++++++++++++++++--- lib/locales/en/address/direction.js | 6 ++++++ lib/locales/en/address/index.js | 1 + 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 lib/locales/en/address/direction.js (limited to 'lib') diff --git a/lib/address.js b/lib/address.js index 0a6f0c5b..59328c46 100644 --- a/lib/address.js +++ b/lib/address.js @@ -32,7 +32,7 @@ function Address (faker) { * order to build the city name. * * If no format string is provided one of the following is randomly used: - * + * * * `{{address.cityPrefix}} {{name.firstName}}{{address.citySuffix}}` * * `{{address.cityPrefix}} {{name.firstName}}` * * `{{name.firstName}}{{address.citySuffix}}` @@ -131,7 +131,7 @@ function Address (faker) { this.streetSuffix = function () { return faker.random.arrayElement(faker.definitions.address.street_suffix); } - + /** * streetPrefix * @@ -226,7 +226,28 @@ function Address (faker) { min = min || -180 return faker.random.number({max: max, min:min, precision:0.0001}).toFixed(4); } - + + /** + * cardinal direction + * + * @method faker.address.cardDirection + */ + this.cardDirection = function () { + return faker.random.arrayElement(faker.definitions.address.direction); + } + + /** + * ordinal direction + * + * @method faker.address.ordDirection + */ + this.ordDirection = function () { + return ( + faker.random.arrayElement(faker.definitions.address.direction.slice(0, 2)) + + faker.random.arrayElement(faker.definitions.address.direction.slice(2, 4)) + ); + } + return this; } diff --git a/lib/locales/en/address/direction.js b/lib/locales/en/address/direction.js new file mode 100644 index 00000000..f5ac9654 --- /dev/null +++ b/lib/locales/en/address/direction.js @@ -0,0 +1,6 @@ +module["exports"] = [ + "N", + "S", + "E", + "W" +]; diff --git a/lib/locales/en/address/index.js b/lib/locales/en/address/index.js index 575f1d46..3a127b4d 100644 --- a/lib/locales/en/address/index.js +++ b/lib/locales/en/address/index.js @@ -17,3 +17,4 @@ address.city = require("./city"); address.street_name = require("./street_name"); address.street_address = require("./street_address"); address.default_country = require("./default_country"); +address.direction = require("./direction"); -- cgit v1.2.3 From 9239055a242ea71d78f7f7c67014b1436c040862 Mon Sep 17 00:00:00 2001 From: Tyler Date: Sat, 21 Oct 2017 14:48:03 -0700 Subject: add unit tests and schema desciptions --- lib/address.js | 19 ++++++++++++++----- lib/index.js | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/address.js b/lib/address.js index 59328c46..cb74da39 100644 --- a/lib/address.js +++ b/lib/address.js @@ -230,26 +230,35 @@ function Address (faker) { /** * cardinal direction * - * @method faker.address.cardDirection + * @method faker.address.cardinalDirection */ - this.cardDirection = function () { + this.cardinalDirection = function () { return faker.random.arrayElement(faker.definitions.address.direction); } + this.cardinalDirection.schema = { + "description": "Generates a cardinal direction.", + "sampleResults": ["N", "S", "E", "W"] + }; + /** * ordinal direction * - * @method faker.address.ordDirection + * @method faker.address.ordinalDirection */ - this.ordDirection = function () { + this.ordinalDirection = function () { return ( faker.random.arrayElement(faker.definitions.address.direction.slice(0, 2)) + faker.random.arrayElement(faker.definitions.address.direction.slice(2, 4)) ); } + this.ordinalDirection.schema = { + "description": "Generates an ordinal direction.", + "sampleResults": ["NW", "SE", "SW", "NE"] + }; + return this; } - module.exports = Address; diff --git a/lib/index.js b/lib/index.js index 58f0012e..08f45b78 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"], + "address": ["city_prefix", "city_suffix", "street_suffix", "county", "country", "country_code", "state", "state_abbr", "street_prefix", "postcode", "direction"], "company": ["adjective", "noun", "descriptor", "bs_adjective", "bs_noun", "bs_verb", "suffix"], "lorem": ["words"], "hacker": ["abbreviation", "adjective", "noun", "verb", "ingverb", "phrase"], -- cgit v1.2.3 From 900c3f1a80e87081d204d00e573c0b465c232932 Mon Sep 17 00:00:00 2001 From: Tyler Date: Sat, 21 Oct 2017 18:12:23 -0700 Subject: add direction generator and test --- lib/address.js | 21 ++++++++++++++++++--- lib/locales/en/address/direction.js | 8 ++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/address.js b/lib/address.js index cb74da39..57531e05 100644 --- a/lib/address.js +++ b/lib/address.js @@ -227,13 +227,29 @@ function Address (faker) { return faker.random.number({max: max, min:min, precision:0.0001}).toFixed(4); } + /** + * direction + * + * @method faker.address.direction + */ + this.direction = function () { + return faker.random.arrayElement(faker.definitions.address.direction) + } + + this.direction.schema = { + "description": "Generates a direction.", + "sampleResults": ["NW", "S","SW", "E"] + }; + /** * cardinal direction * * @method faker.address.cardinalDirection */ this.cardinalDirection = function () { - return faker.random.arrayElement(faker.definitions.address.direction); + return ( + faker.random.arrayElement(faker.definitions.address.direction.slice(0, 4)) + ); } this.cardinalDirection.schema = { @@ -248,8 +264,7 @@ function Address (faker) { */ this.ordinalDirection = function () { return ( - faker.random.arrayElement(faker.definitions.address.direction.slice(0, 2)) + - faker.random.arrayElement(faker.definitions.address.direction.slice(2, 4)) + faker.random.arrayElement(faker.definitions.address.direction.slice(4, 8)) ); } diff --git a/lib/locales/en/address/direction.js b/lib/locales/en/address/direction.js index f5ac9654..775ab0ca 100644 --- a/lib/locales/en/address/direction.js +++ b/lib/locales/en/address/direction.js @@ -1,6 +1,10 @@ module["exports"] = [ "N", - "S", "E", - "W" + "S", + "W", + "NE", + "NW", + "SE", + "SW" ]; -- cgit v1.2.3 From 07929b4c54ea386e5fab198b87b8a05493676c29 Mon Sep 17 00:00:00 2001 From: Tyler Date: Sat, 21 Oct 2017 18:17:18 -0700 Subject: add missing colon --- lib/address.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/address.js b/lib/address.js index 57531e05..5d98554e 100644 --- a/lib/address.js +++ b/lib/address.js @@ -233,12 +233,12 @@ function Address (faker) { * @method faker.address.direction */ this.direction = function () { - return faker.random.arrayElement(faker.definitions.address.direction) + return faker.random.arrayElement(faker.definitions.address.direction); } this.direction.schema = { "description": "Generates a direction.", - "sampleResults": ["NW", "S","SW", "E"] + "sampleResults": ["NW", "S", "SW", "E"] }; /** -- cgit v1.2.3 From d92b012aefd60fdc919d03a147dc0f28ababf0cd Mon Sep 17 00:00:00 2001 From: Tyler Date: Sun, 22 Oct 2017 11:27:30 -0700 Subject: 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 + 5 files changed, 48 insertions(+), 21 deletions(-) create mode 100644 lib/locales/en/address/direction_abbr.js (limited to 'lib') diff --git a/lib/address.js b/lib/address.js index 5d98554e..21e14ff6 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 08f45b78..cd1a6cda 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 775ab0ca..3d7ff312 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 00000000..775ab0ca --- /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 3a127b4d..05b869c5 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"); -- cgit v1.2.3