counties, pagination, filter count, show total results
This commit is contained in:
68
bizmatch-server/src/utils/importCounties.ts
Normal file
68
bizmatch-server/src/utils/importCounties.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import * as fs from 'fs';
|
||||
import * as readline from 'readline';
|
||||
|
||||
interface CityData {
|
||||
city: string;
|
||||
stateShort: string;
|
||||
stateFull: string;
|
||||
county: string;
|
||||
cityAlias: string;
|
||||
}
|
||||
|
||||
interface StateCountyData {
|
||||
state: string;
|
||||
state_full: string;
|
||||
counties: string[];
|
||||
}
|
||||
|
||||
async function parseData(filePath: string): Promise<CityData[]> {
|
||||
const fileStream = fs.createReadStream(filePath);
|
||||
const rl = readline.createInterface({
|
||||
input: fileStream,
|
||||
crlfDelay: Infinity,
|
||||
});
|
||||
|
||||
const data: CityData[] = [];
|
||||
let isFirstLine = true;
|
||||
|
||||
for await (const line of rl) {
|
||||
if (isFirstLine) {
|
||||
isFirstLine = false;
|
||||
continue; // Skip the first line
|
||||
}
|
||||
const [city, stateShort, stateFull, county, cityAlias] = line.split('|');
|
||||
data.push({ city, stateShort, stateFull, county, cityAlias });
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
function transformData(data: CityData[]): StateCountyData[] {
|
||||
const stateMap: { [key: string]: { stateFull: string; counties: Set<string> } } = {};
|
||||
|
||||
data.forEach(item => {
|
||||
if (!stateMap[item.stateShort]) {
|
||||
stateMap[item.stateShort] = {
|
||||
stateFull: item.stateFull,
|
||||
counties: new Set(),
|
||||
};
|
||||
}
|
||||
stateMap[item.stateShort].counties.add(item.county);
|
||||
});
|
||||
|
||||
return Object.entries(stateMap).map(([state, value]) => ({
|
||||
state,
|
||||
state_full: value.stateFull,
|
||||
counties: Array.from(value.counties).sort(),
|
||||
}));
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const filePath = './src/assets/counties_raw.csv'; // Ersetze diesen Pfad mit dem Pfad zu deiner Datei
|
||||
const cityData = await parseData(filePath);
|
||||
const stateCountyData = transformData(cityData);
|
||||
|
||||
console.log(JSON.stringify(stateCountyData, null, 2));
|
||||
}
|
||||
|
||||
main().catch(err => console.error(err));
|
||||
Reference in New Issue
Block a user