@anouluk/laos-address-typescript
Version:
TypeScript utility for Lao addresses
219 lines (218 loc) • 8.02 kB
JavaScript
// Import address data from external sources
import { DISTRICT, PROVINCE, VILLAGE } from "./addresses";
/**
* Find all items in an array that match a specific key-value pair
*/
const getValuesById = (array, key, value) => {
return array.filter(data => data[key] === value);
};
/**
* Find a single item in an array that matches a specific key-value pair
*/
const getASingleValueFromObjectByKey = (array, key, value) => {
return array.find(data => data[key] === value);
};
/**
* Get multiple items from an array based on an array of ids
*/
const getFilterValuesByArrayID = (array, key, arrayCollection) => {
return array.filter(data => arrayCollection.includes(data[key]));
};
/**
* Normalize an id to string format
*/
const normalizeId = (id) => {
if (id === null)
return null;
if (typeof id === "boolean")
return null;
if (id === undefined)
return null;
return typeof id === "number" ? id.toString() : id;
};
/**
* Get a province by its ID
*/
const getProvinceValueByID = (id, existData = null) => {
const pid = normalizeId(id);
if (!pid)
return undefined;
const data = existData || PROVINCE;
return getASingleValueFromObjectByKey(data, 'pid', pid);
};
/**
* Get a district by its ID
*/
const getDistrictValueByID = (id, existData = null) => {
const did = normalizeId(id);
if (!did)
return undefined;
const data = existData || DISTRICT;
return getASingleValueFromObjectByKey(data, 'did', did);
};
/**
* Get a village by its ID
*/
const getVillageNameById = (id, existData = null) => {
const vid = normalizeId(id);
if (!vid)
return undefined;
const data = existData || VILLAGE;
return getASingleValueFromObjectByKey(data, 'vid', vid);
};
/**
* Get all districts
*/
const getDistricts = () => {
return DISTRICT;
};
/**
* Get all villages
*/
const getVillages = () => {
return VILLAGE;
};
/**
* Get all provinces
*/
const getProvinces = () => {
return PROVINCE;
};
/**
* Get districts by province ID
*/
const getDistrictsByProvinceId = (id, existData = null) => {
const pid = normalizeId(id);
if (!pid)
return [];
const data = existData || DISTRICT;
return getValuesById(data, 'pid', pid);
};
/**
* Get villages by district ID
*/
const getVillagesByDistrictId = (id, existData = null) => {
const did = normalizeId(id);
if (!did)
return [];
const data = existData || VILLAGE;
return getValuesById(data, 'did', did);
};
/**
* Get Lao addresses based on criteria
*/
const getLaoAddress = ({ province, district, village }) => {
// Normalize inputs
const normalizedProvince = normalizeId(province);
const normalizedDistrict = normalizeId(district);
const normalizedVillage = normalizeId(village);
// Handle 'all' values
const isAllProvince = normalizedProvince === 'all' || normalizedProvince === 'ALL' || province === true;
const isAllDistrict = normalizedDistrict === 'all' || normalizedDistrict === 'ALL' || district === true;
const isAllVillage = normalizedVillage === 'all' || normalizedVillage === 'ALL' || village === true;
// Case 1: No district and village specified
if (normalizedDistrict == null && normalizedVillage == null) {
if (normalizedProvince == null || isAllProvince) {
// Return all provinces
return getProvinces();
}
else {
// Return specific province
const provinceData = getProvinceValueByID(normalizedProvince);
return provinceData || 'NO DATA';
}
}
// Case 2: District specified but no village
if (normalizedVillage == null && normalizedDistrict != null) {
// Case 2.1: No specific province (or all provinces)
if (isAllProvince || normalizedProvince === null) {
if (isAllDistrict) {
// All districts
return getDistricts();
}
else {
// Specific district
const districtData = getDistrictValueByID(normalizedDistrict);
return districtData || 'NO DATA';
}
}
// Case 2.2: Specific province
else {
if (isAllDistrict) {
// All districts in specific province
const districtList = getDistrictsByProvinceId(normalizedProvince);
return districtList.length > 0 ? districtList : 'NO DATA';
}
else {
// Specific district in specific province
const districtList = getDistrictsByProvinceId(normalizedProvince);
if (districtList.length == 0)
return 'NO DATA';
const districtData = getDistrictValueByID(normalizedDistrict, districtList);
return districtData || 'NO DATA';
}
}
}
// Case 3: Village specified
if (normalizedVillage !== null) {
// Case 3.1: No specific province and district (or all of both)
if ((isAllProvince || normalizedProvince == null) && (isAllDistrict || normalizedDistrict == null)) {
if (isAllVillage) {
// All villages
return getVillages();
}
else {
// Specific village
const villageData = getVillageNameById(normalizedVillage);
return villageData || 'NO DATA';
}
}
// Case 3.2: Specific province or district scenarios
else {
// Case 3.2.1: Specific province, but all/no districts
if (normalizedProvince != null && !isAllProvince && (normalizedDistrict == null || isAllDistrict)) {
if (isAllVillage) {
// All villages in specific province
const districtList = getDistrictsByProvinceId(normalizedProvince);
if (districtList.length == 0)
return 'NO DATA';
const districtIds = districtList.map(d => d.did);
return getFilterValuesByArrayID(VILLAGE, 'did', districtIds);
}
else {
// Specific village
return getVillageNameById(normalizedVillage) || 'NO DATA';
}
}
// Case 3.2.2: Specific district, but all/no provinces
else if (normalizedDistrict != null && !isAllDistrict && (normalizedProvince == null || isAllProvince)) {
if (isAllVillage) {
// All villages in specific district
return getVillagesByDistrictId(normalizedDistrict);
}
else {
// Specific village
return getVillageNameById(normalizedVillage) || 'NO DATA';
}
}
// Case 3.2.3: Both specific province and district
else if (normalizedProvince != null && !isAllProvince && normalizedDistrict != null && !isAllDistrict) {
if (isAllVillage) {
// Check if district belongs to province
const districtList = getDistrictsByProvinceId(normalizedProvince);
const districtExists = districtList.some(d => d.did == normalizedDistrict);
if (!districtExists)
return 'NO MATCH';
// All villages in specific district that belongs to specific province
return getVillagesByDistrictId(normalizedDistrict);
}
else {
// Specific village
return getVillageNameById(normalizedVillage) || 'NO DATA';
}
}
}
}
return 'INVALID QUERY';
};
export { getLaoAddress, getProvinces, getDistricts, getVillages, getProvinceValueByID, getDistrictValueByID, getVillageNameById, getDistrictsByProvinceId, getVillagesByDistrictId };