formatter
import { formatThaiAddressSuggestion } from 'thaizip'
formatThaiAddressSuggestion
Section titled “formatThaiAddressSuggestion”function formatThaiAddressSuggestion( record: ThaiAddressRecord, options?: FormatSuggestionOptions,): ThaiAddressSuggestionConverts a ThaiAddressRecord (a raw result from searchThaiAddress) into a ThaiAddressSuggestion with a ready-to-render label for a dropdown.
Parameters
Section titled “Parameters”| Name | Type | Default | Description |
|---|---|---|---|
record | ThaiAddressRecord | — (required) | One record from a searchThaiAddress result |
options? | FormatSuggestionOptions | undefined | Controls the language of label |
FormatSuggestionOptions:
| Name | Type | Default | Description |
|---|---|---|---|
locale? | 'th' | 'en' | 'th' | Language for the label field — anything other than exactly 'en' produces a Thai label |
Returns
Section titled “Returns”type ThaiAddressSuggestion = { id: string label: string labelTh: string labelEn: string tambon: string tambonEn: string amphure: string amphureEn: string province: string provinceEn: string zipCode: string}idisString(record.tambonId)— the same valueselectSuggestiononuseThaiAddressAutocompleteuses to look the original record back up in O(1)labelThandlabelEnare always both built, regardless of the chosenlocale— the format is`${tambon} > ${amphure} > ${province} ${zipCode}`(using the matching language at all three levels)labelfollowsoptions.locale: it’slabelEnonly whenoptions?.locale === 'en'exactly; every other case (including nooptionsat all) yieldslabelTh- The remaining fields (
tambon,amphure,province,zipCode, and their...Encounterparts) are copied straight fromrecordwith no further transformation
Example
Section titled “Example”import { formatThaiAddressSuggestion, searchThaiAddress } from 'thaizip'
const records = searchThaiAddress(index, 'ลาดพร้าว')const suggestions = records.map((r) => formatThaiAddressSuggestion(r))// suggestions[0].label === suggestions[0].labelTh (default locale is 'th')
const enSuggestions = records.map((r) => formatThaiAddressSuggestion(r, { locale: 'en' }))// enSuggestions[0].label === enSuggestions[0].labelEn