aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarak <[email protected]>2017-10-22 14:40:58 -0400
committerGitHub <[email protected]>2017-10-22 14:40:58 -0400
commitce92b3aebb0f87e07e5cff03a3b3d8ca56cf5a76 (patch)
tree17c39ebd80f9d3e412e9a63678724ac04d85d1d0
parentb78fdff29d5c6c3318948c2c44c7903eed55ce4c (diff)
parentd92b012aefd60fdc919d03a147dc0f28ababf0cd (diff)
downloadfaker-ce92b3aebb0f87e07e5cff03a3b3d8ca56cf5a76.tar.xz
faker-ce92b3aebb0f87e07e5cff03a3b3d8ca56cf5a76.zip
Merge pull request #564 from tylerreichle/add-directions
add directions to Address namespace
-rw-r--r--lib/address.js69
-rw-r--r--lib/index.js2
-rw-r--r--lib/locales/en/address/direction.js10
-rw-r--r--lib/locales/en/address/direction_abbr.js10
-rw-r--r--lib/locales/en/address/index.js2
-rw-r--r--test/address.unit.js53
6 files changed, 141 insertions, 5 deletions
diff --git a/lib/address.js b/lib/address.js
index 0a6f0c5b..21e14ff6 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,9 +226,70 @@ function Address (faker) {
min = min || -180
return faker.random.number({max: max, min:min, precision:0.0001}).toFixed(4);
}
-
+
+ /**
+ * direction
+ *
+ * @method faker.address.direction
+ * @param {Boolean} useAbbr return direction abbreviation. defaults to false
+ */
+ 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. 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 (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_abbr.slice(0, 4))
+ );
+ }
+
+ this.cardinalDirection.schema = {
+ "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 (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_abbr.slice(4, 8))
+ );
+ }
+
+ this.ordinalDirection.schema = {
+ "description": "Generates an ordinal direction. Use optional useAbbr boolean to return abbrevation",
+ "sampleResults": ["Northwest", "Southeast", "SW", "NE"]
+ };
+
return this;
}
-
module.exports = Address;
diff --git a/lib/index.js b/lib/index.js
index 58f0012e..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"],
+ "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
new file mode 100644
index 00000000..3d7ff312
--- /dev/null
+++ b/lib/locales/en/address/direction.js
@@ -0,0 +1,10 @@
+module["exports"] = [
+ "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 575f1d46..05b869c5 100644
--- a/lib/locales/en/address/index.js
+++ b/lib/locales/en/address/index.js
@@ -17,3 +17,5 @@ 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");
+address.direction_abbr = require("./direction_abbr");
diff --git a/test/address.unit.js b/test/address.unit.js
index 5aba2c36..28775850 100644
--- a/test/address.unit.js
+++ b/test/address.unit.js
@@ -308,4 +308,57 @@ describe("address.js", function () {
});
});
+ describe("direction()", function () {
+ it("returns random direction", function () {
+ 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();
+ })
+ })
+
+ describe("ordinalDirection()", function () {
+ it("returns random ordinal direction", function () {
+ 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();
+ })
+ })
+
+ describe("cardinalDirection()", function () {
+ it("returns random cardinal direction", function () {
+ 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();
+ })
+ })
});