Skip to content

Commit

Permalink
fix(location): no leading zero on building number or secondary address (
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmayer authored May 1, 2023
1 parent a94d365 commit a8dc7e0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/definitions/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];

Expand Down Expand Up @@ -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[];

Expand Down
26 changes: 16 additions & 10 deletions src/modules/location/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
);
}

/**
Expand Down Expand Up @@ -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,
})
);
}

/**
Expand Down
30 changes: 15 additions & 15 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 @@ -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"`;

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

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

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

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

Expand All @@ -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

0 comments on commit a8dc7e0

Please sign in to comment.