diff options
| author | Matt Mayer <[email protected]> | 2023-05-01 16:03:00 +0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-05-01 09:03:00 +0000 |
| commit | a8dc7e07f6d5ee2ae38724ba5d503d7b88bd7147 (patch) | |
| tree | fb787d9a30e1305c3c2ffb0364395476caa0b93e | |
| parent | a94d365a2313a7597065b7468abb2d79b6da026e (diff) | |
| download | faker-a8dc7e07f6d5ee2ae38724ba5d503d7b88bd7147.tar.xz faker-a8dc7e07f6d5ee2ae38724ba5d503d7b88bd7147.zip | |
fix(location): no leading zero on building number or secondary address (#2032)
| -rw-r--r-- | src/definitions/location.ts | 4 | ||||
| -rw-r--r-- | src/modules/location/index.ts | 26 | ||||
| -rw-r--r-- | test/__snapshots__/location.spec.ts.snap | 30 | ||||
| -rw-r--r-- | test/location.spec.ts | 7 |
4 files changed, 40 insertions, 27 deletions
diff --git a/src/definitions/location.ts b/src/definitions/location.ts index 9af95fc2..b8e7ebab 100644 --- a/src/definitions/location.ts +++ b/src/definitions/location.ts @@ -72,7 +72,7 @@ export type LocationDefinition = LocaleEntry<{ direction_abbr: string[]; /** - * The pattern used to generate building numbers. + * The pattern used to generate building numbers. Since building numbers rarely start with 0, any consecutive # characters will be replaced by a number without a leading zero. */ building_number: string[]; @@ -112,7 +112,7 @@ export type LocationDefinition = LocaleEntry<{ }; /** - * The address "inside" an address/e.g. an apartment or office. + * The address "inside" an address/e.g. an apartment or office. Since these rarely start with 0, any consecutive # characters will be replaced by a number without a leading zero. */ secondary_address: string[]; diff --git a/src/modules/location/index.ts b/src/modules/location/index.ts index e924bd7e..5b5d37a1 100644 --- a/src/modules/location/index.ts +++ b/src/modules/location/index.ts @@ -188,11 +188,14 @@ export class LocationModule { * @since 8.0.0 */ buildingNumber(): string { - const format = this.faker.helpers.arrayElement( - this.faker.definitions.location.building_number - ); - - return this.faker.helpers.replaceSymbolWithNumber(format); + return this.faker.helpers + .arrayElement(this.faker.definitions.location.building_number) + .replace(/#+/g, (m) => + this.faker.string.numeric({ + length: m.length, + allowLeadingZeros: false, + }) + ); } /** @@ -281,11 +284,14 @@ export class LocationModule { * @since 8.0.0 */ secondaryAddress(): string { - return this.faker.helpers.replaceSymbolWithNumber( - this.faker.helpers.arrayElement( - this.faker.definitions.location.secondary_address - ) - ); + return this.faker.helpers + .arrayElement(this.faker.definitions.location.secondary_address) + .replace(/#+/g, (m) => + this.faker.string.numeric({ + length: m.length, + allowLeadingZeros: false, + }) + ); } /** diff --git a/test/__snapshots__/location.spec.ts.snap b/test/__snapshots__/location.spec.ts.snap index d5476653..58600952 100644 --- a/test/__snapshots__/location.spec.ts.snap +++ b/test/__snapshots__/location.spec.ts.snap @@ -1,6 +1,6 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`location > 42 > buildingNumber 1`] = `"7917"`; +exports[`location > 42 > buildingNumber 1`] = `"8917"`; exports[`location > 42 > cardinalDirection > noArgs 1`] = `"East"`; @@ -126,7 +126,7 @@ exports[`location > 42 > ordinalDirection > with abbreviated option 1`] = `"NW"` exports[`location > 42 > ordinalDirection > with boolean 1`] = `"Northwest"`; -exports[`location > 42 > secondaryAddress 1`] = `"Apt. 791"`; +exports[`location > 42 > secondaryAddress 1`] = `"Apt. 891"`; exports[`location > 42 > state > noArgs 1`] = `"Maine"`; @@ -136,11 +136,11 @@ exports[`location > 42 > stateAbbr 1`] = `"ME"`; exports[`location > 42 > street 1`] = `"Peyton Villages"`; -exports[`location > 42 > streetAddress > noArgs 1`] = `"7917 Miller Park"`; +exports[`location > 42 > streetAddress > noArgs 1`] = `"8917 Miller Park"`; -exports[`location > 42 > streetAddress > with boolean 1`] = `"7917 Miller Park"`; +exports[`location > 42 > streetAddress > with boolean 1`] = `"8917 Miller Park"`; -exports[`location > 42 > streetAddress > with useFullAddress options 1`] = `"7917 Miller Park Apt. 410"`; +exports[`location > 42 > streetAddress > with useFullAddress options 1`] = `"8917 Miller Park Apt. 510"`; exports[`location > 42 > timeZone 1`] = `"America/North_Dakota/New_Salem"`; @@ -152,7 +152,7 @@ exports[`location > 42 > zipCode > with string 1`] = `"379"`; exports[`location > 42 > zipCodeByState > noArgs 1`] = `"79177"`; -exports[`location > 1211 > buildingNumber 1`] = `"487"`; +exports[`location > 1211 > buildingNumber 1`] = `"587"`; exports[`location > 1211 > cardinalDirection > noArgs 1`] = `"West"`; @@ -278,7 +278,7 @@ exports[`location > 1211 > ordinalDirection > with abbreviated option 1`] = `"SW exports[`location > 1211 > ordinalDirection > with boolean 1`] = `"Southwest"`; -exports[`location > 1211 > secondaryAddress 1`] = `"Suite 487"`; +exports[`location > 1211 > secondaryAddress 1`] = `"Suite 587"`; exports[`location > 1211 > state > noArgs 1`] = `"Washington"`; @@ -288,11 +288,11 @@ exports[`location > 1211 > stateAbbr 1`] = `"WA"`; exports[`location > 1211 > street 1`] = `"Koelpin Turnpike"`; -exports[`location > 1211 > streetAddress > noArgs 1`] = `"487 Breana Wells"`; +exports[`location > 1211 > streetAddress > noArgs 1`] = `"587 Breana Wells"`; -exports[`location > 1211 > streetAddress > with boolean 1`] = `"487 Breana Wells"`; +exports[`location > 1211 > streetAddress > with boolean 1`] = `"587 Breana Wells"`; -exports[`location > 1211 > streetAddress > with useFullAddress options 1`] = `"487 Breana Wells Apt. 616"`; +exports[`location > 1211 > streetAddress > with useFullAddress options 1`] = `"587 Breana Wells Apt. 716"`; exports[`location > 1211 > timeZone 1`] = `"Pacific/Fiji"`; @@ -304,7 +304,7 @@ exports[`location > 1211 > zipCode > with string 1`] = `"948"`; exports[`location > 1211 > zipCodeByState > noArgs 1`] = `"48721-9061"`; -exports[`location > 1337 > buildingNumber 1`] = `"51225"`; +exports[`location > 1337 > buildingNumber 1`] = `"61225"`; exports[`location > 1337 > cardinalDirection > noArgs 1`] = `"East"`; @@ -430,7 +430,7 @@ exports[`location > 1337 > ordinalDirection > with abbreviated option 1`] = `"NW exports[`location > 1337 > ordinalDirection > with boolean 1`] = `"Northwest"`; -exports[`location > 1337 > secondaryAddress 1`] = `"Apt. 512"`; +exports[`location > 1337 > secondaryAddress 1`] = `"Apt. 612"`; exports[`location > 1337 > state > noArgs 1`] = `"Indiana"`; @@ -440,11 +440,11 @@ exports[`location > 1337 > stateAbbr 1`] = `"IN"`; exports[`location > 1337 > street 1`] = `"Kellen Crest"`; -exports[`location > 1337 > streetAddress > noArgs 1`] = `"51225 Alexys Gateway"`; +exports[`location > 1337 > streetAddress > noArgs 1`] = `"61225 Alexys Gateway"`; -exports[`location > 1337 > streetAddress > with boolean 1`] = `"51225 Alexys Gateway"`; +exports[`location > 1337 > streetAddress > with boolean 1`] = `"61225 Alexys Gateway"`; -exports[`location > 1337 > streetAddress > with useFullAddress options 1`] = `"51225 Alexys Gateway Apt. 552"`; +exports[`location > 1337 > streetAddress > with useFullAddress options 1`] = `"61225 Alexys Gateway Apt. 552"`; exports[`location > 1337 > timeZone 1`] = `"America/Guatemala"`; diff --git a/test/location.spec.ts b/test/location.spec.ts index 9ea62cd9..e1b9e1ca 100644 --- a/test/location.spec.ts +++ b/test/location.spec.ts @@ -214,6 +214,13 @@ describe('location', () => { }); }); + describe('buildingNumber()', () => { + it('never starts with a zero', () => { + const buildingNumber = faker.location.buildingNumber(); + expect(buildingNumber).not.toMatch(/^0/); + }); + }); + describe('latitude()', () => { it('returns a number', () => { const latitude = faker.location.latitude(); |
