jp-zipcode-lookup
Version:
Japan Postal Zip Code Lookup
123 lines (122 loc) • 4.16 kB
JavaScript
;
// update
Object.defineProperty(exports, "__esModule", { value: true });
const cdate_1 = require("cdate");
const fs = require("fs");
const japanpost_zipcode_1 = require("japanpost-zipcode");
const WARN = (message) => console.warn(message);
async function CLI(outDir) {
const kenAll = await new japanpost_zipcode_1.KenAll({ logger: console });
await kenAll.clean();
const modified = await kenAll.modifiedAt();
console.warn("modified: " + (0, cdate_1.cdate)(modified).utcOffset(9).text("%Y/%m/%d %H:%M"));
const allRows = await kenAll.readAll();
const prefMaster = {};
const cityMaster = {};
const zip5Master = {};
const zip7Master = {};
const lastCity = {};
const zip5To7 = {};
const dup = {};
for (const row of allRows) {
const city = row[0 /* C.全国地方公共団体コード */];
const zip7 = row[2 /* C.郵便番号 */];
const name = row[8 /* C.町域名 */];
// 重複町域名を除外する
const dupKey = city + zip7 + name;
if (dup[dupKey])
continue;
dup[dupKey] = true;
const postal = zip7Master[zip7] || (zip7Master[zip7] = []);
// 市区町村コード
if (lastCity[zip7] !== city) {
postal.push(+city);
lastCity[zip7] = city;
}
// 町域名
postal.push(name);
// 先頭5文字
const zip5 = zip7.substr(0, 5);
const idx5 = zip5To7[zip5] || (zip5To7[zip5] = {});
idx5[zip7] = postal;
// 市区町村名
if (cityMaster[city])
continue;
cityMaster[city] = [row[7 /* C.市区町村名 */], row[4 /* C.市区町村名カナ */]];
// 都道府県名
const pref = city.substr(0, 2);
if (prefMaster[pref])
continue;
prefMaster[pref] = [row[6 /* C.都道府県名 */], row[3 /* C.都道府県名カナ */]];
}
// 正規化
for (const zip5 in zip5To7) {
const idx5 = zip5To7[zip5];
const zip7list = Object.keys(idx5);
if (zip7list.length === 1)
continue;
const rows = zip7list.map(v => idx5[v]);
const city = rows[0][0];
const name = rows[0][1];
// すべて同じ市区町村名
const sameCity = rows.every(row => row[0] === city);
if (!sameCity)
continue;
rows.forEach(item => item.shift());
const row = zip5Master[zip5] = [city];
// すべて同じ町域名
const sameName = rows.every(row => row[0] === name);
if (!sameName)
continue;
rows.forEach(item => item.shift());
row.push(name);
// 空になった要素を削除する
zip7list.forEach(zip7 => {
if (zip7Master[zip7].length === 0)
delete zip7Master[zip7];
});
}
// pref.json
{
let json = JSON.stringify({ pref: prefMaster });
json = json.replace(/("\d{2}":)/g, "\n$1");
write("pref.json", json);
}
// city.json
{
let json = JSON.stringify({ city: cityMaster });
json = json.replace(/("\d{5}":)/g, "\n$1");
write("city.json", json);
}
// zip5.json
{
let json = JSON.stringify({ zip5: compact(zip5Master) });
json = json.replace(/("\d{5,7}":)/g, "\n$1");
write("zip5.json", json);
}
// zip7.json
{
let json = JSON.stringify({ zip7: compact(zip7Master) });
json = json.replace(/("\d{5,7}":)/g, "\n$1");
write("zip7.json", json);
}
function write(file, json) {
if (outDir) {
file = outDir.replace(/\/?$/, "/" + file);
WARN("writing: " + file);
fs.createWriteStream(file).write(json);
}
else {
process.stdout.write(json);
}
}
function compact(master) {
const flex = {};
Object.keys(master).sort().forEach(key => {
const row = master[key];
flex[key] = (row.length === 1) ? row[0] : row;
});
return flex;
}
}
CLI.apply(null, process.argv.slice(2));