Skip to content

Commit

Permalink
refactors zipCodeByState to match correct zip code ranges with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
willyraedy committed Jan 20, 2018
1 parent fd6df80 commit e797362
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 65 deletions.
19 changes: 14 additions & 5 deletions lib/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,22 @@ function Address (faker) {
return Helpers.replaceSymbols(format);
}


/**
* Generates random zipcode from state abbreviation. If state abbreviation is
* not specified, a random zip code is generated according to the locale's zip format.
* Only works for locales with postcode_by_state definition. If a locale does not
* have a postcode_by_state definition, a random zip code is generated according
* to the locale's zip format.
*
* @method faker.address.zipCodeByState
* @param {String} state
*/
this.zipCodeByState = function (state) {
var zipFormat = faker.definitions.address.postcode_by_state[state];
if (zipFormat) {
return this.zipCode(zipFormat);
var zipRange = faker.definitions.address.postcode_by_state[state];
if (zipRange) {
return faker.random.number(zipRange);
}
return undefined;
return this.zipCode();
}

/**
Expand Down
262 changes: 209 additions & 53 deletions lib/locales/en_US/address/postcode_by_state.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,210 @@
module["exports"] = {
"AL": "350##",
"AK": "995##",
"AS": "967##",
"AZ": "850##",
"AR": "717##",
"CA": "900##",
"CO": "800##",
"CT": "061##",
"DC": "204##",
"DE": "198##",
"FL": "322##",
"GA": "301##",
"HI": "967##",
"ID": "832##",
"IL": "600##",
"IN": "463##",
"IA": "510##",
"KS": "666##",
"KY": "404##",
"LA": "701##",
"ME": "042##",
"MD": "210##",
"MA": "026##",
"MI": "480##",
"MN": "555##",
"MS": "387##",
"MO": "650##",
"MT": "590##",
"NE": "688##",
"NV": "898##",
"NH": "036##",
"NJ": "076##",
"NM": "880##",
"NY": "122##",
"NC": "288##",
"ND": "586##",
"OH": "444##",
"OK": "730##",
"OR": "979##",
"PA": "186##",
"RI": "029##",
"SC": "299##",
"SD": "577##",
"TN": "383##",
"TX": "798##",
"UT": "847##",
"VT": "050##",
"VA": "222##",
"WA": "990##",
"WV": "247##",
"WI": "549##",
"WY": "831##"
};
AK:{
min:99501,
max:99950
},
AL:{
min:35004,
max:36925
},
AR:{
min:71601,
max:72959
},
AZ:{
min:85001,
max:86556
},
CA:{
min:90001,
max:96162
},
CO:{
min:80001,
max:81658
},
CT:{
min:6001,
max:6389
},
DC:{
min:20001,
max:20039
},
DE:{
min:19701,
max:19980
},
FL:{
min:32004,
max:34997
},
GA:{
min:30001,
max:31999
},
HI:{
min:96701,
max:96898
},
IA:{
min:50001,
max:52809
},
ID:{
min:83201,
max:83876
},
IL:{
min:60001,
max:62999
},
IN:{
min:46001,
max:47997
},
KS:{
min:66002,
max:67954
},
KY:{
min:40003,
max:42788
},
LA:{
min:70001,
max:71232
},
MA:{
min:1001,
max:2791
},
MD:{
min:20331,
max:20331
},
ME:{
min:3901,
max:4992
},
MI:{
min:48001,
max:49971
},
MN:{
min:55001,
max:56763
},
MO:{
min:63001,
max:65899
},
MS:{
min:38601,
max:39776
},
MT:{
min:59001,
max:59937
},
NC:{
min:27006,
max:28909
},
ND:{
min:58001,
max:58856
},
NE:{
min:68001,
max:68118
},
NH:{
min:3031,
max:3897
},
NJ:{
min:7001,
max:8989
},
NM:{
min:87001,
max:88441
},
NV:{
min:88901,
max:89883
},
NY:{
min:6390,
max:6390
},
OH:{
min:43001,
max:45999
},
OK:{
min:73001,
max:73199
},
OR:{
min:97001,
max:97920
},
PA:{
min:15001,
max:19640
},
PR:{
min:0,
max:0
},
RI:{
min:2801,
max:2940
},
SC:{
min:29001,
max:29948
},
SD:{
min:57001,
max:57799
},
TN:{
min:37010,
max:38589
},
TX:{
min:73301,
max:73301
},
UT:{
min:84001,
max:84784
},
VA:{
min:20040,
max:20041
},
VT:{
min:5001,
max:5495
},
WA:{
min:98001,
max:99403
},
WI:{
min:53001,
max:54990
},
WV:{
min:24701,
max:26886
},
WY:{
min:82001,
max:83128
}
}
32 changes: 25 additions & 7 deletions test/address.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,19 +252,37 @@ describe("address.js", function () {
});
});

describe.only("zipCodeByState()", function () {
describe("zipCodeByState()", function () {
it("returns zipCode valid for specified State", function () {
faker.locale = "en_US";
var state = "IL";
var zipCode = faker.address.zipCodeByState(state);
var states = ["IL", "GA", "WA"];

assert.ok(zipCode >= 60000);
assert.ok(zipCode <= 60099);
var zipCode1 = faker.address.zipCodeByState(states[0]);
assert.ok(zipCode1 >= 60001);
assert.ok(zipCode1 <= 62999);
var zipCode2 = faker.address.zipCodeByState(states[1]);
assert.ok(zipCode2 >= 30001);
assert.ok(zipCode2 <= 31999);
var zipCode3 = faker.address.zipCodeByState(states[2]);
assert.ok(zipCode3 >= 98001);
assert.ok(zipCode3 <= 99403);
});

it("throws error if locale is invalid", function () {
it("returns undefined if state is invalid", function () {
var state = "XX";
sinon.spy(faker.address, 'zipCode');
var zipCode = faker.address.zipCodeByState(state);
assert.ok(faker.address.zipCode.called);
faker.address.zipCode.restore();
});
it("throws error if state is invalid", function () {

it("returns undefined if state is valid but localeis invalid", function () {
faker.locale = "zh_CN";
var state = "IL";
sinon.spy(faker.address, 'zipCode');
var zipCode = faker.address.zipCodeByState(state);
assert.ok(faker.address.zipCode.called);
faker.address.zipCode.restore();
});
});

Expand Down

0 comments on commit e797362

Please sign in to comment.