Skip to content

fix(location): no leading zero on building number or secondary address #2032

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

Merged
2 changes: 1 addition & 1 deletion src/definitions/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export type LocationDefinitions = 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[];

Expand Down
11 changes: 8 additions & 3 deletions src/modules/location/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,14 @@ export class LocationModule {
* @since 8.0.0
*/
buildingNumber(): string {
const format = this.faker.helpers.arrayElement(
this.faker.definitions.location.building_number
);
const format = this.faker.helpers
.arrayElement(this.faker.definitions.location.building_number)
.replace(/#+/g, (m) =>
this.faker.string.numeric({
length: m.length,
allowLeadingZeros: false,
})
);

return this.faker.helpers.replaceSymbolWithNumber(format);
}
Expand Down
24 changes: 12 additions & 12 deletions test/__snapshots__/location.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -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"`;

Expand Down Expand Up @@ -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. 410"`;

exports[`location > 42 > timeZone 1`] = `"America/North_Dakota/New_Salem"`;

Expand All @@ -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"`;

Expand Down Expand Up @@ -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. 616"`;

exports[`location > 1211 > timeZone 1`] = `"Pacific/Fiji"`;

Expand All @@ -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"`;

Expand Down Expand Up @@ -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"`;

Expand Down
7 changes: 7 additions & 0 deletions test/location.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down