From 941faa2957abf0e5c129950934b439de0a55d0dc Mon Sep 17 00:00:00 2001 From: Matt Mayer Date: Wed, 26 Jul 2023 16:56:11 -0400 Subject: [PATCH 1/4] fix(location): Pad en_US ZIP codes left to 5 characters if needed --- src/definitions/location.ts | 4 +++- src/locales/en_US/location/postcode_by_state.ts | 7 +++++++ src/modules/location/index.ts | 7 ++++++- test/location.spec.ts | 5 +++++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/definitions/location.ts b/src/definitions/location.ts index b8e7ebabffe..e1dc3b81645 100644 --- a/src/definitions/location.ts +++ b/src/definitions/location.ts @@ -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]: { min: number; max: number; length?: number }; + }; /** * Postcodes patterns. diff --git a/src/locales/en_US/location/postcode_by_state.ts b/src/locales/en_US/location/postcode_by_state.ts index 1c49baf3876..52c8ef43c4b 100644 --- a/src/locales/en_US/location/postcode_by_state.ts +++ b/src/locales/en_US/location/postcode_by_state.ts @@ -26,6 +26,7 @@ export default { CT: { min: 6001, max: 6389, + length: 5, }, DC: { min: 20001, @@ -78,6 +79,7 @@ export default { MA: { min: 1001, max: 2791, + length: 5, }, MD: { min: 20899, @@ -86,6 +88,7 @@ export default { ME: { min: 3901, max: 4992, + length: 5, }, MI: { min: 48001, @@ -122,6 +125,7 @@ export default { NH: { min: 3031, max: 3897, + length: 5, }, NJ: { min: 7001, @@ -138,6 +142,7 @@ export default { NY: { min: 6390, max: 6390, + length: 5, }, OH: { min: 43001, @@ -158,6 +163,7 @@ export default { PR: { min: 0, max: 0, + length: 5, }, RI: { min: 2801, @@ -190,6 +196,7 @@ export default { VT: { min: 5001, max: 5495, + length: 5, }, WA: { min: 98001, diff --git a/src/modules/location/index.ts b/src/modules/location/index.ts index cdfa44d029c..cb686419d41 100644 --- a/src/modules/location/index.ts +++ b/src/modules/location/index.ts @@ -68,7 +68,12 @@ export class LocationModule { const zipRange = this.faker.definitions.location.postcode_by_state[state]; if (zipRange) { - return String(this.faker.number.int(zipRange)); + let zip = String(this.faker.number.int(zipRange)); + if (zipRange.length) { + zip = zip.padStart(zipRange.length, '0'); + } + + return zip; } throw new FakerError(`No zip code definition found for state "${state}"`); diff --git a/test/location.spec.ts b/test/location.spec.ts index 578b91d4cfc..0c96700149d 100644 --- a/test/location.spec.ts +++ b/test/location.spec.ts @@ -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( From 75297a230b38ef38b152539877268e98c688c43c Mon Sep 17 00:00:00 2001 From: Matt Mayer Date: Wed, 26 Jul 2023 20:26:07 -0400 Subject: [PATCH 2/4] fix NJ and RI --- src/locales/en_US/location/postcode_by_state.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/locales/en_US/location/postcode_by_state.ts b/src/locales/en_US/location/postcode_by_state.ts index 52c8ef43c4b..2a9e886bc88 100644 --- a/src/locales/en_US/location/postcode_by_state.ts +++ b/src/locales/en_US/location/postcode_by_state.ts @@ -130,6 +130,7 @@ export default { NJ: { min: 7001, max: 8989, + length: 5, }, NM: { min: 87001, @@ -168,6 +169,7 @@ export default { RI: { min: 2801, max: 2940, + length: 5, }, SC: { min: 29001, From 2d6d2ede3889cd38ada88e7defb570a32e7a1644 Mon Sep 17 00:00:00 2001 From: Matt Mayer Date: Wed, 26 Jul 2023 20:27:10 -0400 Subject: [PATCH 3/4] fix PR --- src/locales/en_US/location/postcode_by_state.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/locales/en_US/location/postcode_by_state.ts b/src/locales/en_US/location/postcode_by_state.ts index 2a9e886bc88..ae0434e76de 100644 --- a/src/locales/en_US/location/postcode_by_state.ts +++ b/src/locales/en_US/location/postcode_by_state.ts @@ -162,8 +162,8 @@ export default { max: 19640, }, PR: { - min: 0, - max: 0, + min: 601, + max: 988, length: 5, }, RI: { From e6984f59349bfa428039c7a8c8ec3bc0a4b7b504 Mon Sep 17 00:00:00 2001 From: Matt Mayer Date: Mon, 31 Jul 2023 14:23:28 -0400 Subject: [PATCH 4/4] updated postcode_by_state to use string patterns --- src/definitions/location.ts | 2 +- .../en_US/location/postcode_by_state.ts | 269 ++++-------------- src/modules/location/index.ts | 12 +- 3 files changed, 57 insertions(+), 226 deletions(-) diff --git a/src/definitions/location.ts b/src/definitions/location.ts index e1dc3b81645..b32ae1c160e 100644 --- a/src/definitions/location.ts +++ b/src/definitions/location.ts @@ -8,7 +8,7 @@ export type LocationDefinition = LocaleEntry<{ * Postcodes patterns by state */ postcode_by_state: { - [state: string]: { min: number; max: number; length?: number }; + [state: string]: string; }; /** diff --git a/src/locales/en_US/location/postcode_by_state.ts b/src/locales/en_US/location/postcode_by_state.ts index ae0434e76de..70b61700453 100644 --- a/src/locales/en_US/location/postcode_by_state.ts +++ b/src/locales/en_US/location/postcode_by_state.ts @@ -1,219 +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, - length: 5, - }, - 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, - length: 5, - }, - MD: { - min: 20899, - max: 20908, - }, - ME: { - min: 3901, - max: 4992, - length: 5, - }, - 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, - length: 5, - }, - NJ: { - min: 7001, - max: 8989, - length: 5, - }, - NM: { - min: 87001, - max: 88441, - }, - NV: { - min: 88901, - max: 89883, - }, - NY: { - min: 6390, - max: 6390, - length: 5, - }, - OH: { - min: 43001, - max: 45999, - }, - OK: { - min: 73001, - max: 73199, - }, - OR: { - min: 97001, - max: 97920, - }, - PA: { - min: 15001, - max: 19640, - }, - PR: { - min: 601, - max: 988, - length: 5, - }, - RI: { - min: 2801, - max: 2940, - length: 5, - }, - 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, - length: 5, - }, - 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})}}', }; diff --git a/src/modules/location/index.ts b/src/modules/location/index.ts index cb686419d41..175c5433d14 100644 --- a/src/modules/location/index.ts +++ b/src/modules/location/index.ts @@ -65,15 +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) { - let zip = String(this.faker.number.int(zipRange)); - if (zipRange.length) { - zip = zip.padStart(zipRange.length, '0'); - } - - return zip; + if (zipPattern) { + return this.faker.helpers.fake(zipPattern); } throw new FakerError(`No zip code definition found for state "${state}"`);