iran-cities
Version:
The most accurate database of city, county and province names of Iran
103 lines (92 loc) • 2.9 kB
JavaScript
const fs = require('fs');
const jsonfile = require('jsonfile');
const path = require('path');
const csvPath = {
city: '/csv/city.csv',
county: '/csv/county.csv',
province: '/csv/province.csv'
};
const jsonPath = {
city: '/json/city.json',
county: '/json/county.json',
province: '/json/province.json'
};
let data = {
city: [],
county: [],
province: []
};
let csvToJsonCity = () => {
let lineReader = require('readline').createInterface({
input: require('fs').createReadStream(path.resolve(__dirname + csvPath.city))
});
lineReader.on('line', (line) => {
let splitLine = line.split(',');
let obj = {
id: splitLine[0],
province_id: splitLine[1],
county_id: splitLine[2],
name: splitLine[3]
};
data.city.push(obj);
}).on('close', () => {
if (!fs.existsSync(path.resolve(__dirname + jsonPath.city))) {
jsonfile.writeFileSync(path.resolve(__dirname + jsonPath.city), data.city, { flag: 'a' });
}
});
};
let csvToJsonCounty = () => {
let lineReader = require('readline').createInterface({
input: require('fs').createReadStream(path.resolve(__dirname + csvPath.county))
});
lineReader.on('line', (line) => {
let splitLine = line.split(',');
let obj = {
id: splitLine[0],
province_id: splitLine[1],
name: splitLine[2]
};
data.county.push(obj);
}).on('close', () => {
if (!fs.existsSync(path.resolve(__dirname + jsonPath.county))) {
jsonfile.writeFileSync(path.resolve(__dirname + jsonPath.county), data.county, { flag: 'a' });
}
});
};
let csvToJsonProvince = () => {
let lineReader = require('readline').createInterface({
input: require('fs').createReadStream(path.resolve(__dirname + csvPath.province))
});
lineReader.on('line', (line) => {
let splitLine = line.split(',');
let obj = {
id: splitLine[0],
name: splitLine[1]
};
data.province.push(obj);
}).on('close', () => {
if (!fs.existsSync(path.resolve(__dirname + jsonPath.province))) {
jsonfile.writeFileSync(path.resolve(__dirname + jsonPath.province), data.province, { flag: 'a' });
}
});
};
exports.convertToJson = () => {
csvToJsonCity();
csvToJsonCounty();
csvToJsonProvince();
};
exports.getCity = () => {
if (fs.existsSync(path.resolve(__dirname + jsonPath.city))) {
return jsonfile.readFileSync(path.resolve(__dirname + jsonPath.city));
}
};
exports.getCounty = () => {
if (fs.existsSync(path.resolve(__dirname + jsonPath.county))) {
return jsonfile.readFileSync(path.resolve(__dirname + jsonPath.county));
}
};
exports.getProvince = () => {
if (fs.existsSync(path.resolve(__dirname + jsonPath.province))) {
return jsonfile.readFileSync(path.resolve(__dirname + jsonPath.province));
}
};