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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
|
import test from 'ava'
import Chance from '../chance.js'
import _ from 'lodash'
import phoneNumber from './helpers/phoneNumber.min.js'
const chance = new Chance()
// chance.address()
test('address() returns a string', t => {
t.true(_.isString(chance.address()))
})
test('address() starts with a number', t => {
_.times(1000, () => t.true(/[0-9]+.+/.test(chance.address())))
})
test('address() can take a short_suffix arg and obey it', t => {
_.times(1000, () => {
let address = chance.address({ short_suffix: true })
t.true(address.split(' ')[2].length < 5)
})
})
// chance.altitude()
test('altitude() looks right', t => {
t.is(typeof chance.altitude(), 'number')
})
test('altitude() is in the right range', t => {
_.times(1000, () => {
let altitude = chance.altitude()
t.true(altitude > 0)
t.true(altitude < 8848)
})
})
test('altitude() will accept a min and obey it', t => {
_.times(1000, () => {
let min = chance.floating({ min: 0, max: 8848 })
let altitude = chance.altitude({ min: min })
t.true(altitude > min)
t.true(altitude < 8848)
})
})
test('altitude() will accept a max and obey it', t => {
_.times(1000, () => {
let max = chance.floating({ min: 0, max: 8848 })
let altitude = chance.altitude({ max: max })
t.true(altitude > 0)
t.true(altitude < max)
})
})
// chance.areacode()
test('areacode() looks right', t => {
_.times(1000, () => {
let areacode = chance.areacode()
t.true(_.isString(areacode))
t.true(/^\(([2-9][0-8][0-9])\)$/.test(areacode))
})
})
test('areacode() can take parens', t => {
_.times(1000, () => {
let areacode = chance.areacode({ parens: false })
t.true(_.isString(areacode))
t.true(/^([2-9][0-8][0-9])$/.test(areacode))
})
})
// chance.city()
test('city() looks right', t => {
_.times(1000, () => {
let city = chance.city()
t.true(_.isString(city))
t.true(/[a-zA-Z]+/.test(city))
})
})
// chance.coordinates()
test('coordinates() looks right', t => {
_.times(1000, () => {
let coordinates = chance.coordinates()
t.true(_.isString(coordinates))
t.is(coordinates.split(',').length, 2)
})
})
test('coordinates() returns coordinates in DD format as default', t => {
_.times(1000, () => {
const CHARS_NOT_TO_CONTAIN = ['°', '’', '”']
let coordinates = chance.coordinates()
let [ latitude, longitude ] = coordinates.split(',')
t.true(_.isString(coordinates))
t.is(coordinates.split(',').length, 2)
t.true(CHARS_NOT_TO_CONTAIN.every(char => !latitude.includes(char)))
t.true(CHARS_NOT_TO_CONTAIN.every(char => !longitude.includes(char)))
})
})
test('coordinates() will obey DD format', t => {
_.times(1000, () => {
const CHARS_NOT_TO_CONTAIN = ['°', '’', '”']
let coordinates = chance.coordinates({format: 'dd'})
let [ latitude, longitude ] = coordinates.split(',')
t.true(_.isString(coordinates))
t.is(coordinates.split(',').length, 2)
t.true(CHARS_NOT_TO_CONTAIN.every(char => !latitude.includes(char)))
t.true(CHARS_NOT_TO_CONTAIN.every(char => !longitude.includes(char)))
})
})
test('coordinates() will obey DDM format', t => {
_.times(1000, () => {
const CHARS_TO_CONTAIN = ['°']
const CHARS_NOT_TO_CONTAIN = ['’', '”']
let coordinates = chance.coordinates({format: 'ddm'})
let [ latitude, longitude ] = coordinates.split(',')
t.true(_.isString(coordinates))
t.is(coordinates.split(',').length, 2)
t.true(CHARS_TO_CONTAIN.every(char => latitude.includes(char)))
t.true(CHARS_TO_CONTAIN.every(char => longitude.includes(char)))
t.true(CHARS_NOT_TO_CONTAIN.every(char => !latitude.includes(char)))
t.true(CHARS_NOT_TO_CONTAIN.every(char => !longitude.includes(char)))
})
})
test('coordinates() will obey DMS format', t => {
_.times(1000, () => {
const CHARS_TO_CONTAIN = ['°', '’', '”']
let coordinates = chance.coordinates({format: 'dms'})
let [ latitude, longitude ] = coordinates.split(',')
t.true(_.isString(coordinates))
t.is(coordinates.split(',').length, 2)
t.true(CHARS_TO_CONTAIN.every(char => latitude.includes(char)))
t.true(CHARS_TO_CONTAIN.every(char => longitude.includes(char)))
})
})
// chance.counties()
test('counties() returns an array of counties', t => {
t.true(_.isArray(chance.counties()))
})
test('counties() returns a (long) county name', t => {
_.times(1000, () => t.true(chance.counties({ full: true }).length > 2))
})
test('counties() can return a random (long) county name', t => {
_.times(1000, () => {
t.true(chance.counties({ full: true, country: 'uk' }).length > 2)
})
})
// chance.countries()
test('countries() returns an array of countries', t => {
t.true(_.isArray(chance.countries()))
})
// chance.country()
test('country() returns a random (short) country name', t => {
_.times(1000, () => {
t.is(chance.country().length, 2)
})
})
test('country() returns a random (long) country name', t => {
_.times(1000, () => {
t.true(chance.country({ full: true }).length > 2)
})
})
// chance.county()
test('county() returns a random county name', t => {
_.times(1000, () => {
t.true(_.isString(chance.county()))
})
})
test('country() returns a random (long) country name', t => {
_.times(1000, () => {
t.true(chance.country({ full: true }).length > 2)
})
})
// chance.depth()
test('depth() looks right', t => {
t.is(typeof chance.depth(), 'number')
})
test('depth() is in the right range', t => {
_.times(1000, () => {
let depth = chance.depth()
t.true(depth > -10994)
t.true(depth < 0)
})
})
test('depth() will accept a min and obey it', t => {
_.times(1000, () => {
let min = chance.floating({ min: -10994, max: 0 })
let depth = chance.depth({ min: min })
t.true(depth > min)
t.true(depth < 0)
})
})
test('depth() will accept a max and obey it', t => {
_.times(1000, () => {
let max = chance.floating({ min: -10994, max: 0 })
let depth = chance.depth({ max: max })
t.true(depth > -10994)
t.true(depth < max)
})
})
// chance.geohash
test('geohash() looks right', t => {
let geohash = chance.geohash()
t.true(_.isString(geohash))
t.is(geohash.length, 7)
})
test('geohash() will accept a length and obey it', t => {
_.times(1000, () => {
let length = chance.d10()
let geohash = chance.geohash({ length: length })
t.is(geohash.length, length)
})
})
// chance.latitude()
test('latitude() looks right', t => {
t.is(typeof chance.latitude(), 'number')
})
test('latitude() is in the right range', t => {
_.times(1000, () => {
let latitude = chance.latitude()
t.true(latitude >= -90)
t.true(latitude <= 90)
})
})
test('latitude() will accept a min and obey it', t => {
_.times(1000, () => {
let min = chance.floating({ min: -90, max: 90 })
let latitude = chance.latitude({ min: min })
t.true(latitude >= min)
t.true(latitude <= 90)
})
})
test('latitude() will accept a max and obey it', t => {
_.times(1000, () => {
let max = chance.floating({ min: -90, max: 90 })
let latitude = chance.latitude({ max: max })
t.true(latitude >= -90)
t.true(latitude <= max)
})
})
test('latitude() returns latitude in DD format as default', t => {
_.times(1000, () => {
const CHARS_NOT_TO_CONTAIN = ['°', '’', '”']
let latitude = chance.latitude()
t.is(typeof latitude, 'number')
t.true(CHARS_NOT_TO_CONTAIN.every(char => !latitude.toString().includes(char)))
})
})
test('latitude() will obey DD format', t => {
_.times(1000, () => {
const CHARS_NOT_TO_CONTAIN = ['°', '’', '”']
let latitude = chance.latitude({format: 'dd'})
t.is(typeof latitude, 'number')
t.true(CHARS_NOT_TO_CONTAIN.every(char => !latitude.toString().includes(char)))
})
})
test('latitude() will obey DDM format', t => {
_.times(1000, () => {
const CHARS_TO_CONTAIN = ['°']
const CHARS_NOT_TO_CONTAIN = ['’', '”']
let latitude = chance.latitude({format: 'ddm'})
t.true(_.isString(latitude))
t.true(CHARS_TO_CONTAIN.every(char => latitude.includes(char)))
t.true(CHARS_NOT_TO_CONTAIN.every(char => !latitude.includes(char)))
})
})
test('latitude() will obey DMS format', t => {
_.times(1000, () => {
const CHARS_TO_CONTAIN = ['°', '’', '”']
let latitude = chance.latitude({format: 'dms'})
t.true(_.isString(latitude))
t.true(CHARS_TO_CONTAIN.every(char => latitude.includes(char)))
})
})
// chance.longitude()
test('longitude() looks right', t => {
t.is(typeof chance.longitude(), 'number')
})
test('longitude() is in the right range', t => {
_.times(1000, () => {
let longitude = chance.longitude()
t.true(longitude >= -180)
t.true(longitude <= 180)
})
})
test('longitude() will accept a min and obey it', t => {
_.times(1000, () => {
let min = chance.floating({ min: -180, max: 180 })
let longitude = chance.longitude({ min: min })
t.true(longitude >= min)
t.true(longitude <= 180)
})
})
test('longitude() will accept a max and obey it', t => {
_.times(1000, () => {
let max = chance.floating({ min: -180, max: 180 })
let longitude = chance.longitude({ max: max })
t.true(longitude >= -180)
t.true(longitude <= max)
})
})
test('longitude() returns longitude in DD format as default', t => {
_.times(1000, () => {
const CHARS_NOT_TO_CONTAIN = ['°', '’', '”']
let longitude = chance.longitude()
t.is(typeof longitude, 'number')
t.true(CHARS_NOT_TO_CONTAIN.every(char => !longitude.toString().includes(char)))
})
})
test('longitude() will obey DD format', t => {
_.times(1000, () => {
const CHARS_NOT_TO_CONTAIN = ['°', '’', '”']
let longitude = chance.longitude({format: 'dd'})
t.is(typeof longitude, 'number')
t.true(CHARS_NOT_TO_CONTAIN.every(char => !longitude.toString().includes(char)))
})
})
test('longitude() will obey DDM format', t => {
_.times(1000, () => {
const CHARS_TO_CONTAIN = ['°']
const CHARS_NOT_TO_CONTAIN = ['’', '”']
let longitude = chance.longitude({format: 'ddm'})
t.true(_.isString(longitude))
t.true(CHARS_TO_CONTAIN.every(char => longitude.includes(char)))
t.true(CHARS_NOT_TO_CONTAIN.every(char => !longitude.includes(char)))
})
})
test('longitude() will obey DMS format', t => {
_.times(1000, () => {
const CHARS_TO_CONTAIN = ['°', '’', '”']
let longitude = chance.longitude({format: 'dms'})
t.true(_.isString(longitude))
t.true(CHARS_TO_CONTAIN.every(char => longitude.includes(char)))
})
})
// chance.phone()
test('phone() returns a string', t => {
t.true(_.isString(chance.phone()))
})
test('phone() looks like an actual phone number', t => {
t.true(/^\(([2-9][0-8][0-9])\)?[\-. ]?([2-9][0-9]{2,2})[\-. ]?([0-9]{4,4})$/.test(chance.phone()))
})
test('phone() obeys formatted option', t => {
_.times(1000, () => {
let phone = chance.phone({ formatted: false })
t.true(_.isString(phone))
t.true(/^[2-9][0-8]\d[2-9]\d{6,6}$/.test(phone))
})
})
test('phone() obeys formatted option and parens option', t => {
_.times(1000, () => {
let phone = chance.phone({ formatted: false, parens: true })
t.true(_.isString(phone))
t.true(/^[2-9][0-8]\d[2-9]\d{6,6}$/.test(phone))
})
})
test('phone() with uk option works', t => {
t.true(_.isString(chance.phone({ country: 'uk' })))
})
test('phone() with uk option works and mobile option', t => {
t.true(_.isString(chance.phone({ country: 'uk', mobile: true })))
})
test('phone() with uk country looks right', t => {
t.true(phoneNumber.isValid(chance.phone({ country: 'uk' })))
})
test('phone() with uk country unformatted looks right', t => {
t.true(phoneNumber.isValid(phoneNumber.format(chance.phone({
country: 'uk',
formatted: false
}))))
})
test('phone() with uk country and mobile option looks right', t => {
_.times(1000, () => {
t.true(phoneNumber.isValid(chance.phone({
country: 'uk',
mobile: true
})))
})
})
test('phone() with uk country and mobile option unformatted looks right', t => {
_.times(1000, () => {
t.true(phoneNumber.isValid(phoneNumber.format(chance.phone({
country: 'uk',
mobile: true,
formatted: false
}))))
})
})
test('phone() with fr country works', t => {
t.true(_.isString(chance.phone({ country: 'fr' })))
})
test('phone() with fr country works with mobile option', t => {
t.true(_.isString(chance.phone({ country: 'fr', mobile: true })))
})
test('phone() with fr country looks right', t => {
_.times(1000, () => {
t.true(/0[123459] .. .. .. ../.test(chance.phone({ country: 'fr' })))
})
})
test('phone() with fr country looks right unformatted', t => {
_.times(1000, () => {
t.true(/0........./.test(chance.phone({
country: 'fr',
formatted: false
})))
})
})
test('phone() with fr country on mobile looks right', t => {
_.times(1000, () => {
t.true(/0[67] .. .. .. ../.test(chance.phone({
country: 'fr',
mobile: true
})))
})
})
test('phone() with fr country on mobile, unformatted looks right', t => {
_.times(1000, () => {
t.true(/0[67]......../.test(chance.phone({
country: 'fr',
mobile: true,
formatted: false
})))
})
})
test('phone() with br country option works', t => {
t.true(_.isString(chance.phone({ country: 'br' })))
})
test('phone() with br country and mobile option works', t => {
t.true(_.isString(chance.phone({ country: 'br', mobile: true })))
})
test('phone() with br country and formatted false option return a correct format', t => {
t.true(/([0-9]{2})([2-5]{1})([0-9]{3})([0-9]{4})/.test(chance.phone({
country: 'br',
mobile: false,
formatted: false
})))
})
test('phone() with br country, formatted false and mobile option return a correct format', t => {
t.true(/([0-9]{2})\9([0-9]{4})([0-9]{4})/.test(chance.phone({
country: 'br',
mobile: true,
formatted: false
})))
})
test('phone() with br country and formatted option apply the correct mask', t => {
t.true(/\(([0-9]{2})\) ([2-5]{1})([0-9]{3})\-([0-9]{4})/.test(chance.phone({
country: 'br',
mobile: false,
formatted: true
})))
})
test('phone() with br country, formatted and mobile option apply the correct mask', t => {
t.true(/\(([0-9]{2})\) 9([0-9]{4})\-([0-9]{4})/.test(chance.phone({
country: 'br',
mobile: true,
formatted: true
})))
})
// chance.postal()
test('postal() returns a valid basic postal code', t => {
_.times(1000, () => {
let postal = chance.postal()
t.is(postal.length, 7)
postal.split('').map((char) => {
t.is(char.toUpperCase(), char)
})
})
})
test('postcode() returns a valid basic postcode', t => {
_.times(10, () => {
let postcode = chance.postcode();
t.regex(postcode, /^[A-Z]{1,2}\d[A-Z\d]? \d[A-Z]{2}$/);
})
})
// chance.province()
test('province() returns a random (short) province name', t => {
_.times(1000, () => t.true(chance.province().length < 3))
})
test('province() can return a long random province name', t => {
_.times(1000, () => t.true(chance.province({ full: true }).length > 2))
})
test('province() can return a random long "it" province', t => {
_.times(1000, () => {
t.true(chance.province({country: 'it', full: true }).length > 2)
})
})
// chance.provinces()
test('provinces() returns an array of provinces', t => {
t.true(_.isArray(chance.provinces()))
})
test('provinces() supports internationalization', t => {
t.not(chance.provinces(), chance.provinces({ country: 'it' }))
})
// chance.state()
test('state() returns a random (short) state name', t => {
_.times(1000, () => t.true(chance.state().length < 3))
})
test('state() can take a country and return a state', t => {
_.times(1000, () => t.true(chance.state({ country: 'it' }).length === 3))
})
test('state() can return full state name', t => {
_.times(1000, () => {
t.true(chance.state({
full: true
}).length > 2)
})
})
test('state() with country returns a long state name', t => {
_.times(1000, () => {
t.true(chance.state({
country: 'it',
full: true
}).length > 2)
})
_.times(1000, () => {
t.true(chance.state({
country: 'uk',
full: true
}).length > 2)
})
})
// chance.states()
test('states() returns an array of states', t => {
t.true(_.isArray(chance.states()))
})
test('states() returns all 50 states and DC', t => {
t.is(chance.states().length, 51)
})
test('states() with territories returns 50 US states, DC, and 7 US territories', t => {
t.is(chance.states({
territories: true
}).length, 58)
})
test('states() without us states and dc returns 7 US territories', t => {
t.is(chance.states({
territories: true,
us_states_and_dc: false
}).length, 7)
})
test('states() with armed forces returns 50 states, DC, and 3 armed forces military states', t => {
t.is(chance.states({
armed_forces: true
}).length, 54)
})
test('states() with armed forces without states returns 3 armed forces states', t => {
t.is(chance.states({
armed_forces: true,
us_states_and_dc: false
}).length, 3)
})
test('states() with all options returns 61 items', t => {
t.is(chance.states({
territories: true,
armed_forces: true
}).length, 61)
})
test('states() without states returns 7 territories and 3 armed forces states', t => {
t.is(chance.states({
territories: true,
armed_forces: true,
us_states_and_dc: false
}).length, 10)
})
test('states() with country of "it" returns 20 regions', t => {
t.is(chance.states({
country: 'it'
}).length, 20)
})
test('states() with country of "uk" returns 129 UK counties', t => {
t.is(chance.states({
country: 'uk'
}).length, 129)
})
test('states() with country of "mx" returns 32 MX states', t => {
t.is(chance.states({
country: 'mx'
}).length, 32)
})
// chance.street()
test('street() works', t => {
_.times(100, () => t.is(typeof chance.street(), 'string'))
})
test('street() works with it country', t => {
_.times(100, () => t.is(typeof chance.street({ country: 'it' }), 'string'))
})
// chance.street_suffix()
test('street_suffix() returns a single suffix', t => {
_.times(1000, () => {
let suffix = chance.street_suffix()
t.is(typeof suffix, 'object')
t.is(typeof suffix.name, 'string')
t.is(typeof suffix.abbreviation, 'string')
})
})
// chance.street_suffixes()
test('street_suffixes() returns the suffix array', t => {
let suffixes = chance.street_suffixes()
t.true(_.isArray(suffixes))
suffixes.map((suffix) => {
t.truthy(suffix.name)
t.truthy(suffix.abbreviation)
})
})
test('street_suffixes() are short', t => {
let suffixes = chance.street_suffixes()
suffixes.map((suffix) => {
t.true(suffix.abbreviation.length < 5)
})
})
test('street_suffixes() are longish', t => {
let suffixes = chance.street_suffixes()
suffixes.map((suffix) => {
t.true(suffix.name.length > 2)
})
})
// chance.zip()
test('zip() returns a valid basic zip code', t => {
_.times(1000, () => {
let zip = chance.zip()
t.is(zip.length, 5)
t.true(/(^\d{5}$)|(^\d{5}-\d{4}$)/.test(zip))
})
})
test('zip() returns a valid zip+4 code', t => {
_.times(1000, () => {
let zip = chance.zip({ plusfour: true })
t.is(zip.length, 10)
t.true(/(^\d{5}$)|(^\d{5}-\d{4}$)/.test(zip))
})
})
|