@clregions/core
Version:
¡Bienvenido! Con nuestra librería de TypeScript, podrás acceder fácilmente a información actualizada y precisa sobre las regiones, provincias y comunas de Chile, ahorrando tiempo y esfuerzo al no tener que recopilar y mantener los datos tú mismo.
131 lines (129 loc) • 3.59 kB
JavaScript
// src/index.ts
import {
CLCommuneObject,
CLProvinceObject,
CLRegionObject,
CommuneId,
ProvinceId,
RegionId
} from "@clregions/data";
// src/lib.ts
import { clRegions } from "@clregions/data/object";
var findAllRegions = () => structuredClone(clRegions.regions);
var findRegionById = (regionId) => clRegions.regions[regionId] ? structuredClone(clRegions.regions[regionId]) : null;
var findAllProvincesByRegion = (regionId) => {
const region = clRegions.regions[regionId];
if (!region) {
return null;
}
return structuredClone(region.provinces);
};
var findAllCommunesByRegion = (regionId) => {
const region = clRegions.regions[regionId];
if (!region) {
return null;
}
const provinces = structuredClone(region.provinces);
const communes = {};
for (const provKey in provinces) {
if (Object.prototype.hasOwnProperty.call(provinces, provKey)) {
const province = provinces[provKey];
for (const communeKey in province.communes) {
if (Object.prototype.hasOwnProperty.call(province.communes, communeKey)) {
communes[communeKey] = province.communes[communeKey];
}
}
}
}
return communes;
};
var findAllProvinces = () => {
const result = {};
for (const region of Object.values(clRegions.regions)) {
const provinces = structuredClone(region.provinces);
for (const key in provinces) {
if (Object.prototype.hasOwnProperty.call(provinces, key)) {
result[key] = provinces[key];
}
}
}
return result;
};
var findProvinceById = (provinceId) => {
try {
const parentRegionKey = provinceId.length === 2 ? provinceId.slice(0, 1) : provinceId.slice(0, 2);
const province = structuredClone(
clRegions.regions[parentRegionKey].provinces[provinceId]
);
return province;
} catch {
return null;
}
};
var findAllCommunesByProvince = (provinceId) => {
try {
const communesData = structuredClone(
clRegions.regions[provinceId.slice(0, 2)].provinces[provinceId].communes
);
const result = {};
for (const key in communesData) {
if (Object.prototype.hasOwnProperty.call(communesData, key)) {
result[key] = communesData[key];
}
}
return result;
} catch {
return null;
}
};
var findAllCommunes = () => {
const communes = {};
for (const region of Object.values(clRegions.regions)) {
for (const provKey in region.provinces) {
if (Object.prototype.hasOwnProperty.call(region.provinces, provKey)) {
const province = region.provinces[provKey];
for (const commKey in province.communes) {
if (Object.prototype.hasOwnProperty.call(province.communes, commKey)) {
communes[commKey] = structuredClone(province.communes[commKey]);
}
}
}
}
}
return communes;
};
var findCommuneById = (communeId) => {
try {
const commune = structuredClone(
clRegions.regions[communeId.slice(0, 2)].provinces[communeId.slice(0, 3)].communes[communeId]
);
return structuredClone(commune);
} catch {
return null;
}
};
var findRegionByCommune = (communeId) => {
const region = structuredClone(clRegions.regions[communeId.slice(0, 2)]);
if (!region) {
return null;
}
return region;
};
export {
CLCommuneObject,
CLProvinceObject,
CLRegionObject,
CommuneId,
ProvinceId,
RegionId,
findAllCommunes,
findAllCommunesByProvince,
findAllCommunesByRegion,
findAllProvinces,
findAllProvincesByRegion,
findAllRegions,
findCommuneById,
findProvinceById,
findRegionByCommune,
findRegionById
};