formatter
import { formatThaiAddressSuggestion } from 'thaizip'
formatThaiAddressSuggestion
หัวข้อ “formatThaiAddressSuggestion”function formatThaiAddressSuggestion( record: ThaiAddressRecord, options?: FormatSuggestionOptions,): ThaiAddressSuggestionแปลง ThaiAddressRecord (ผลลัพธ์ดิบจาก searchThaiAddress) ให้เป็น ThaiAddressSuggestion ที่มี label สำหรับแสดงในช่อง dropdown พร้อมใช้งาน
พารามิเตอร์
หัวข้อ “พารามิเตอร์”| ชื่อ | ชนิด | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
record | ThaiAddressRecord | — (จำเป็น) | record หนึ่งรายการจากผลลัพธ์ searchThaiAddress |
options? | FormatSuggestionOptions | undefined | ตัวเลือกภาษาของ label |
FormatSuggestionOptions:
| ชื่อ | ชนิด | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
locale? | 'th' | 'en' | 'th' | ภาษาของ field label — ถ้าไม่ใช่ 'en' เป๊ะ ๆ จะได้ label เป็นภาษาไทยเสมอ |
คืนค่า
หัวข้อ “คืนค่า”type ThaiAddressSuggestion = { id: string label: string labelTh: string labelEn: string tambon: string tambonEn: string amphure: string amphureEn: string province: string provinceEn: string zipCode: string}หมายเหตุ
หัวข้อ “หมายเหตุ”idคือString(record.tambonId)— เป็นค่าเดียวกับที่selectSuggestionของuseThaiAddressAutocompleteใช้ lookup กลับไปยัง record ต้นฉบับแบบ O(1)labelThและlabelEnถูกสร้างมาเสมอทั้งคู่ ไม่ว่าจะเลือกlocaleไหน — รูปแบบคือ`${tambon} > ${amphure} > ${province} ${zipCode}`(ใช้ชื่อภาษาที่ตรงกันทั้งสามระดับ)labelตามoptions.locale: เป็นlabelEnเมื่อoptions?.locale === 'en'เป๊ะ ๆ เท่านั้น กรณีอื่นทั้งหมด (รวมถึงไม่ส่งoptionsมาเลย) ได้labelTh- field ที่เหลือ (
tambon,amphure,province,zipCodeและคู่...En) คัดลอกมาจากrecordตรง ๆ ไม่มีการแปลงเพิ่ม
ตัวอย่าง
หัวข้อ “ตัวอย่าง”import { formatThaiAddressSuggestion, searchThaiAddress } from 'thaizip'
const records = searchThaiAddress(index, 'ลาดพร้าว')const suggestions = records.map((r) => formatThaiAddressSuggestion(r))// suggestions[0].label === suggestions[0].labelTh (locale เริ่มต้นเป็น 'th')
const enSuggestions = records.map((r) => formatThaiAddressSuggestion(r, { locale: 'en' }))// enSuggestions[0].label === enSuggestions[0].labelEn