@bcrs-shared-components/base-address 3.0.2 → 3.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/BaseAddress.stories.ts +52 -53
- package/BaseAddress.vue +389 -381
- package/LICENSE +201 -201
- package/factories/address-factory.ts +216 -216
- package/factories/countries-provinces-factory.ts +78 -78
- package/factories/index.ts +3 -3
- package/factories/validation-factory.ts +42 -42
- package/factories/vuelidate-validation-factory.ts +118 -118
- package/index.ts +1 -1
- package/package.json +2 -2
- package/resources/default-schema.ts +42 -42
- package/resources/index.ts +1 -1
|
@@ -1,216 +1,216 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
import { computed, reactive, ref, Ref } from 'vue-demi'
|
|
3
|
-
import { uniqueId } from 'lodash'
|
|
4
|
-
|
|
5
|
-
import { AddressIF, SchemaIF } from '@bcrs-shared-components/interfaces'
|
|
6
|
-
|
|
7
|
-
export function useAddress (address: Ref<AddressIF>, schema: SchemaIF) {
|
|
8
|
-
const addressLocal = address
|
|
9
|
-
/** The Address Country, to simplify the template and so we can watch it directly. */
|
|
10
|
-
const country = computed((): string => {
|
|
11
|
-
return addressLocal.value.
|
|
12
|
-
})
|
|
13
|
-
const schemaLocal = ref(schema)
|
|
14
|
-
const isSchemaRequired = (prop: string): boolean => {
|
|
15
|
-
if (!schemaLocal || !schemaLocal.value[prop]) return false
|
|
16
|
-
|
|
17
|
-
// check for any rule that does not allow an empty string
|
|
18
|
-
for (let index in schemaLocal.value[prop]) {
|
|
19
|
-
if (schemaLocal.value[prop][index]('') !== true) return true
|
|
20
|
-
}
|
|
21
|
-
return false
|
|
22
|
-
}
|
|
23
|
-
const labels = {
|
|
24
|
-
/** The Street Address Additional label with 'optional' as needed. */
|
|
25
|
-
streetAdditionalLabel: computed((): string => {
|
|
26
|
-
return 'Address Line 2' + (isSchemaRequired('streetAdditional') ? '' : ' (Optional)')
|
|
27
|
-
}),
|
|
28
|
-
/** The Street Address label with 'optional' as needed. */
|
|
29
|
-
streetLabel: computed((): string => {
|
|
30
|
-
return 'Address Line 1' + (isSchemaRequired('street') ? '' : ' (Optional)')
|
|
31
|
-
}),
|
|
32
|
-
/** The Address City label with 'optional' as needed. */
|
|
33
|
-
cityLabel: computed((): string => {
|
|
34
|
-
return 'City' + (isSchemaRequired('city') ? '' : ' (Optional)')
|
|
35
|
-
}),
|
|
36
|
-
/** The Address Region label with 'optional' as needed. */
|
|
37
|
-
regionLabel: computed((): string => {
|
|
38
|
-
let label: string
|
|
39
|
-
let required = isSchemaRequired('region')
|
|
40
|
-
|
|
41
|
-
// NB: make region required for Canada and USA
|
|
42
|
-
if (addressLocal.value.
|
|
43
|
-
label = 'Province'
|
|
44
|
-
required = true
|
|
45
|
-
} else if (addressLocal.value.
|
|
46
|
-
label = 'State'
|
|
47
|
-
required = true
|
|
48
|
-
} else {
|
|
49
|
-
label = 'Region'
|
|
50
|
-
}
|
|
51
|
-
return label + (required ? '' : ' (Optional)')
|
|
52
|
-
}),
|
|
53
|
-
/** The Postal Code label with 'optional' as needed. */
|
|
54
|
-
postalCodeLabel: computed((): string => {
|
|
55
|
-
let label: string
|
|
56
|
-
if (addressLocal.value.
|
|
57
|
-
label = 'Zip Code'
|
|
58
|
-
} else {
|
|
59
|
-
label = 'Postal Code'
|
|
60
|
-
}
|
|
61
|
-
return label + (isSchemaRequired('postalCode') ? '' : ' (Optional)')
|
|
62
|
-
}),
|
|
63
|
-
/** The Address Country label with 'optional' as needed. */
|
|
64
|
-
countryLabel: computed((): string => {
|
|
65
|
-
return 'Country' + (isSchemaRequired('country') ? '' : ' (Optional)')
|
|
66
|
-
}),
|
|
67
|
-
/** The Delivery Instructions label with 'optional' as needed. */
|
|
68
|
-
deliveryInstructionsLabel: computed((): string => {
|
|
69
|
-
return 'Delivery Instructions' + (isSchemaRequired('deliveryInstructions') ? '' : ' (Optional)')
|
|
70
|
-
})
|
|
71
|
-
}
|
|
72
|
-
return {
|
|
73
|
-
addressLocal,
|
|
74
|
-
country,
|
|
75
|
-
schemaLocal,
|
|
76
|
-
isSchemaRequired,
|
|
77
|
-
labels
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export function useAddressComplete (addressLocal: Ref<AddressIF>) {
|
|
82
|
-
const combineLines = (line1: string, line2: string) => {
|
|
83
|
-
if (!line1) return line2
|
|
84
|
-
if (!line2) return line1
|
|
85
|
-
return line1 + '\n' + line2
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Callback to update the address data after the user chooses a suggested address.
|
|
89
|
-
* @param address the data object returned by the AddressComplete Retrieve API
|
|
90
|
-
*/
|
|
91
|
-
const addressCompletePopulate = (addressComplete: object): void => {
|
|
92
|
-
addressLocal.value.streetAddress = addressComplete['Line1'] || 'N/A'
|
|
93
|
-
// Combine extra address lines into Street Address Additional field.
|
|
94
|
-
addressLocal.value.streetAddressAdditional = combineLines(
|
|
95
|
-
combineLines(addressComplete['Line2'], addressComplete['Line3']),
|
|
96
|
-
combineLines(addressComplete['Line4'], addressComplete['Line5'])
|
|
97
|
-
)
|
|
98
|
-
addressLocal.value.
|
|
99
|
-
if (useCountryRegions(addressComplete['CountryIso2'])) {
|
|
100
|
-
// In this case, v-select will map known province code to province name
|
|
101
|
-
// or v-select will be blank and user will have to select a known item.
|
|
102
|
-
addressLocal.value.
|
|
103
|
-
} else {
|
|
104
|
-
// In this case, v-text-input will allow manual entry but province info is probably too long
|
|
105
|
-
// so set region to null and add province name to the Street Address Additional field.
|
|
106
|
-
// If length is excessive, user will have to fix it.
|
|
107
|
-
addressLocal.value.
|
|
108
|
-
addressLocal.value.
|
|
109
|
-
addressLocal.value.
|
|
110
|
-
)
|
|
111
|
-
}
|
|
112
|
-
addressLocal.value.postalCode = addressComplete['PostalCode']
|
|
113
|
-
addressLocal.value.
|
|
114
|
-
}
|
|
115
|
-
const uniqueIds = reactive({
|
|
116
|
-
/** A unique id for this instance of this component. */
|
|
117
|
-
uniqueId: uniqueId(),
|
|
118
|
-
/** A unique id for the Street Address input. */
|
|
119
|
-
streetId: computed((): string => {
|
|
120
|
-
return `street-address-${uniqueIds.uniqueId}`
|
|
121
|
-
}),
|
|
122
|
-
/** A unique id for the Address Country input. */
|
|
123
|
-
countryId: computed((): string => {
|
|
124
|
-
return `address-country-${uniqueIds.uniqueId}`
|
|
125
|
-
})
|
|
126
|
-
})
|
|
127
|
-
/**
|
|
128
|
-
* Creates the AddressComplete object for this instance of the component.
|
|
129
|
-
* @param pca the Postal Code Anywhere object provided by AddressComplete
|
|
130
|
-
* @param key the key for the Canada Post account that is to be charged for lookups
|
|
131
|
-
* @returns an object that is a pca.Address instance
|
|
132
|
-
*/
|
|
133
|
-
const createAddressComplete = (pca: any, key: string): object => {
|
|
134
|
-
// Set up the two fields that AddressComplete will use for input.
|
|
135
|
-
// Ref: https://www.canadapost.ca/pca/support/guides/advanced
|
|
136
|
-
// Note: Use special field for country, which user can't click, and which AC will overwrite
|
|
137
|
-
// but that we don't care about.
|
|
138
|
-
const fields = [
|
|
139
|
-
{ element: uniqueIds.streetId, field: 'Line1', mode: pca.fieldMode.SEARCH },
|
|
140
|
-
{ element: uniqueIds.countryId, field: 'CountryName', mode: pca.fieldMode.COUNTRY }
|
|
141
|
-
]
|
|
142
|
-
const options = { key }
|
|
143
|
-
|
|
144
|
-
const addressComplete = new pca.Address(fields, options)
|
|
145
|
-
|
|
146
|
-
// The documentation contains sample load/populate callback code that doesn't work, but this will. The side effect
|
|
147
|
-
// is that it breaks the autofill functionality provided by the library, but we really don't want the library
|
|
148
|
-
// altering the DOM because Vue is already doing so, and the two don't play well together.
|
|
149
|
-
addressComplete.listen('populate', addressCompletePopulate)
|
|
150
|
-
|
|
151
|
-
return addressComplete
|
|
152
|
-
}
|
|
153
|
-
/** Enables AddressComplete for this instance of the address. */
|
|
154
|
-
const enableAddressComplete = (): void => {
|
|
155
|
-
// If you want to use this component with the Canada Post AddressComplete service:
|
|
156
|
-
// 1. The AddressComplete JavaScript script (and stylesheet) must be loaded.
|
|
157
|
-
// 2. Your AddressComplete account key must be defined.
|
|
158
|
-
const pca = window['pca']
|
|
159
|
-
const key = window['addressCompleteKey']
|
|
160
|
-
if (!pca || !key) {
|
|
161
|
-
// eslint-disable-next-line no-console
|
|
162
|
-
console.log('AddressComplete not initialized due to missing script and/or key')
|
|
163
|
-
return
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// Destroy the old object if it exists, and create a new one.
|
|
167
|
-
if (window['currentAddressComplete']) {
|
|
168
|
-
window['currentAddressComplete'].destroy()
|
|
169
|
-
}
|
|
170
|
-
window['currentAddressComplete'] = createAddressComplete(pca, key)
|
|
171
|
-
}
|
|
172
|
-
return {
|
|
173
|
-
addressCompletePopulate,
|
|
174
|
-
createAddressComplete,
|
|
175
|
-
enableAddressComplete,
|
|
176
|
-
uniqueIds
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Determines whether to use a country's known regions (ie, provinces/states).
|
|
182
|
-
* @param code the short code of the country
|
|
183
|
-
* @returns whether to use v-select (true) or v-text-field (false) for input
|
|
184
|
-
*/
|
|
185
|
-
export function useCountryRegions (code: string): boolean {
|
|
186
|
-
return (code === 'CA' || code === 'US')
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
export function formatAddress (address: AddressIF): AddressIF {
|
|
190
|
-
let formattedPostalCode = address.postalCode?.toUpperCase() || ''
|
|
191
|
-
if (address.addressCountry === 'CA') {
|
|
192
|
-
formattedPostalCode = address.postalCode.replace('-', ' ')
|
|
193
|
-
if (address.postalCode.length > 4 && address.postalCode[3] !== ' ') {
|
|
194
|
-
formattedPostalCode = address.postalCode.slice(0, 3) + ' ' + address.postalCode.slice(3,)
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
return {
|
|
198
|
-
addressCity: address.addressCity?.trim(),
|
|
199
|
-
addressCountry: address.addressCountry?.trim(),
|
|
200
|
-
addressRegion: address.addressRegion?.trim(),
|
|
201
|
-
addressType: address.addressType?.trim(),
|
|
202
|
-
deliveryInstructions: address.deliveryInstruction?.trim(),
|
|
203
|
-
postalCode: formattedPostalCode.trim(),
|
|
204
|
-
streetAddress: address.streetAddress?.trim(),
|
|
205
|
-
streetAddressAdditional: address.streetAddressAdditional?.trim()
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
export function checkAddress (address: AddressIF, schema: SchemaIF): AddressIF {
|
|
210
|
-
for(var addressProperty in schema) {
|
|
211
|
-
if (!address[addressProperty]) {
|
|
212
|
-
address[addressProperty] = ''
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
return address
|
|
216
|
-
}
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
import { computed, reactive, ref, Ref } from 'vue-demi'
|
|
3
|
+
import { uniqueId } from 'lodash'
|
|
4
|
+
|
|
5
|
+
import { AddressIF, SchemaIF } from '@bcrs-shared-components/interfaces'
|
|
6
|
+
|
|
7
|
+
export function useAddress (address: Ref<AddressIF>, schema: SchemaIF) {
|
|
8
|
+
const addressLocal = address
|
|
9
|
+
/** The Address Country, to simplify the template and so we can watch it directly. */
|
|
10
|
+
const country = computed((): string => {
|
|
11
|
+
return addressLocal.value.addressCountry
|
|
12
|
+
})
|
|
13
|
+
const schemaLocal = ref(schema)
|
|
14
|
+
const isSchemaRequired = (prop: string): boolean => {
|
|
15
|
+
if (!schemaLocal || !schemaLocal.value[prop]) return false
|
|
16
|
+
|
|
17
|
+
// check for any rule that does not allow an empty string
|
|
18
|
+
for (let index in schemaLocal.value[prop]) {
|
|
19
|
+
if (schemaLocal.value[prop][index]('') !== true) return true
|
|
20
|
+
}
|
|
21
|
+
return false
|
|
22
|
+
}
|
|
23
|
+
const labels = {
|
|
24
|
+
/** The Street Address Additional label with 'optional' as needed. */
|
|
25
|
+
streetAdditionalLabel: computed((): string => {
|
|
26
|
+
return 'Address Line 2' + (isSchemaRequired('streetAdditional') ? '' : ' (Optional)')
|
|
27
|
+
}),
|
|
28
|
+
/** The Street Address label with 'optional' as needed. */
|
|
29
|
+
streetLabel: computed((): string => {
|
|
30
|
+
return 'Address Line 1' + (isSchemaRequired('street') ? '' : ' (Optional)')
|
|
31
|
+
}),
|
|
32
|
+
/** The Address City label with 'optional' as needed. */
|
|
33
|
+
cityLabel: computed((): string => {
|
|
34
|
+
return 'City' + (isSchemaRequired('city') ? '' : ' (Optional)')
|
|
35
|
+
}),
|
|
36
|
+
/** The Address Region label with 'optional' as needed. */
|
|
37
|
+
regionLabel: computed((): string => {
|
|
38
|
+
let label: string
|
|
39
|
+
let required = isSchemaRequired('region')
|
|
40
|
+
|
|
41
|
+
// NB: make region required for Canada and USA
|
|
42
|
+
if (addressLocal.value.addressCountry === 'CA') {
|
|
43
|
+
label = 'Province'
|
|
44
|
+
required = true
|
|
45
|
+
} else if (addressLocal.value.addressCountry === 'US') {
|
|
46
|
+
label = 'State'
|
|
47
|
+
required = true
|
|
48
|
+
} else {
|
|
49
|
+
label = 'Region'
|
|
50
|
+
}
|
|
51
|
+
return label + (required ? '' : ' (Optional)')
|
|
52
|
+
}),
|
|
53
|
+
/** The Postal Code label with 'optional' as needed. */
|
|
54
|
+
postalCodeLabel: computed((): string => {
|
|
55
|
+
let label: string
|
|
56
|
+
if (addressLocal.value.addressCountry === 'US') {
|
|
57
|
+
label = 'Zip Code'
|
|
58
|
+
} else {
|
|
59
|
+
label = 'Postal Code'
|
|
60
|
+
}
|
|
61
|
+
return label + (isSchemaRequired('postalCode') ? '' : ' (Optional)')
|
|
62
|
+
}),
|
|
63
|
+
/** The Address Country label with 'optional' as needed. */
|
|
64
|
+
countryLabel: computed((): string => {
|
|
65
|
+
return 'Country' + (isSchemaRequired('country') ? '' : ' (Optional)')
|
|
66
|
+
}),
|
|
67
|
+
/** The Delivery Instructions label with 'optional' as needed. */
|
|
68
|
+
deliveryInstructionsLabel: computed((): string => {
|
|
69
|
+
return 'Delivery Instructions' + (isSchemaRequired('deliveryInstructions') ? '' : ' (Optional)')
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
addressLocal,
|
|
74
|
+
country,
|
|
75
|
+
schemaLocal,
|
|
76
|
+
isSchemaRequired,
|
|
77
|
+
labels
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function useAddressComplete (addressLocal: Ref<AddressIF>) {
|
|
82
|
+
const combineLines = (line1: string, line2: string) => {
|
|
83
|
+
if (!line1) return line2
|
|
84
|
+
if (!line2) return line1
|
|
85
|
+
return line1 + '\n' + line2
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Callback to update the address data after the user chooses a suggested address.
|
|
89
|
+
* @param address the data object returned by the AddressComplete Retrieve API
|
|
90
|
+
*/
|
|
91
|
+
const addressCompletePopulate = (addressComplete: object): void => {
|
|
92
|
+
addressLocal.value.streetAddress = addressComplete['Line1'] || 'N/A'
|
|
93
|
+
// Combine extra address lines into Street Address Additional field.
|
|
94
|
+
addressLocal.value.streetAddressAdditional = combineLines(
|
|
95
|
+
combineLines(addressComplete['Line2'], addressComplete['Line3']),
|
|
96
|
+
combineLines(addressComplete['Line4'], addressComplete['Line5'])
|
|
97
|
+
)
|
|
98
|
+
addressLocal.value.addressCity = addressComplete['City']
|
|
99
|
+
if (useCountryRegions(addressComplete['CountryIso2'])) {
|
|
100
|
+
// In this case, v-select will map known province code to province name
|
|
101
|
+
// or v-select will be blank and user will have to select a known item.
|
|
102
|
+
addressLocal.value.addressRegion = addressComplete['ProvinceCode']
|
|
103
|
+
} else {
|
|
104
|
+
// In this case, v-text-input will allow manual entry but province info is probably too long
|
|
105
|
+
// so set region to null and add province name to the Street Address Additional field.
|
|
106
|
+
// If length is excessive, user will have to fix it.
|
|
107
|
+
addressLocal.value.addressRegion = ''
|
|
108
|
+
addressLocal.value.streetAddressAdditional = combineLines(
|
|
109
|
+
addressLocal.value.streetAddressAdditional, addressComplete['ProvinceName']
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
addressLocal.value.postalCode = addressComplete['PostalCode']
|
|
113
|
+
addressLocal.value.addressCountry = addressComplete['CountryIso2']
|
|
114
|
+
}
|
|
115
|
+
const uniqueIds = reactive({
|
|
116
|
+
/** A unique id for this instance of this component. */
|
|
117
|
+
uniqueId: uniqueId(),
|
|
118
|
+
/** A unique id for the Street Address input. */
|
|
119
|
+
streetId: computed((): string => {
|
|
120
|
+
return `street-address-${uniqueIds.uniqueId}`
|
|
121
|
+
}),
|
|
122
|
+
/** A unique id for the Address Country input. */
|
|
123
|
+
countryId: computed((): string => {
|
|
124
|
+
return `address-country-${uniqueIds.uniqueId}`
|
|
125
|
+
})
|
|
126
|
+
})
|
|
127
|
+
/**
|
|
128
|
+
* Creates the AddressComplete object for this instance of the component.
|
|
129
|
+
* @param pca the Postal Code Anywhere object provided by AddressComplete
|
|
130
|
+
* @param key the key for the Canada Post account that is to be charged for lookups
|
|
131
|
+
* @returns an object that is a pca.Address instance
|
|
132
|
+
*/
|
|
133
|
+
const createAddressComplete = (pca: any, key: string): object => {
|
|
134
|
+
// Set up the two fields that AddressComplete will use for input.
|
|
135
|
+
// Ref: https://www.canadapost.ca/pca/support/guides/advanced
|
|
136
|
+
// Note: Use special field for country, which user can't click, and which AC will overwrite
|
|
137
|
+
// but that we don't care about.
|
|
138
|
+
const fields = [
|
|
139
|
+
{ element: uniqueIds.streetId, field: 'Line1', mode: pca.fieldMode.SEARCH },
|
|
140
|
+
{ element: uniqueIds.countryId, field: 'CountryName', mode: pca.fieldMode.COUNTRY }
|
|
141
|
+
]
|
|
142
|
+
const options = { key }
|
|
143
|
+
|
|
144
|
+
const addressComplete = new pca.Address(fields, options)
|
|
145
|
+
|
|
146
|
+
// The documentation contains sample load/populate callback code that doesn't work, but this will. The side effect
|
|
147
|
+
// is that it breaks the autofill functionality provided by the library, but we really don't want the library
|
|
148
|
+
// altering the DOM because Vue is already doing so, and the two don't play well together.
|
|
149
|
+
addressComplete.listen('populate', addressCompletePopulate)
|
|
150
|
+
|
|
151
|
+
return addressComplete
|
|
152
|
+
}
|
|
153
|
+
/** Enables AddressComplete for this instance of the address. */
|
|
154
|
+
const enableAddressComplete = (): void => {
|
|
155
|
+
// If you want to use this component with the Canada Post AddressComplete service:
|
|
156
|
+
// 1. The AddressComplete JavaScript script (and stylesheet) must be loaded.
|
|
157
|
+
// 2. Your AddressComplete account key must be defined.
|
|
158
|
+
const pca = window['pca']
|
|
159
|
+
const key = window['addressCompleteKey']
|
|
160
|
+
if (!pca || !key) {
|
|
161
|
+
// eslint-disable-next-line no-console
|
|
162
|
+
console.log('AddressComplete not initialized due to missing script and/or key')
|
|
163
|
+
return
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Destroy the old object if it exists, and create a new one.
|
|
167
|
+
if (window['currentAddressComplete']) {
|
|
168
|
+
window['currentAddressComplete'].destroy()
|
|
169
|
+
}
|
|
170
|
+
window['currentAddressComplete'] = createAddressComplete(pca, key)
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
addressCompletePopulate,
|
|
174
|
+
createAddressComplete,
|
|
175
|
+
enableAddressComplete,
|
|
176
|
+
uniqueIds
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Determines whether to use a country's known regions (ie, provinces/states).
|
|
182
|
+
* @param code the short code of the country
|
|
183
|
+
* @returns whether to use v-select (true) or v-text-field (false) for input
|
|
184
|
+
*/
|
|
185
|
+
export function useCountryRegions (code: string): boolean {
|
|
186
|
+
return (code === 'CA' || code === 'US')
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function formatAddress (address: AddressIF): AddressIF {
|
|
190
|
+
let formattedPostalCode = address.postalCode?.toUpperCase() || ''
|
|
191
|
+
if (address.addressCountry === 'CA') {
|
|
192
|
+
formattedPostalCode = address.postalCode.replace('-', ' ')
|
|
193
|
+
if (address.postalCode.length > 4 && address.postalCode[3] !== ' ') {
|
|
194
|
+
formattedPostalCode = address.postalCode.slice(0, 3) + ' ' + address.postalCode.slice(3,)
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
addressCity: address.addressCity?.trim(),
|
|
199
|
+
addressCountry: address.addressCountry?.trim(),
|
|
200
|
+
addressRegion: address.addressRegion?.trim(),
|
|
201
|
+
addressType: address.addressType?.trim(),
|
|
202
|
+
deliveryInstructions: address.deliveryInstruction?.trim(),
|
|
203
|
+
postalCode: formattedPostalCode.trim(),
|
|
204
|
+
streetAddress: address.streetAddress?.trim(),
|
|
205
|
+
streetAddressAdditional: address.streetAddressAdditional?.trim()
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function checkAddress (address: AddressIF, schema: SchemaIF): AddressIF {
|
|
210
|
+
for(var addressProperty in schema) {
|
|
211
|
+
if (!address[addressProperty]) {
|
|
212
|
+
address[addressProperty] = ''
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return address
|
|
216
|
+
}
|
|
@@ -1,78 +1,78 @@
|
|
|
1
|
-
import countriesData from 'country-list/data.json'
|
|
2
|
-
import provincesData from 'provinces/provinces.json'
|
|
3
|
-
|
|
4
|
-
window['countries'] = window['countries'] || countriesData.sort((a, b) =>
|
|
5
|
-
(a.name < b.name) ? -1 : (a.name > b.name) ? 1 : 0)
|
|
6
|
-
|
|
7
|
-
window['provinces'] = window['provinces'] || provincesData.sort((a, b) =>
|
|
8
|
-
(a.name < b.name) ? -1 : (a.name > b.name) ? 1 : 0)
|
|
9
|
-
|
|
10
|
-
// global caching to improve performance when called multiple times
|
|
11
|
-
window['countryNameCache'] = {}
|
|
12
|
-
window['countryRegionsCache'] = {}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Factory that allows VM access to useful country/province data and functions.
|
|
16
|
-
* @link https://www.npmjs.com/package/country-list
|
|
17
|
-
* @lint https://www.npmjs.com/package/provinces
|
|
18
|
-
*/
|
|
19
|
-
export function useCountriesProvinces () {
|
|
20
|
-
/**
|
|
21
|
-
* Helper function to return a list of countries.
|
|
22
|
-
* @returns An array of country objects, sorted alphabetically.
|
|
23
|
-
*/
|
|
24
|
-
const getCountries = (naOnly = false): Array<object> => {
|
|
25
|
-
let countries = []
|
|
26
|
-
countries.push({ code: 'CA', name: 'Canada' })
|
|
27
|
-
countries.push({ code: 'US', name: 'United States' })
|
|
28
|
-
if (!naOnly) {
|
|
29
|
-
// name is set this way to ensure the divider is there in the search when CA/US are not the only options
|
|
30
|
-
countries.push({ code: '0', name: 'Can.nada. United States .Of.America', divider: true })
|
|
31
|
-
countries = countries.concat(window['countries'])
|
|
32
|
-
}
|
|
33
|
-
return countries
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Helper function to return a country's name.
|
|
37
|
-
* @param code The short code of the country.
|
|
38
|
-
* @returns The long name of the country.
|
|
39
|
-
*/
|
|
40
|
-
const getCountryName = (code: string): string => {
|
|
41
|
-
if (!code) return null
|
|
42
|
-
if (window['countryNameCache'][code]) return window['countryNameCache'][code]
|
|
43
|
-
const country = window['countries'].find(c => c.code === code)
|
|
44
|
-
const result = country ? country.name : null
|
|
45
|
-
window['countryNameCache'][code] = result
|
|
46
|
-
return result
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Helper function to return a country's list of provinces.
|
|
50
|
-
* @param code The short code of the country.
|
|
51
|
-
* @param overrideDefault A flag to bypass manual defaults.
|
|
52
|
-
* @returns An array of province objects, sorted alphabetically.
|
|
53
|
-
*/
|
|
54
|
-
const getCountryRegions = (code: string, overrideDefault = false): Array<object> => {
|
|
55
|
-
if (!code) return []
|
|
56
|
-
if (window['countryRegionsCache'][code]) return window['countryRegionsCache'][code]
|
|
57
|
-
let regions = []
|
|
58
|
-
if (code === 'CA' && !overrideDefault) {
|
|
59
|
-
regions.push({ name: 'British Columbia', short: 'BC' })
|
|
60
|
-
// name is set this way to ensure the divider is there in the search when BC is not the only option
|
|
61
|
-
regions.push({ code: '0', name: 'Br.it.is.h.Co.l.u.m.b.ia', divider: true })
|
|
62
|
-
}
|
|
63
|
-
const result = window['provinces']
|
|
64
|
-
.filter(p => p.country === code)
|
|
65
|
-
.map(p => ({
|
|
66
|
-
name: p.english || p.name,
|
|
67
|
-
short: (p.short && p.short.length <= 2) ? p.short : '--'
|
|
68
|
-
}))
|
|
69
|
-
regions = regions.concat(result)
|
|
70
|
-
window['countryRegionsCache'][code] = regions
|
|
71
|
-
return regions
|
|
72
|
-
}
|
|
73
|
-
return {
|
|
74
|
-
getCountries,
|
|
75
|
-
getCountryName,
|
|
76
|
-
getCountryRegions
|
|
77
|
-
}
|
|
78
|
-
}
|
|
1
|
+
import countriesData from 'country-list/data.json'
|
|
2
|
+
import provincesData from 'provinces/provinces.json'
|
|
3
|
+
|
|
4
|
+
window['countries'] = window['countries'] || countriesData.sort((a, b) =>
|
|
5
|
+
(a.name < b.name) ? -1 : (a.name > b.name) ? 1 : 0)
|
|
6
|
+
|
|
7
|
+
window['provinces'] = window['provinces'] || provincesData.sort((a, b) =>
|
|
8
|
+
(a.name < b.name) ? -1 : (a.name > b.name) ? 1 : 0)
|
|
9
|
+
|
|
10
|
+
// global caching to improve performance when called multiple times
|
|
11
|
+
window['countryNameCache'] = {}
|
|
12
|
+
window['countryRegionsCache'] = {}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Factory that allows VM access to useful country/province data and functions.
|
|
16
|
+
* @link https://www.npmjs.com/package/country-list
|
|
17
|
+
* @lint https://www.npmjs.com/package/provinces
|
|
18
|
+
*/
|
|
19
|
+
export function useCountriesProvinces () {
|
|
20
|
+
/**
|
|
21
|
+
* Helper function to return a list of countries.
|
|
22
|
+
* @returns An array of country objects, sorted alphabetically.
|
|
23
|
+
*/
|
|
24
|
+
const getCountries = (naOnly = false): Array<object> => {
|
|
25
|
+
let countries = []
|
|
26
|
+
countries.push({ code: 'CA', name: 'Canada' })
|
|
27
|
+
countries.push({ code: 'US', name: 'United States' })
|
|
28
|
+
if (!naOnly) {
|
|
29
|
+
// name is set this way to ensure the divider is there in the search when CA/US are not the only options
|
|
30
|
+
countries.push({ code: '0', name: 'Can.nada. United States .Of.America', divider: true })
|
|
31
|
+
countries = countries.concat(window['countries'])
|
|
32
|
+
}
|
|
33
|
+
return countries
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Helper function to return a country's name.
|
|
37
|
+
* @param code The short code of the country.
|
|
38
|
+
* @returns The long name of the country.
|
|
39
|
+
*/
|
|
40
|
+
const getCountryName = (code: string): string => {
|
|
41
|
+
if (!code) return null
|
|
42
|
+
if (window['countryNameCache'][code]) return window['countryNameCache'][code]
|
|
43
|
+
const country = window['countries'].find(c => c.code === code)
|
|
44
|
+
const result = country ? country.name : null
|
|
45
|
+
window['countryNameCache'][code] = result
|
|
46
|
+
return result
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Helper function to return a country's list of provinces.
|
|
50
|
+
* @param code The short code of the country.
|
|
51
|
+
* @param overrideDefault A flag to bypass manual defaults.
|
|
52
|
+
* @returns An array of province objects, sorted alphabetically.
|
|
53
|
+
*/
|
|
54
|
+
const getCountryRegions = (code: string, overrideDefault = false): Array<object> => {
|
|
55
|
+
if (!code) return []
|
|
56
|
+
if (window['countryRegionsCache'][code]) return window['countryRegionsCache'][code]
|
|
57
|
+
let regions = []
|
|
58
|
+
if (code === 'CA' && !overrideDefault) {
|
|
59
|
+
regions.push({ name: 'British Columbia', short: 'BC' })
|
|
60
|
+
// name is set this way to ensure the divider is there in the search when BC is not the only option
|
|
61
|
+
regions.push({ code: '0', name: 'Br.it.is.h.Co.l.u.m.b.ia', divider: true })
|
|
62
|
+
}
|
|
63
|
+
const result = window['provinces']
|
|
64
|
+
.filter(p => p.country === code)
|
|
65
|
+
.map(p => ({
|
|
66
|
+
name: p.english || p.name,
|
|
67
|
+
short: (p.short && p.short.length <= 2) ? p.short : '--'
|
|
68
|
+
}))
|
|
69
|
+
regions = regions.concat(result)
|
|
70
|
+
window['countryRegionsCache'][code] = regions
|
|
71
|
+
return regions
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
getCountries,
|
|
75
|
+
getCountryName,
|
|
76
|
+
getCountryRegions
|
|
77
|
+
}
|
|
78
|
+
}
|
package/factories/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { formatAddress, useAddress, useAddressComplete, useCountryRegions } from './address-factory'
|
|
2
|
-
export { useCountriesProvinces } from './countries-provinces-factory'
|
|
3
|
-
export { baseRules, spaceRules, useBaseValidations } from './validation-factory'
|
|
1
|
+
export { formatAddress, useAddress, useAddressComplete, useCountryRegions } from './address-factory'
|
|
2
|
+
export { useCountriesProvinces } from './countries-provinces-factory'
|
|
3
|
+
export { baseRules, spaceRules, useBaseValidations } from './validation-factory'
|