forked from zuriby/Faker.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
address.js
379 lines (339 loc) · 11 KB
/
address.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/**
*
* @namespace faker.address
*/
function Address (faker) {
var f = faker.fake,
Helpers = faker.helpers;
/**
* Generates random zipcode from format. If format is not specified, the
* locale's zip format is used.
*
* @method faker.address.zipCode
* @param {String} format
*/
this.zipCode = function(format) {
// if zip format is not specified, use the zip format defined for the locale
if (typeof format === 'undefined') {
var localeFormat = faker.definitions.address.postcode;
if (typeof localeFormat === 'string') {
format = localeFormat;
} else {
format = faker.random.arrayElement(localeFormat);
}
}
return Helpers.replaceSymbols(format);
}
/**
* Generates random zipcode from state abbreviation. If state abbreviation is
* not specified, a random zip code is generated according to the locale's zip format.
* Only works for locales with postcode_by_state definition. If a locale does not
* have a postcode_by_state definition, a random zip code is generated according
* to the locale's zip format.
*
* @method faker.address.zipCodeByState
* @param {String} state
*/
this.zipCodeByState = function (state) {
var zipRange = faker.definitions.address.postcode_by_state[state];
if (zipRange) {
return faker.random.number(zipRange);
}
return this.zipCode();
}
/**
* Generates a random localized city name. The format string can contain any
* method provided by faker wrapped in `{{}}`, e.g. `{{name.firstName}}` in
* order to build the city name.
*
* If no format string is provided one of the following is randomly used:
*
* * `{{address.cityPrefix}} {{name.firstName}}{{address.citySuffix}}`
* * `{{address.cityPrefix}} {{name.firstName}}`
* * `{{name.firstName}}{{address.citySuffix}}`
* * `{{name.lastName}}{{address.citySuffix}}`
*
* @method faker.address.city
* @param {String} format
*/
this.city = function (format) {
var formats = [
'{{address.cityPrefix}} {{name.firstName}}{{address.citySuffix}}',
'{{address.cityPrefix}} {{name.firstName}}',
'{{name.firstName}}{{address.citySuffix}}',
'{{name.lastName}}{{address.citySuffix}}'
];
if (typeof format !== "number") {
format = faker.random.number(formats.length - 1);
}
return f(formats[format]);
}
/**
* Return a random localized city prefix
* @method faker.address.cityPrefix
*/
this.cityPrefix = function () {
return faker.random.arrayElement(faker.definitions.address.city_prefix);
}
/**
* Return a random localized city suffix
*
* @method faker.address.citySuffix
*/
this.citySuffix = function () {
return faker.random.arrayElement(faker.definitions.address.city_suffix);
}
/**
* Returns a random localized street name
*
* @method faker.address.streetName
*/
this.streetName = function () {
var result;
var suffix = faker.address.streetSuffix();
if (suffix !== "") {
suffix = " " + suffix
}
switch (faker.random.number(1)) {
case 0:
result = faker.name.lastName() + suffix;
break;
case 1:
result = faker.name.firstName() + suffix;
break;
}
return result;
}
//
// TODO: change all these methods that accept a boolean to instead accept an options hash.
//
/**
* Returns a random localized street address
*
* @method faker.address.streetAddress
* @param {Boolean} useFullAddress
*/
this.streetAddress = function (useFullAddress) {
if (useFullAddress === undefined) { useFullAddress = false; }
var address = "";
switch (faker.random.number(2)) {
case 0:
address = Helpers.replaceSymbolWithNumber("#####") + " " + faker.address.streetName();
break;
case 1:
address = Helpers.replaceSymbolWithNumber("####") + " " + faker.address.streetName();
break;
case 2:
address = Helpers.replaceSymbolWithNumber("###") + " " + faker.address.streetName();
break;
}
return useFullAddress ? (address + " " + faker.address.secondaryAddress()) : address;
}
/**
* streetSuffix
*
* @method faker.address.streetSuffix
*/
this.streetSuffix = function () {
return faker.random.arrayElement(faker.definitions.address.street_suffix);
}
/**
* streetPrefix
*
* @method faker.address.streetPrefix
*/
this.streetPrefix = function () {
return faker.random.arrayElement(faker.definitions.address.street_prefix);
}
/**
* secondaryAddress
*
* @method faker.address.secondaryAddress
*/
this.secondaryAddress = function () {
return Helpers.replaceSymbolWithNumber(faker.random.arrayElement(
[
'Apt. ###',
'Suite ###'
]
));
}
/**
* county
*
* @method faker.address.county
*/
this.county = function () {
return faker.random.arrayElement(faker.definitions.address.county);
}
/**
* country
*
* @method faker.address.country
*/
this.country = function () {
return faker.random.arrayElement(faker.definitions.address.country);
}
/**
* countryCode
*
* @method faker.address.countryCode
*/
this.countryCode = function () {
return faker.random.arrayElement(faker.definitions.address.country_code);
}
/**
* state
*
* @method faker.address.state
* @param {Boolean} useAbbr
*/
this.state = function (useAbbr) {
return faker.random.arrayElement(faker.definitions.address.state);
}
/**
* stateAbbr
*
* @method faker.address.stateAbbr
*/
this.stateAbbr = function () {
return faker.random.arrayElement(faker.definitions.address.state_abbr);
}
/**
* latitude
*
* @method faker.address.latitude
* @param {Double} max default is 90
* @param {Double} min default is -90
* @param {number} precision default is 4
*/
this.latitude = function (max, min, precision) {
max = max || 90
min = min || -90
precision = precision || 4
return faker.random.number({
max: max,
min: min,
precision: parseFloat((0.0).toPrecision(precision) + '1')
}).toFixed(precision);
}
/**
* longitude
*
* @method faker.address.longitude
* @param {Double} max default is 180
* @param {Double} min default is -180
* @param {number} precision default is 4
*/
this.longitude = function (max, min, precision) {
max = max || 180
min = min || -180
precision = precision || 4
return faker.random.number({
max: max,
min: min,
precision: parseFloat((0.0).toPrecision(precision) + '1')
}).toFixed(precision);
}
/**
* direction
*
* @method faker.address.direction
* @param {Boolean} useAbbr return direction abbreviation. defaults to false
*/
this.direction = function (useAbbr) {
if (typeof useAbbr === 'undefined' || useAbbr === false) {
return faker.random.arrayElement(faker.definitions.address.direction);
}
return faker.random.arrayElement(faker.definitions.address.direction_abbr);
}
this.direction.schema = {
"description": "Generates a direction. Use optional useAbbr bool to return abbrevation",
"sampleResults": ["Northwest", "South", "SW", "E"]
};
/**
* cardinal direction
*
* @method faker.address.cardinalDirection
* @param {Boolean} useAbbr return direction abbreviation. defaults to false
*/
this.cardinalDirection = function (useAbbr) {
if (typeof useAbbr === 'undefined' || useAbbr === false) {
return (
faker.random.arrayElement(faker.definitions.address.direction.slice(0, 4))
);
}
return (
faker.random.arrayElement(faker.definitions.address.direction_abbr.slice(0, 4))
);
}
this.cardinalDirection.schema = {
"description": "Generates a cardinal direction. Use optional useAbbr boolean to return abbrevation",
"sampleResults": ["North", "South", "E", "W"]
};
/**
* ordinal direction
*
* @method faker.address.ordinalDirection
* @param {Boolean} useAbbr return direction abbreviation. defaults to false
*/
this.ordinalDirection = function (useAbbr) {
if (typeof useAbbr === 'undefined' || useAbbr === false) {
return (
faker.random.arrayElement(faker.definitions.address.direction.slice(4, 8))
);
}
return (
faker.random.arrayElement(faker.definitions.address.direction_abbr.slice(4, 8))
);
}
this.ordinalDirection.schema = {
"description": "Generates an ordinal direction. Use optional useAbbr boolean to return abbrevation",
"sampleResults": ["Northwest", "Southeast", "SW", "NE"]
};
this.nearbyGPSCoordinate = function(coordinate, radius, isMetric) {
function randomFloat(min, max) {
return Math.random() * (max-min) + min;
}
function degreesToRadians(degrees) {
return degrees * (Math.PI/180.0);
}
function radiansToDegrees(radians) {
return radians * (180.0/Math.PI);
}
function kilometersToMiles(miles) {
return miles * 0.621371;
}
function coordinateWithOffset(coordinate, bearing, distance, isMetric) {
var R = 6378.137; // Radius of the Earth (http://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html)
var d = isMetric ? distance : kilometersToMiles(distance); // Distance in km
var lat1 = degreesToRadians(coordinate[0]); //Current lat point converted to radians
var lon1 = degreesToRadians(coordinate[1]); //Current long point converted to radians
var lat2 = Math.asin(Math.sin(lat1) * Math.cos(d/R) +
Math.cos(lat1) * Math.sin(d/R) * Math.cos(bearing));
var lon2 = lon1 + Math.atan2(
Math.sin(bearing) * Math.sin(d/R) * Math.cos(lat1),
Math.cos(d/R) - Math.sin(lat1) * Math.sin(lat2));
// Keep longitude in range [-180, 180]
if (lon2 > degreesToRadians(180)) {
lon2 = lon2 - degreesToRadians(360);
} else if (lon2 < degreesToRadians(-180)) {
lon2 = lon2 + degreesToRadians(360);
}
return [radiansToDegrees(lat2), radiansToDegrees(lon2)];
}
// If there is no coordinate, the best we can do is return a random GPS coordinate.
if (coordinate === undefined) {
return [this.latitude(), this.longitude()]
}
radius = radius || 10.0;
isMetric = isMetric || false;
// TODO: implement either a gaussian/uniform distribution of points in cicular region.
// Possibly include param to function that allows user to choose between distributions.
// This approach will likely result in a higher density of points near the center.
var randomCoord = coordinateWithOffset(coordinate, degreesToRadians(Math.random() * 360.0), radius, isMetric);
return [randomCoord[0].toFixed(4), randomCoord[1].toFixed(4)];
}
return this;
}
module.exports = Address;