Skip to content

Commit

Permalink
[merge] Canadian zip codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Marak committed Jul 6, 2015
2 parents 35e2176 + 4ad23bc commit 36d5e85
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build/build/faker.js
Original file line number Diff line number Diff line change
Expand Up @@ -18427,7 +18427,7 @@ en_CA.address = {
"PE",
"QC",
"SK",
"YK"
"YT"
],
"default_country": [
"Canada"
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var _stringDefinitions = ["title", "separator"];

var _definitions = {
"name": ["first_name", "last_name", "prefix", "suffix", "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"],
"address": ["city_prefix", "city_suffix", "street_suffix", "county", "country", "country_code", "state", "state_abbr", "street_prefix", "zipFormat"],
"company": ["adjective", "noun", "descriptor", "bs_adjective", "bs_noun", "bs_verb", "suffix"],
"lorem": ["words"],
"hacker": ["abbreviation", "adjective", "noun", "verb", "ingverb"],
Expand Down
13 changes: 11 additions & 2 deletions lib/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@ var faker = require('../index'),
f = faker.fake;

var address = {
zipCode: function () {
return Helpers.replaceSymbolWithNumber(faker.random.array_element(["#####", '#####-####']));
zipCode: function (format) {
// if zip format is not specified, use the zip format defined for the locale
if (typeof format === 'undefined') {
var localeFormat = faker.definitions.address.zipFormat;
if (typeof localeFormat === 'string') {
format = localeFormat;
} else {
format = faker.random.array_element(localeFormat);
}
}
return Helpers.replaceSymbols(format);
},

city: function (format) {
Expand Down
18 changes: 18 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ exports.replaceSymbolWithNumber = function (string, symbol) {
return str;
};

// parses string for symbols (numbers or letters) and replaces them appropriately
exports.replaceSymbols = function (string) {
string = string || "";
var alpha = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
var str = '';

for (var i = 0; i < string.length; i++) {
if (string.charAt(i) == "#") {
str += faker.random.number(9);
} else if (string.charAt(i) == "?") {
str += alpha[Math.floor(Math.random() * alpha.length)];
} else {
str += string.charAt(i);
}
}
return str;
};

// takes an array and returns it randomized
exports.shuffle = function (o) {
o = o || ["a", "b", "c"];
Expand Down
4 changes: 4 additions & 0 deletions lib/locales/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ module["exports"] = en;
en.title = "English";
en.separator = " & ";
en.address = {
"zipFormat": [
"#####",
"#####-####"
],
"city_prefix": [
"North",
"East",
Expand Down
7 changes: 2 additions & 5 deletions lib/locales/en_CA.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ var en_CA = {};
module["exports"] = en_CA;
en_CA.title = "Canada (English)";
en_CA.address = {
"postcode": [
"?#? #?#",
"?#?#?#"
],
"zipFormat": "?#? #?#",
"state": [
"Alberta",
"British Columbia",
Expand Down Expand Up @@ -34,7 +31,7 @@ en_CA.address = {
"PE",
"QC",
"SK",
"YK"
"YT"
],
"default_country": [
"Canada"
Expand Down

0 comments on commit 36d5e85

Please sign in to comment.