-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathindex.js
More file actions
143 lines (131 loc) · 4.25 KB
/
index.js
File metadata and controls
143 lines (131 loc) · 4.25 KB
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
module.exports = explodeAddress
module.exports.explodeAddress = explodeAddress
module.exports.implodeAddress = implodeAddress
function explodeAddress(singleLineAddress,cb){
process.nextTick(function(){
var addressObj = {
street_address1: null
,city: null
,state: null
,postal_code: null
,country: null
}
if (typeof singleLineAddress != 'string') {
//return cb(new Error('Input must be a String'))
return cb(false,addressObj)
}
singleLineAddress = singleLineAddress.trim()
var postalCode = singleLineAddress.match(/([0-9]{5})|([a-z][0-9][a-z] ?[0-9][a-z][0-9])/gi)
,indexOfPostalCode = -1
if (postalCode) {
postalCode = postalCode.pop() // pick match closest to end
indexOfPostalCode = singleLineAddress.lastIndexOf(postalCode)
if (indexOfPostalCode == 0 && singleLineAddress.length > 10) {
// postal code is probably part of street address
postalCode = null
indexOfPostalCode = -1
}
if (postalCode) {
addressObj.postal_code = postalCode
var everythingAfterPostalCode = singleLineAddress.substr(indexOfPostalCode+postalCode.length)
singleLineAddress = singleLineAddress.substr(0,indexOfPostalCode)+everythingAfterPostalCode
var possibleCountry = everythingAfterPostalCode.replace(/\s*,/,'').split(',').shift().trim()
if (possibleCountry && looksLikeCountry(possibleCountry)) {
addressObj.country = possibleCountry
singleLineAddress = singleLineAddress.substr(0,indexOfPostalCode) // just ditch everything after postal + country
}
}
}
var addySplit = singleLineAddress.split(',')
// Handle special cases...
// Neighborhood, City, State
if (addySplit.length == 3 && looksLikeState(addySplit[2])) {
addressObj.street_address1 = addySplit[0].trim()
addressObj.city = addySplit[1].trim()
addressObj.state = addySplit[2].trim()
return cb(false,addressObj)
}
// Handle generic case...
addySplit.forEach(function(addyPart){
if (!(addyPart = addyPart.trim())) return
// if has numbers, assume street address
if (/[0-9]/.test(addyPart)) {
return !addressObj.street_address1 && (addressObj.street_address1 = addyPart)
}
// if looks like state
if (looksLikeState(addyPart) && !addressObj.state) {
return addressObj.state = addyPart
}
// if looks like country
if (looksLikeCountry(addyPart)) {
return !addressObj.country && (addressObj.country = addyPart)
}
// else assume city
!addressObj.city && (addressObj.city = addyPart)
})
cb(false,addressObj)
})
}
function implodeAddress(addressObj,cb){
process.nextTick(function(){
if (addressObj === null || typeof addressObj != 'object') {
//return cb(new Error('Input must be an Object'))
return cb(false, '')
}
var addyParts = []
,addyPart
if (typeof addressObj.street_address1 == 'string' && (addyPart = addressObj.street_address1.trim())) {
addyParts[0] = addyPart
if (typeof addressObj.street_address2 == 'string' && (addyPart = addressObj.street_address2.trim())) {
addyParts[0] += ' '+addyPart
}
}
['city','state'].forEach(function(addyKey){
if (typeof addressObj[addyKey] == 'string' && (addyPart = addressObj[addyKey].trim())) {
addyParts.push(addyPart)
}
})
var singleLineAddress = addyParts.join(', ')
if (typeof addressObj.postal_code == 'string' && (addyPart = addressObj.postal_code.trim())) {
singleLineAddress += ' '+addyPart
singleLineAddress = singleLineAddress.trim()
}
if (typeof addressObj.country == 'string' && (addyPart = addressObj.country.trim())) {
singleLineAddress += singleLineAddress ? ', '+addyPart : addyPart
}
cb(false,singleLineAddress)
})
}
var states
function looksLikeState(str){
if (!states) {
var map = require('./lib/states.json')
states = {}
for (var k in map) {
if (map.hasOwnProperty(k)){
states[k.toLowerCase()] = true
states[map[k].toLowerCase()] = true
}
}
}
str = str.trim().toLowerCase()
return !!states[str]
}
var countries
function looksLikeCountry(str){
if (!countries) {
var map = require('./lib/countries.json')
countries = {}
for (var k in map) {
if (map.hasOwnProperty(k)){
countries[k.toLowerCase()] = true
countries[map[k].toLowerCase()] = true
}
}
}
str = str.trim().toLowerCase()
if (str == 'usa') {
return true
}
return !!countries[str]
}