67 lines
2.2 KiB
TypeScript
67 lines
2.2 KiB
TypeScript
import { useI18n } from 'vue-i18n';
|
|
import { District, Province, SubDistrict } from 'src/stores/address';
|
|
|
|
export function formatAddress(opt: {
|
|
address: string;
|
|
addressEN: string;
|
|
moo?: string;
|
|
mooEN?: string;
|
|
soi?: string;
|
|
soiEN?: string;
|
|
street?: string;
|
|
streetEN?: string;
|
|
province?: Province | null;
|
|
district?: District | null;
|
|
subDistrict?: SubDistrict | null;
|
|
en?: boolean;
|
|
zipCode?: string;
|
|
}) {
|
|
const { t } = useI18n();
|
|
let addressParts: string[];
|
|
if (opt.en) {
|
|
// en
|
|
addressParts = [`${opt.addressEN}`];
|
|
if (opt.mooEN) addressParts.push(`Moo ${opt.mooEN}`);
|
|
if (opt.soiEN) addressParts.push(`Soi ${opt.soiEN}`);
|
|
if (opt.streetEN) addressParts.push(`${opt.streetEN} Rd.`);
|
|
|
|
if (opt.subDistrict) {
|
|
addressParts.push(`${opt.subDistrict.nameEN} sub-district`);
|
|
}
|
|
if (opt.district) addressParts.push(`${opt.district.nameEN} distric`);
|
|
if (opt.province) addressParts.push(`${opt.province.nameEN}`);
|
|
} else {
|
|
// th
|
|
addressParts = [`${opt.address}`];
|
|
if (opt.moo) addressParts.push(`หมู่ ${opt.moo}`);
|
|
if (opt.soi) addressParts.push(`ซอย ${opt.soi}`);
|
|
if (opt.street) addressParts.push(`ถนน${opt.street}`);
|
|
|
|
if (opt.subDistrict) {
|
|
addressParts.push(
|
|
`${opt.province?.id === '10' ? 'แขวง' : 'ตำบล'}${opt.subDistrict.name}`,
|
|
);
|
|
}
|
|
if (opt.district) {
|
|
addressParts.push(
|
|
`${opt.province?.id === '10' ? 'เขต' : 'อำเภอ'}${opt.district.name}`,
|
|
);
|
|
}
|
|
if (opt.province) addressParts.push(`จังหวัด${opt.province.name}`);
|
|
// addressParts.push(
|
|
// `${opt.province.id === '10' ? t('address.subArea') : t('address.subDistrict')}${opt.subDistrict.name},`,
|
|
// );
|
|
// addressParts.push(
|
|
// `${opt.province.id === '10' ? t('address.area') : t('address.district')}${opt.district.name},`,
|
|
// );
|
|
// addressParts.push(
|
|
// ` ${opt.province.id === '10' ? t('address.province') : t('address.province')}${opt.province.name},`,
|
|
// );
|
|
}
|
|
|
|
if (opt.subDistrict && !opt.zipCode)
|
|
addressParts.push(`${opt.subDistrict.zipCode}`);
|
|
if (opt.zipCode) addressParts.push(`${opt.zipCode}`);
|
|
|
|
return addressParts.join(' ');
|
|
}
|