Skip to content

Commit 4977cab

Browse files
Improve address generation
1 parent dee8749 commit 4977cab

File tree

1 file changed

+123
-30
lines changed

1 file changed

+123
-30
lines changed

app/lib/generators/address-generator.js

Lines changed: 123 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,70 @@
22

33
const { faker } = require('@faker-js/faker');
44

5-
// Common UK street names
6-
const STREETS = [
7-
'High Street',
8-
'Church Road',
9-
'Station Road',
10-
'Victoria Road',
11-
'Manor Road',
12-
'The Green',
13-
'Queens Road',
14-
'Kings Road',
15-
'New Road',
16-
'School Lane',
17-
'Mill Lane',
18-
'The Avenue',
19-
'Park Road',
20-
'London Road',
21-
'York Road'
22-
];
5+
// Common UK street suffixes
6+
const STREET_SUFFIXES = [
7+
'Street',
8+
'Road',
9+
'Lane',
10+
'Avenue',
11+
'Close',
12+
'Drive',
13+
'Way',
14+
'Gardens',
15+
'Crescent',
16+
'Grove',
17+
'Place',
18+
'Green',
19+
'Park',
20+
'Walk',
21+
'Mews'
22+
]
23+
24+
// Common UK street name prefixes
25+
const STREET_PREFIXES = [
26+
'High',
27+
'Church',
28+
'Station',
29+
'Victoria',
30+
'Manor',
31+
'Queen\'s',
32+
'King\'s',
33+
'New',
34+
'School',
35+
'Mill',
36+
'Park',
37+
'London',
38+
'York',
39+
'Albert',
40+
'Windsor',
41+
'Castle',
42+
'George',
43+
'The',
44+
'Old',
45+
'North',
46+
'South',
47+
'East',
48+
'West'
49+
]
50+
51+
// Common house/building names
52+
const HOUSE_NAMES = [
53+
'Rose Cottage',
54+
'The Old School House',
55+
'The Coach House',
56+
'The Old Vicarage',
57+
'The Gables',
58+
'The Willows',
59+
'Orchard House',
60+
'Oak House',
61+
'Ivy Cottage',
62+
'The Old Post Office',
63+
'The Old Rectory',
64+
'Holly Cottage',
65+
'The Laurels',
66+
'The Old Farm',
67+
'Sunnyside'
68+
]
2369

2470
/**
2571
* Extract postcode area (first 1-2 letters) from a full postcode
@@ -62,7 +108,45 @@ const generateNearbyPostcode = (referencePostcode) => {
62108
faker.helpers.arrayElement('ABCDEFGHJKLMNPQRSTUWXYZ');
63109

64110
return `${area}${nearbyDistrict} ${sector}${unit}`;
65-
};
111+
}
112+
113+
/**
114+
* Generate a random street name
115+
* @returns {string} Generated street name
116+
*/
117+
const generateStreetName = () => {
118+
const prefix = faker.helpers.arrayElement(STREET_PREFIXES)
119+
const suffix = faker.helpers.arrayElement(STREET_SUFFIXES)
120+
return `${prefix} ${suffix}`
121+
}
122+
123+
/**
124+
* Generate line 1 of an address
125+
* @returns {Object} Address line 1 details
126+
*/
127+
const generateAddressLine1 = () => {
128+
// 20% chance of using a house name instead of number
129+
if (Math.random() < 0.2) {
130+
return {
131+
line1: faker.helpers.arrayElement(HOUSE_NAMES)
132+
}
133+
}
134+
135+
const houseNumber = faker.number.int({ min: 1, max: 300 })
136+
const streetName = generateStreetName()
137+
138+
// 15% chance of being a flat/apartment
139+
if (Math.random() < 0.15) {
140+
const flatNumber = faker.number.int({ min: 1, max: 20 })
141+
return {
142+
line1: `Flat ${flatNumber}, ${houseNumber} ${streetName}`
143+
}
144+
}
145+
146+
return {
147+
line1: `${houseNumber} ${streetName}`
148+
}
149+
}
66150

67151
/**
68152
* Generate list of nearby towns/areas based on BSU location
@@ -78,15 +162,23 @@ const generateNearbyAreas = (bsu) => {
78162
.filter(l => l.type === 'hospital')
79163
.map(l => l.address.city)
80164
.filter(Boolean)
81-
].filter(Boolean));
165+
].filter(Boolean))
82166

83167
// Add some generated nearby areas
84-
for(let i = 0; i < 5; i++) {
85-
areas.add(faker.location.city());
168+
for (let i = 0; i < 2; i++) {
169+
// Use UK-style town names
170+
const suffix = faker.helpers.arrayElement([
171+
'on-Sea', 'upon-Thames', 'St Mary', 'St John',
172+
'under-Edge', 'on-the-Hill',
173+
'', '', '', '' // More weight to no suffix
174+
])
175+
176+
const name = `${faker.location.city()}${suffix ? ` ${suffix}` : ''}`
177+
areas.add(name)
86178
}
87179

88-
return Array.from(areas);
89-
};
180+
return Array.from(areas)
181+
}
90182

91183
/**
92184
* Generate an address appropriate for the BSU area
@@ -95,15 +187,16 @@ const generateNearbyAreas = (bsu) => {
95187
*/
96188
const generateBSUAppropriateAddress = (bsu) => {
97189
const nearbyAreas = generateNearbyAreas(bsu)
190+
const addressLine1 = generateAddressLine1()
98191

99-
// Helper to capitalize first letter of each word
100-
const capitalise = (str) => str.split(' ')
101-
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
102-
.join(' ')
192+
// 30% chance of having a line2
193+
const line2 = Math.random() < 0.3 ?
194+
`${faker.word.adjective().charAt(0).toUpperCase() + faker.word.adjective().slice(1)} House` :
195+
null
103196

104197
return {
105-
line1: `${faker.number.int({ min: 1, max: 300 })} ${faker.helpers.arrayElement(STREETS)}`,
106-
line2: Math.random() < 0.3 ? `${capitalise(faker.word.adjective())} House` : null,
198+
...addressLine1,
199+
line2,
107200
city: faker.helpers.arrayElement(nearbyAreas),
108201
postcode: generateNearbyPostcode(bsu.address.postcode)
109202
}

0 commit comments

Comments
 (0)