UNPKG

idn-area-data

Version:

Indonesia administrative area data, based on the latest regulation

144 lines (142 loc) 3.59 kB
// src/index.ts import { dirname, resolve } from "path"; import { fileURLToPath } from "url"; // src/csv-parser.ts import { createReadStream } from "fs"; import Papa from "papaparse"; var CsvParser = class _CsvParser { defaultConfig; /** * Create a new instance of CsvHelper. * @param defaultConfig The default options */ constructor(defaultConfig) { this.defaultConfig = defaultConfig; } /** * Parse CSV file asynchronously. * * @param path Path to the CSV file. * @param config The configuration for the parser. */ static async parse(path, config) { const encoding = config?.encoding ? config.encoding : "utf8"; const sourceFile = createReadStream(path, encoding); return new Promise((resolve2, reject) => { Papa.parse(sourceFile, { ...config, complete: resolve2, error: reject }); }); } /** * Parse CSV file asynchronously. * * @param path Path to the CSV file. * @param config The configuration for the parser. */ async parse(path, config) { return _CsvParser.parse(path, { ...this.defaultConfig, ...config }); } }; // src/index.ts function transformValue(value, header, transformer) { let headerKey = header; if (!Object.hasOwn(transformer.values ?? {}, header)) { const headerEntries = Object.entries(transformer.headers ?? {}); for (const [oldHeader, newHeader] of headerEntries) { if (newHeader === header) { headerKey = oldHeader; break; } } } const transformFunction = transformer.values?.[headerKey]; return transformFunction ? transformFunction(value) : value; } async function getData(area, options) { const filePath = resolve( dirname(fileURLToPath(import.meta.url)), `../data/${area}.csv` ); const transformer = options?.transform; const result = await CsvParser.parse(filePath, { header: true, ...transformer && { transformHeader(header) { const newHeader = transformer.headers?.[header]; return newHeader ?? header; }, transform(value, tHeader) { if (typeof tHeader === "string") { return transformValue(value, tHeader, transformer); } return value; } } }); return result.data; } function getProvinces() { return getData("provinces"); } function getRegencies(options) { return getData("regencies", { ...options, transform: options?.transform ? { headers: { province_code: "provinceCode" } } : void 0 }); } function getDistricts(options) { return getData("districts", { ...options, transform: options?.transform ? { headers: { regency_code: "regencyCode" } } : void 0 }); } function getIslands(options) { return getData("islands", { ...options, transform: options?.transform ? { headers: { is_outermost_small: "isOutermostSmall", is_populated: "isPopulated", regency_code: "regencyCode" }, values: { is_populated: (value) => !!Number.parseInt(value, 10), is_outermost_small: (value) => !!Number.parseInt(value, 10), regency_code: (value) => value === "" ? null : value } } : void 0 }); } function getVillages(options) { return getData("villages", { ...options, transform: options?.transform ? { headers: { district_code: "districtCode" } } : void 0 }); } export { getData, getDistricts, getIslands, getProvinces, getRegencies, getVillages }; //# sourceMappingURL=index.js.map