-
Notifications
You must be signed in to change notification settings - Fork 1
/
ZenkakuAlpha.js
111 lines (110 loc) · 2.31 KB
/
ZenkakuAlpha.js
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
class ZenkakuAlpha {
static isZen(c) {
if (c == null || c == "") {
return false;
}
if (`”“‘’`.indexOf(c) >= 0) {
return true;
}
const n = c.codePointAt();
return n >= 65281 && n <= 65374 || n == 12288; // include zen space
}
static isHan(c) {
if (c == null || c == "") {
return false;
}
const n = c.codePointAt();
return n >= 32 && n <= 126; // include han space
}
static lengthHan = (s) => {
let n = 0;
for (const c of s) {
if (ZenkakuAlpha.isHan(c)) {
n++;
} else {
n += 2;
}
}
return n;
}
static substringHan(s, start, end) {
const res = [];
let n = 0;
for (let i = 0; i < s.length; i++) {
const c = s[i];
const len = ZenkakuAlpha.isHan(c) ? 1 : 2;
if (n >= start) {
res.push(c);
}
n += len;
if (n >= end) {
break;
}
}
return res.join("");
};
static _convert(s, from, to) {
if (s == null) {
return s;
}
const res = [];
for (const c of s) {
const n = from.indexOf(c)
if (n >= 0) {
res.push(to[n]);
} else {
res.push(c);
}
}
return res.join("");
}
static toHan(s) {
if (s == null) {
return s;
}
const res = [];
for (const c of s) {
if (`”“`.indexOf(c) >= 0) {
res.push(`"`);
} else if (`‘’`.indexOf(c) >= 0) {
res.push(`'`);
} else {
const n = c.codePointAt(0);
if (n >= 65281 && n <= 65374) {
res.push(String.fromCodePoint(n - 65248));
} else if (n == 12288) {
res.push(" ");
} else {
res.push(c);
}
}
}
return res.join("");
}
static toZen(s) {
if (s == null) {
return s;
}
const res = [];
for (const c of s) {
const n = c.codePointAt(0);
if (n >= 33 && n <= 126) {
res.push(String.fromCodePoint(n + 65248));
} else if (n == 32) {
res.push(" ");
} else {
res.push(c);
}
}
return res.join("");
}
static toZens(s) {
if (s == null) {
return [];
}
if (s == '"') return ['"', '"', '”', '“'];
if (s == "'") return [''', ''', "'", "'"];
return [ZenkakuAlpha.toZen(s)];
}
}
export { ZenkakuAlpha };