forked from mkoryak/address-validator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.coffee
110 lines (89 loc) · 3.67 KB
/
test.coffee
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
validator = require('./index')
Address = validator.Address
_ = require('underscore')
q = []
validateAddress = (input, type) ->
q.push([input, type])
go = -> #this is prevents us from going over the rate limit
if q.length
[input, type] = q.pop()
inputAddress = new Address(input)
validator.validate(inputAddress, type, (err, validAddresses, inexactMatches) ->
console.log("input: #{inputAddress.toString()} -- type: #{type}")
if err
console.log(err)
else
console.log('address: ', _.map(validAddresses, (a) ->
"#{a.toString()} -- type: #{a.matchType}"
))
console.log('did you mean: ', _.map(inexactMatches, (a) ->
"#{a.toString()} -- type: #{a.matchType}"
))
console.log('\n')
)
setTimeout(go, 300)
address1 =
street: '100 North Washington St'
city: 'Boston'
state: 'MA'
country: 'US'
address2 =
street: '100 North Washington St'
city: 'Poop'
state: 'MA'
country: 'US'
address3 =
street: '1 Shitstorm St'
city: 'Boston'
state: 'MA'
country: 'US'
address4 =
street: '1 Main St'
city: 'San Diego'
state: 'CA'
country: 'US'
address5 =
street: '12 proctor rd'
state: 'Massachussetts'
country: 'US'
address6 =
street: '1 Main St'
city: 'San Diego'
country: 'US'
console.log("********* There is a 300ms pause between requests as not to go over the API rate limit *********\n\n")
validator.setOptions(
countryMatch: "us" #all results must be from the US.
)
validateAddress(address1, validator.match.unknown)
validateAddress(address2, validator.match.unknown)
validateAddress(address3, validator.match.unknown)
validateAddress(address4, validator.match.unknown)
validateAddress(address5, validator.match.unknown)
validateAddress(address6, validator.match.unknown)
validateAddress('100 north washington st, bostont', validator.match.unknown);
validateAddress('100 North Washington Street, Boston, MA, US', validator.match.unknown);
validateAddress('100 north washington st, boston, ma, us', validator.match.unknown);
validateAddress('100 N washington st, boston, ma, us', validator.match.unknown);
validateAddress('12 proctor rd townsend, Mass', validator.match.unknown)
validateAddress('Boston, MA', validator.match.unknown)
validateAddress('Boston, MA, USA', validator.match.unknown)
validateAddress('MA', validator.match.unknown)
validateAddress('Sibirskaya 22, Novosibirks, Russia', validator.match.unknown)
validateAddress('115 Mercury House Willoughton Drive Gainsborough DN21 3DF, UK', validator.match.unknown)
## now lets mix it up by giving the validator info about the type of an address we asked to validate:
validateAddress(address1, validator.match.streetAddress)
validateAddress(address2, validator.match.streetAddress)
validateAddress(address3, validator.match.streetAddress)
validateAddress(address4, validator.match.streetAddress)
validateAddress(address5, validator.match.streetAddress)
validateAddress(address6, validator.match.streetAddress)
validateAddress('100 north washington st, bostont', validator.match.streetAddress);
validateAddress('100 North Washington Street, Boston, MA, US', validator.match.streetAddress);
validateAddress('100 north washington st, boston, ma, us', validator.match.city);
validateAddress('100 N washington st, boston, ma, us', validator.match.streetAddress);
validateAddress('12 proctor rd townsend, Mass', validator.match.streetAddress)
validateAddress('Boston, MA', validator.match.city)
validateAddress('Boston, MA, USA', validator.match.city)
validateAddress('US', validator.match.country)
validateAddress('Sibirskaya 22, Novosibirks, Russia', validator.match.streetAddress)
go()