Skip to content

Commit

Permalink
Merge pull request #800 from kaykhan/country-code-alpha-3
Browse files Browse the repository at this point in the history
feat: added support for generating a random ISO_3166-1 alpha-3 country code
  • Loading branch information
Marak authored Aug 25, 2020
2 parents 6a979f2 + 9a914b1 commit 2f694e3
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 2 deletions.
13 changes: 12 additions & 1 deletion lib/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,20 @@ function Address (faker) {
* countryCode
*
* @method faker.address.countryCode
* @param {string} alphaCode default alpha-2
*/
this.countryCode = function () {
this.countryCode = function (alphaCode) {

if (typeof alphaCode === 'undefined' || alphaCode === 'alpha-2') {
return faker.random.arrayElement(faker.definitions.address.country_code);
}

if (alphaCode === 'alpha-3') {
return faker.random.arrayElement(faker.definitions.address.country_code_alpha_3);
}

return faker.random.arrayElement(faker.definitions.address.country_code);

}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function Faker (opts) {

var _definitions = {
"name": ["first_name", "last_name", "prefix", "suffix", "gender", "title", "male_prefix", "female_prefix", "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", "postcode_by_state", "direction", "direction_abbr", "time_zone"],
"address": ["city_prefix", "city_suffix", "street_suffix", "county", "country", "country_code", "country_code_alpha_3", "state", "state_abbr", "street_prefix", "postcode", "postcode_by_state", "direction", "direction_abbr", "time_zone"],
"company": ["adjective", "noun", "descriptor", "bs_adjective", "bs_noun", "bs_verb", "suffix"],
"lorem": ["words"],
"hacker": ["abbreviation", "adjective", "noun", "verb", "ingverb", "phrase"],
Expand Down
252 changes: 252 additions & 0 deletions lib/locales/en/address/country_code_alpha_3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
module["exports"] = [
"BGD",
"BEL",
"BFA",
"BGR",
"BIH",
"BRB",
"WLF",
"BLM",
"BMU",
"BRN",
"BOL",
"BHR",
"BDI",
"BEN",
"BTN",
"JAM",
"BVT",
"BWA",
"WSM",
"BES",
"BRA",
"BHS",
"JEY",
"BLR",
"BLZ",
"RUS",
"RWA",
"SRB",
"TLS",
"REU",
"TKM",
"TJK",
"ROU",
"TKL",
"GNB",
"GUM",
"GTM",
"SGS",
"GRC",
"GNQ",
"GLP",
"JPN",
"GUY",
"GGY",
"GUF",
"GEO",
"GRD",
"GBR",
"GAB",
"SLV",
"GIN",
"GMB",
"GRL",
"GIB",
"GHA",
"OMN",
"TUN",
"JOR",
"HRV",
"HTI",
"HUN",
"HKG",
"HND",
"HMD",
"VEN",
"PRI",
"PSE",
"PLW",
"PRT",
"SJM",
"PRY",
"IRQ",
"PAN",
"PYF",
"PNG",
"PER",
"PAK",
"PHL",
"PCN",
"POL",
"SPM",
"ZMB",
"ESH",
"EST",
"EGY",
"ZAF",
"ECU",
"ITA",
"VNM",
"SLB",
"ETH",
"SOM",
"ZWE",
"SAU",
"ESP",
"ERI",
"MNE",
"MDA",
"MDG",
"MAF",
"MAR",
"MCO",
"UZB",
"MMR",
"MLI",
"MAC",
"MNG",
"MHL",
"MKD",
"MUS",
"MLT",
"MWI",
"MDV",
"MTQ",
"MNP",
"MSR",
"MRT",
"IMN",
"UGA",
"TZA",
"MYS",
"MEX",
"ISR",
"FRA",
"IOT",
"SHN",
"FIN",
"FJI",
"FLK",
"FSM",
"FRO",
"NIC",
"NLD",
"NOR",
"NAM",
"VUT",
"NCL",
"NER",
"NFK",
"NGA",
"NZL",
"NPL",
"NRU",
"NIU",
"COK",
"XKX",
"CIV",
"CHE",
"COL",
"CHN",
"CMR",
"CHL",
"CCK",
"CAN",
"COG",
"CAF",
"COD",
"CZE",
"CYP",
"CXR",
"CRI",
"CUW",
"CPV",
"CUB",
"SWZ",
"SYR",
"SXM",
"KGZ",
"KEN",
"SSD",
"SUR",
"KIR",
"KHM",
"KNA",
"COM",
"STP",
"SVK",
"KOR",
"SVN",
"PRK",
"KWT",
"SEN",
"SMR",
"SLE",
"SYC",
"KAZ",
"CYM",
"SGP",
"SWE",
"SDN",
"DOM",
"DMA",
"DJI",
"DNK",
"VGB",
"DEU",
"YEM",
"DZA",
"USA",
"URY",
"MYT",
"UMI",
"LBN",
"LCA",
"LAO",
"TUV",
"TWN",
"TTO",
"TUR",
"LKA",
"LIE",
"LVA",
"TON",
"LTU",
"LUX",
"LBR",
"LSO",
"THA",
"ATF",
"TGO",
"TCD",
"TCA",
"LBY",
"VAT",
"VCT",
"ARE",
"AND",
"ATG",
"AFG",
"AIA",
"VIR",
"ISL",
"IRN",
"ARM",
"ALB",
"AGO",
"ATA",
"ASM",
"ARG",
"AUS",
"AUT",
"ABW",
"IND",
"ALA",
"AZE",
"IRL",
"IDN",
"UKR",
"QAT",
"MOZ"
];
1 change: 1 addition & 0 deletions lib/locales/en/address/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ address.city_suffix = require("./city_suffix");
address.county = require("./county");
address.country = require("./country");
address.country_code = require("./country_code");
address.country_code_alpha_3 = require("./country_code_alpha_3");
address.building_number = require("./building_number");
address.street_suffix = require("./street_suffix");
address.secondary_address = require("./secondary_address");
Expand Down
11 changes: 11 additions & 0 deletions test/address.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,24 @@ describe("address.js", function () {
});

describe("countryCode()", function () {

it("returns random countryCode", function () {
sinon.spy(faker.address, 'countryCode');
var countryCode = faker.address.countryCode();
assert.ok(countryCode);
assert.ok(faker.address.countryCode.called);
faker.address.countryCode.restore();
});

it("returns random alpha-3 countryCode", function () {
sinon.spy(faker.address, 'countryCode');
var countryCode = faker.address.countryCode("alpha-3");
assert.ok(countryCode);
assert.ok(faker.address.countryCode.called);
assert.equal(countryCode.length, 3);
faker.address.countryCode.restore();
});

});

describe("state()", function () {
Expand Down

0 comments on commit 2f694e3

Please sign in to comment.