Skip to content

Commit a8dc7e0

Browse files
authored
fix(location): no leading zero on building number or secondary address (#2032)
1 parent a94d365 commit a8dc7e0

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed

src/definitions/location.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export type LocationDefinition = LocaleEntry<{
7272
direction_abbr: string[];
7373

7474
/**
75-
* The pattern used to generate building numbers.
75+
* 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.
7676
*/
7777
building_number: string[];
7878

@@ -112,7 +112,7 @@ export type LocationDefinition = LocaleEntry<{
112112
};
113113

114114
/**
115-
* The address "inside" an address/e.g. an apartment or office.
115+
* 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.
116116
*/
117117
secondary_address: string[];
118118

src/modules/location/index.ts

+16-10
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,14 @@ export class LocationModule {
188188
* @since 8.0.0
189189
*/
190190
buildingNumber(): string {
191-
const format = this.faker.helpers.arrayElement(
192-
this.faker.definitions.location.building_number
193-
);
194-
195-
return this.faker.helpers.replaceSymbolWithNumber(format);
191+
return this.faker.helpers
192+
.arrayElement(this.faker.definitions.location.building_number)
193+
.replace(/#+/g, (m) =>
194+
this.faker.string.numeric({
195+
length: m.length,
196+
allowLeadingZeros: false,
197+
})
198+
);
196199
}
197200

198201
/**
@@ -281,11 +284,14 @@ export class LocationModule {
281284
* @since 8.0.0
282285
*/
283286
secondaryAddress(): string {
284-
return this.faker.helpers.replaceSymbolWithNumber(
285-
this.faker.helpers.arrayElement(
286-
this.faker.definitions.location.secondary_address
287-
)
288-
);
287+
return this.faker.helpers
288+
.arrayElement(this.faker.definitions.location.secondary_address)
289+
.replace(/#+/g, (m) =>
290+
this.faker.string.numeric({
291+
length: m.length,
292+
allowLeadingZeros: false,
293+
})
294+
);
289295
}
290296

291297
/**

test/__snapshots__/location.spec.ts.snap

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`location > 42 > buildingNumber 1`] = `"7917"`;
3+
exports[`location > 42 > buildingNumber 1`] = `"8917"`;
44

55
exports[`location > 42 > cardinalDirection > noArgs 1`] = `"East"`;
66

@@ -126,7 +126,7 @@ exports[`location > 42 > ordinalDirection > with abbreviated option 1`] = `"NW"`
126126

127127
exports[`location > 42 > ordinalDirection > with boolean 1`] = `"Northwest"`;
128128

129-
exports[`location > 42 > secondaryAddress 1`] = `"Apt. 791"`;
129+
exports[`location > 42 > secondaryAddress 1`] = `"Apt. 891"`;
130130

131131
exports[`location > 42 > state > noArgs 1`] = `"Maine"`;
132132

@@ -136,11 +136,11 @@ exports[`location > 42 > stateAbbr 1`] = `"ME"`;
136136

137137
exports[`location > 42 > street 1`] = `"Peyton Villages"`;
138138

139-
exports[`location > 42 > streetAddress > noArgs 1`] = `"7917 Miller Park"`;
139+
exports[`location > 42 > streetAddress > noArgs 1`] = `"8917 Miller Park"`;
140140

141-
exports[`location > 42 > streetAddress > with boolean 1`] = `"7917 Miller Park"`;
141+
exports[`location > 42 > streetAddress > with boolean 1`] = `"8917 Miller Park"`;
142142

143-
exports[`location > 42 > streetAddress > with useFullAddress options 1`] = `"7917 Miller Park Apt. 410"`;
143+
exports[`location > 42 > streetAddress > with useFullAddress options 1`] = `"8917 Miller Park Apt. 510"`;
144144

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

@@ -152,7 +152,7 @@ exports[`location > 42 > zipCode > with string 1`] = `"379"`;
152152

153153
exports[`location > 42 > zipCodeByState > noArgs 1`] = `"79177"`;
154154

155-
exports[`location > 1211 > buildingNumber 1`] = `"487"`;
155+
exports[`location > 1211 > buildingNumber 1`] = `"587"`;
156156

157157
exports[`location > 1211 > cardinalDirection > noArgs 1`] = `"West"`;
158158

@@ -278,7 +278,7 @@ exports[`location > 1211 > ordinalDirection > with abbreviated option 1`] = `"SW
278278

279279
exports[`location > 1211 > ordinalDirection > with boolean 1`] = `"Southwest"`;
280280

281-
exports[`location > 1211 > secondaryAddress 1`] = `"Suite 487"`;
281+
exports[`location > 1211 > secondaryAddress 1`] = `"Suite 587"`;
282282

283283
exports[`location > 1211 > state > noArgs 1`] = `"Washington"`;
284284

@@ -288,11 +288,11 @@ exports[`location > 1211 > stateAbbr 1`] = `"WA"`;
288288

289289
exports[`location > 1211 > street 1`] = `"Koelpin Turnpike"`;
290290

291-
exports[`location > 1211 > streetAddress > noArgs 1`] = `"487 Breana Wells"`;
291+
exports[`location > 1211 > streetAddress > noArgs 1`] = `"587 Breana Wells"`;
292292

293-
exports[`location > 1211 > streetAddress > with boolean 1`] = `"487 Breana Wells"`;
293+
exports[`location > 1211 > streetAddress > with boolean 1`] = `"587 Breana Wells"`;
294294

295-
exports[`location > 1211 > streetAddress > with useFullAddress options 1`] = `"487 Breana Wells Apt. 616"`;
295+
exports[`location > 1211 > streetAddress > with useFullAddress options 1`] = `"587 Breana Wells Apt. 716"`;
296296

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

@@ -304,7 +304,7 @@ exports[`location > 1211 > zipCode > with string 1`] = `"948"`;
304304

305305
exports[`location > 1211 > zipCodeByState > noArgs 1`] = `"48721-9061"`;
306306

307-
exports[`location > 1337 > buildingNumber 1`] = `"51225"`;
307+
exports[`location > 1337 > buildingNumber 1`] = `"61225"`;
308308

309309
exports[`location > 1337 > cardinalDirection > noArgs 1`] = `"East"`;
310310

@@ -430,7 +430,7 @@ exports[`location > 1337 > ordinalDirection > with abbreviated option 1`] = `"NW
430430

431431
exports[`location > 1337 > ordinalDirection > with boolean 1`] = `"Northwest"`;
432432

433-
exports[`location > 1337 > secondaryAddress 1`] = `"Apt. 512"`;
433+
exports[`location > 1337 > secondaryAddress 1`] = `"Apt. 612"`;
434434

435435
exports[`location > 1337 > state > noArgs 1`] = `"Indiana"`;
436436

@@ -440,11 +440,11 @@ exports[`location > 1337 > stateAbbr 1`] = `"IN"`;
440440

441441
exports[`location > 1337 > street 1`] = `"Kellen Crest"`;
442442

443-
exports[`location > 1337 > streetAddress > noArgs 1`] = `"51225 Alexys Gateway"`;
443+
exports[`location > 1337 > streetAddress > noArgs 1`] = `"61225 Alexys Gateway"`;
444444

445-
exports[`location > 1337 > streetAddress > with boolean 1`] = `"51225 Alexys Gateway"`;
445+
exports[`location > 1337 > streetAddress > with boolean 1`] = `"61225 Alexys Gateway"`;
446446

447-
exports[`location > 1337 > streetAddress > with useFullAddress options 1`] = `"51225 Alexys Gateway Apt. 552"`;
447+
exports[`location > 1337 > streetAddress > with useFullAddress options 1`] = `"61225 Alexys Gateway Apt. 552"`;
448448

449449
exports[`location > 1337 > timeZone 1`] = `"America/Guatemala"`;
450450

test/location.spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ describe('location', () => {
214214
});
215215
});
216216

217+
describe('buildingNumber()', () => {
218+
it('never starts with a zero', () => {
219+
const buildingNumber = faker.location.buildingNumber();
220+
expect(buildingNumber).not.toMatch(/^0/);
221+
});
222+
});
223+
217224
describe('latitude()', () => {
218225
it('returns a number', () => {
219226
const latitude = faker.location.latitude();

0 commit comments

Comments
 (0)