/
postal-address.js
258 lines (212 loc) · 6.02 KB
/
postal-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
// Import modules and constants
import allAddressFormats from './address-formats'
import allAddressParsers from './address-parsers'
import countries from './countries'
import objectInitialState from './object-initial-state'
class PostalAddress {
constructor() {
// Possible values: 'array', 'string'
this.outputFormat = 'array'
// 2 letter country code
this.formatForCountry = 'US'
// Possible values: 'business', 'english', 'default', 'french', 'personal'
this.formatForType = 'default'
// Transform input data or keep it as is
this.useTransforms = true
// The object properties that can be set
this.object = { ...objectInitialState }
// Validator functions
this.validators = {
formatForCountry: value => this.allowed.formatForCountry.includes(value),
formatForType: value => this.allowed.formatForType.includes(value),
outputFormat: value => this.allowed.outputFormat.includes(value),
}
// Allowed values
this.allowed = {
formatForCountry: Object.keys(allAddressFormats),
formatForType: ['business', 'default', 'english', 'french', 'personal'],
outputFormat: ['array', 'string'],
}
// Address formats
this.addressFormats = allAddressFormats
// Parsers
this.addressParsers = allAddressParsers
}
validator(property, newValue, object = true) {
let oldValue = ''
if (object) {
oldValue = this.object[property]
} else {
oldValue = this[property]
}
const validatorFn = this.validators[property]
if (typeof validatorFn === 'function') {
if (validatorFn(newValue)) {
return newValue
}
return oldValue
}
return newValue
}
setProperty(property, newValue, object = true) {
if (typeof newValue === 'string') {
if (object) {
if (typeof this.object[property] === 'string') {
this.object[property] = this.validator(property, newValue, object)
}
} else if (typeof this[property] === 'string') {
this[property] = this.validator(property, newValue, object)
}
}
}
setAddress(newValue) {
this.setAddress1(newValue)
}
setAddress1(newValue) {
this.setProperty('address1', newValue)
this.setProperty('dong', newValue)
}
setAddress2(newValue) {
this.setProperty('address2', newValue)
}
setAddressNum(newValue) {
this.setProperty('addressNum', newValue)
}
setCity(newValue) {
this.setProperty('city', newValue)
this.setProperty('si', newValue)
}
setCompanyName(newValue) {
this.setProperty('companyName', newValue)
}
setCountry(newValue) {
this.setProperty('country', newValue)
const countryAlpha2 = countries[newValue]
if (countryAlpha2) {
this.setProperty('countryAlpha2', countryAlpha2)
}
}
setDo(newValue) {
this.setProperty('do', newValue)
this.setProperty('province', newValue)
}
setDong(newValue) {
this.setProperty('dong', newValue)
this.setProperty('address1', newValue)
}
setFirstLastName(newValue) {
this.setProperty('firstLastName', newValue)
}
setFirstName(newValue) {
this.setProperty('firstName', newValue)
}
setGu(newValue) {
this.setProperty('gu', newValue)
}
setHonorific(newValue) {
this.setProperty('honorific', newValue)
}
setJobTitle(newValue) {
this.setProperty('jobTitle', newValue)
}
setLastName(newValue) {
this.setProperty('lastName', newValue)
this.setProperty('secondLastName', newValue)
}
setPostalCode(newValue) {
this.setProperty('postalCode', newValue)
}
setPrefecture(newValue) {
this.setProperty('prefecture', newValue)
}
setProvince(newValue) {
this.setProperty('province', newValue)
this.setProperty('do', newValue)
}
setRegion(newValue) {
this.setProperty('region', newValue)
}
setRepublic(newValue) {
this.setProperty('republic', newValue)
}
setSecondLastName(newValue) {
this.setProperty('secondLastName', newValue)
this.setProperty('lastName', newValue)
}
setSecondName(newValue) {
this.setProperty('secondName', newValue)
}
setSi(newValue) {
this.setProperty('si', newValue)
this.setProperty('city', newValue)
}
setState(newValue) {
this.setProperty('state', newValue)
}
setTitle(newValue) {
this.setProperty('title', newValue)
}
setOutputFormat(string) {
this.setProperty('outputFormat', string, false)
}
setFormat({ country, type }) {
this.setProperty('formatForCountry', country, false)
this.setProperty('formatForType', type, false)
}
setUseTransforms(boolean) {
if (typeof boolean === 'boolean') {
this.useTransforms = boolean
}
}
getFormat(overrideFormat) {
const {
outputFormat, formatForCountry, formatForType, addressFormats,
} = this
const format = overrideFormat || outputFormat
let formatsAvailable = addressFormats[formatForCountry]
if (!formatsAvailable) {
// Default to the US format
formatsAvailable = addressFormats.US
}
let outputType = {}
if (formatsAvailable[formatForType]) {
outputType = formatsAvailable[formatForType]
} else if (formatsAvailable.default) {
outputType = formatsAvailable.default
}
if (outputType[format]) {
return outputType[format]
} else if (outputType.array) {
return outputType.array
}
return null
}
getParser(overrideFormat) {
const { outputFormat, addressParsers } = this
const format = overrideFormat || outputFormat
if (addressParsers[format]) {
return addressParsers[format]
}
return null
}
output(overrideFormat) {
const { useTransforms } = this
const outputFormat = this.getFormat(overrideFormat)
const outputParser = this.getParser(overrideFormat)
if (typeof outputParser === 'function' && outputFormat) {
return outputParser(this.object, outputFormat, useTransforms)
}
return null
}
toString() {
const output = this.output('array')
if (output) {
return output.map(part => part.join(' ')).join('\n')
}
return null
}
raw() {
return this.object
}
}
export default PostalAddress