Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(location): Pad en_US ZIP codes left to 5 characters if needed #2278

Merged
merged 7 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/definitions/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export type LocationDefinition = LocaleEntry<{
/**
* Postcodes patterns by state
*/
postcode_by_state: { [state: string]: { min: number; max: number } };
postcode_by_state: {
[state: string]: string;
xDivisionByZerox marked this conversation as resolved.
Show resolved Hide resolved
};

/**
* Postcodes patterns.
Expand Down
260 changes: 52 additions & 208 deletions src/locales/en_US/location/postcode_by_state.ts
Original file line number Diff line number Diff line change
@@ -1,210 +1,54 @@
export default {
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: 20899,
max: 20908,
},
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: 75503,
max: 79999,
},
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,
},
AK: '{{number.int({"min": 99501,"max": 99950})}}',
AL: '{{number.int({"min": 35004,"max": 36925})}}',
AR: '{{number.int({"min": 71601,"max": 72959})}}',
AZ: '{{number.int({"min": 85001,"max": 86556})}}',
CA: '{{number.int({"min": 90001,"max": 96162})}}',
CO: '{{number.int({"min": 80001,"max": 81658})}}',
CT: '0{{number.int({"min": 6001,"max": 6389})}}',
DC: '{{number.int({"min": 20001,"max": 20039})}}',
DE: '{{number.int({"min": 19701,"max": 19980})}}',
FL: '{{number.int({"min": 32004,"max": 34997})}}',
GA: '{{number.int({"min": 30001,"max": 31999})}}',
HI: '{{number.int({"min": 96701,"max": 96898})}}',
IA: '{{number.int({"min": 50001,"max": 52809})}}',
ID: '{{number.int({"min": 83201,"max": 83876})}}',
IL: '{{number.int({"min": 60001,"max": 62999})}}',
IN: '{{number.int({"min": 46001,"max": 47997})}}',
KS: '{{number.int({"min": 66002,"max": 67954})}}',
KY: '{{number.int({"min": 40003,"max": 42788})}}',
LA: '{{number.int({"min": 70001,"max": 71232})}}',
MA: '0{{number.int({"min": 1001,"max": 2791})}}',
MD: '{{number.int({"min": 20899,"max": 20908})}}',
ME: '0{{number.int({"min": 3901,"max": 4992})}}',
MI: '{{number.int({"min": 48001,"max": 49971})}}',
MN: '{{number.int({"min": 55001,"max": 56763})}}',
MO: '{{number.int({"min": 63001,"max": 65899})}}',
MS: '{{number.int({"min": 38601,"max": 39776})}}',
MT: '{{number.int({"min": 59001,"max": 59937})}}',
NC: '{{number.int({"min": 27006,"max": 28909})}}',
ND: '{{number.int({"min": 58001,"max": 58856})}}',
NE: '{{number.int({"min": 68001,"max": 68118})}}',
NH: '0{{number.int({"min": 3031,"max": 3897})}}',
NJ: '0{{number.int({"min": 7001,"max": 8989})}}',
NM: '{{number.int({"min": 87001,"max": 88441})}}',
NV: '{{number.int({"min": 88901,"max": 89883})}}',
NY: '0{{number.int({"min": 6390,"max": 6390})}}',
OH: '{{number.int({"min": 43001,"max": 45999})}}',
OK: '{{number.int({"min": 73001,"max": 73199})}}',
OR: '{{number.int({"min": 97001,"max": 97920})}}',
PA: '{{number.int({"min": 15001,"max": 19640})}}',
PR: '00{{number.int({"min": 601,"max": 988})}}',
RI: '0{{number.int({"min": 2801,"max": 2940})}}',
SC: '{{number.int({"min": 29001,"max": 29948})}}',
SD: '{{number.int({"min": 57001,"max": 57799})}}',
TN: '{{number.int({"min": 37010,"max": 38589})}}',
TX: '{{number.int({"min": 75503,"max": 79999})}}',
UT: '{{number.int({"min": 84001,"max": 84784})}}',
VA: '{{number.int({"min": 20040,"max": 20041})}}',
VT: '0{{number.int({"min": 5001,"max": 5495})}}',
WA: '{{number.int({"min": 98001,"max": 99403})}}',
WI: '{{number.int({"min": 53001,"max": 54990})}}',
WV: '{{number.int({"min": 24701,"max": 26886})}}',
WY: '{{number.int({"min": 82001,"max": 83128})}}',
};
7 changes: 4 additions & 3 deletions src/modules/location/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ export class LocationModule {
const { state } = options;

if (state) {
const zipRange = this.faker.definitions.location.postcode_by_state[state];
const zipPattern: string =
this.faker.definitions.location.postcode_by_state[state];

if (zipRange) {
return String(this.faker.number.int(zipRange));
if (zipPattern) {
return this.faker.helpers.fake(zipPattern);
}

throw new FakerError(`No zip code definition found for state "${state}"`);
Expand Down
5 changes: 5 additions & 0 deletions test/modules/location.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ describe('location', () => {
expect(zipCode1).toBeLessThanOrEqual(upper);
});

it('should return a zip code with length 5 for ZIP codes that start with 0', () => {
const zipCode = fakerEN_US.location.zipCode({ state: 'NH' });
expect(zipCode.length).toBe(5);
});

it('should throw when definitions.location.postcode_by_state not set', () => {
expect(() => faker.location.zipCode({ state: 'XX' })).toThrow(
new FakerError(
Expand Down