forked from code4sabae/zipcode-japan-es
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostalCode.js
More file actions
133 lines (126 loc) · 3.2 KB
/
PostalCode.js
File metadata and controls
133 lines (126 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { CSV } from "https://js.sabae.cc/CSV.js";
import { LGCode } from "https://code4fukui.github.io/LGCode/LGCode.js";
const zipcache = {};
const fromZipCode = async (code) => {
let s = code;
if (typeof s === "string") {
//s = IMIMojiConverter.toHalfWidth(s).replace(/[\D]/g, "");
s = s.replace(/[\D]/g, "");
if (s.length < 7) {
const n = parseInt(s);
s = isNaN(n) ? "0000000" : n + "0000000";
}
s = s.substring(0, 7);
} else if (typeof s === "number") {
s = s.toString();
if (s.length < 7) {
s = "0000000" + parseInt(code);
}
s = s.substring(s.length - 7);
} else {
return [];
}
const zip0 = parseInt(s.charAt(0));
let cache = zipcache[zip0];
if (!cache) {
const fn = `data/${zip0}.csv`;
let data = null;
if (
import.meta && import.meta.url && import.meta.url.startsWith("file://") &&
window.Deno
) {
const url = import.meta.url;
const path = url.substring("file://".length, url.lastIndexOf("/") + 1);
data = await CSV.fetch(path + fn);
} else {
const path = PostalCode.url;
data = await CSV.fetch(path + fn);
}
const json = {};
const csv = data; // CSV.decode(data);
for (const d of csv) {
const n = parseInt(d[0]);
if (!json[n]) {
json[n] = [d];
} else {
json[n].push(d);
}
}
cache = zipcache[zip0] = json;
}
const d = cache[parseInt(s)];
if (!d) return [];
return d.map((d) => {
return {
zipcode: s,
lgcode: d[1],
town: d[2],
townyomi: d[3],
};
});
};
const getMatchLenStr = (s1, s2) => {
const len = Math.min(s1.length, s2.length);
for (let i = 0; i < len; i++) {
if (s1[i] != s2[i]) {
return i;
}
}
return len;
};
/*
console.log(getMatchLenStr("abc", "abcd") == 3);
console.log(getMatchLenStr("abcchu", "abcd") == 3);
console.log(getMatchLenStr("acchu", "abcd") == 1);
*/
const fromZipCode1 = async (code) => {
const res = await fromZipCode(code);
if (!res || res.length == 0) {
return null;
} else if (res.length <= 1) {
return res[0];
}
let max = res[0].town;
let maxyomi = res[0].townyomi;
for (let i = 1; i < res.length; i++) {
const r = res[i];
const n = getMatchLenStr(r.town, max);
max = max.substring(0, n);
const n2 = getMatchLenStr(r.townyomi, maxyomi);
maxyomi = maxyomi.substring(0, n2);
if (!n) {
break;
}
}
res[0].town = max;
res[0].townyomi = maxyomi;
return res[0];
};
class PostalCode {
static url = "https://code4fukui.github.io/PostalCode/"; // default
static setDataPath(url) {
PostalCode.url = url;
}
static async decode(code) {
//return await fromZipCode(code);
const zip = await fromZipCode1(code);
if (!zip || !zip.lgcode) {
return null;
}
const city = LGCode.decode(LGCode.normalize(zip.lgcode));
const res = {};
res.lgcode = zip.lgcode;
if (city.length == 3) {
res.pref = city[0];
const city1 = city[1] == "特別区部" ? "" : city[1];
res.city = city1 + city[2];
} else {
res.pref = city[0];
res.city = city[1];
}
res.town = zip.town;
res.townyomi = zip.townyomi;
return res;
}
}
export { PostalCode };