private-thailand-address 1.0.6 → 1.0.7
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/index.js +51 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -156,6 +156,55 @@ function getAutoSuggestion(search, limit = 10) {
|
|
|
156
156
|
return cleaned
|
|
157
157
|
}
|
|
158
158
|
|
|
159
|
+
function getRelateAddress(search, from, limit = 10) {
|
|
160
|
+
const searchRegex = new RegExp(search.value, 'i')
|
|
161
|
+
const fromRegex = new RegExp(from.value, 'i')
|
|
162
|
+
const fromData = data.filter((item) => {
|
|
163
|
+
return (
|
|
164
|
+
(from.field === 'zipCode' && fromRegex.test(item.zipCode))
|
|
165
|
+
|| (from.field === 'subDistrict' && item.subDistrictList && item.subDistrictList.some((subDistrict) => fromRegex.test(subDistrict.subDistrictName)))
|
|
166
|
+
|| (from.field === 'district' && item.districtList && item.districtList.some((district) => fromRegex.test(district.districtName)))
|
|
167
|
+
|| (from.field === 'province' && item.provinceList && item.provinceList.some((province) => fromRegex.test(province.provinceName)))
|
|
168
|
+
)
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
const results = fromData.filter((item) => {
|
|
172
|
+
return (
|
|
173
|
+
(search.field === 'zipCode' && searchRegex.test(item.zipCode))
|
|
174
|
+
|| (search.field === 'subDistrict' && item.subDistrictList && item.subDistrictList.some((subDistrict) => searchRegex.test(subDistrict.subDistrictName)))
|
|
175
|
+
|| (search.field === 'district' && item.districtList && item.districtList.some((district) => searchRegex.test(district.districtName)))
|
|
176
|
+
|| (search.field === 'province' && item.provinceList && item.provinceList.some((province) => searchRegex.test(province.provinceName)))
|
|
177
|
+
)
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
const remapped = toAddress(results)
|
|
181
|
+
const cleaned = remapped.filter((r) => (
|
|
182
|
+
(search.field === 'zipCode' && searchRegex.test(r.zipCode))
|
|
183
|
+
|| (search.field === 'subDistrict' && searchRegex.test(r.subDistrict))
|
|
184
|
+
|| (search.field === 'district' && searchRegex.test(r.district))
|
|
185
|
+
|| (search.field === 'province' && searchRegex.test(r.province))
|
|
186
|
+
)).slice(0, limit)
|
|
187
|
+
|
|
188
|
+
cleaned.sort((a, b) => {
|
|
189
|
+
let aSimilarity = calculateSimilarity(search.value, a)
|
|
190
|
+
let bSimilarity = calculateSimilarity(search.value, b)
|
|
191
|
+
|
|
192
|
+
return aSimilarity - bSimilarity
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
if (search.field === 'zipCode') {
|
|
196
|
+
return [...new Set(cleaned.map((c) => c.zipCode))].slice(0, limit)
|
|
197
|
+
} else if (search.field === 'subDistrict') {
|
|
198
|
+
return [...new Set(cleaned.map((c) => c.subDistrict))].slice(0, limit)
|
|
199
|
+
} else if (search.field === 'district') {
|
|
200
|
+
return [...new Set(cleaned.map((c) => c.district))].slice(0, limit)
|
|
201
|
+
} else if (search.field === 'province') {
|
|
202
|
+
return [...new Set(cleaned.map((c) => c.province))].slice(0, limit)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return cleaned
|
|
206
|
+
}
|
|
207
|
+
|
|
159
208
|
function getSubDistricts(search, limit = 10) {
|
|
160
209
|
const regex = new RegExp(search, 'i')
|
|
161
210
|
const results = []
|
|
@@ -226,5 +275,6 @@ module.exports = {
|
|
|
226
275
|
getSubDistricts,
|
|
227
276
|
getDistricts,
|
|
228
277
|
getProvinces,
|
|
229
|
-
getZipCodes
|
|
278
|
+
getZipCodes,
|
|
279
|
+
getRelateAddress
|
|
230
280
|
}
|