aboutsummaryrefslogtreecommitdiff
path: root/util/japanese.ts
blob: 8814e11403b937931f7046e5451567e3bab9f652 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import { UnicodeRange } from "./string.ts";
import "./number.ts";

declare global {
	interface String {
		/**
		 * @summary check if string is hiragana only
		 *
		 * @argument strict  don't allow ascii whitespace and punctuation (default: false)
		 *
		 * return `true` if at least one hiragana character is in string, and no other
		 * unicode ranges are found. ascii whitespace and punctuation is still allowed,
		 * but not counted as hiragana. this behavior can be turned off by setting
		 * `strict` to true
		 */
		hiraganaOnly(strict?: boolean): boolean
		/**
		 * @summary check if string is katakana only
		 *
		 * @argument strict  don't allow ascii whitespace and punctuation (default: false)
		 *
		 * return `true` if at least one katakana character is in string, and no other
		 * unicode ranges are found. ascii whitespace and punctuation is still allowed,
		 * but not counted as katakana. this behavior can be turned off by setting
		 * `strict` to true
		 */
		katakanaOnly(strict?: boolean): boolean
		/**
		 * @summary check if string is kanji only
		 *
		 * @argument strict  don't allow ascii whitespace and punctuation (default: false)
		 *
		 * return `true` if at least one kanji character is in string, and no other
		 * unicode ranges are found. ascii whitespace and punctuation is still allowed,
		 * but not counted as kanji. this behavior can be turned off by setting
		 * `strict` to true
		 */
		kanjiOnly(strict?: boolean): boolean
		/**
		 * @summary check if string is kana only
		 *
		 * @argument strict  don't allow ascii whitespace and punctuation (default: false)
		 *
		 * return `true` if at least one kana character is in string, and no other
		 * unicode ranges are found. ascii whitespace and punctuation is still allowed,
		 * but not counted as kana. this behavior can be turned off by setting `strict`
		 * to true
		 */
		kanaOnly(strict?: boolean): boolean
		/**
		 * @summary check if string is japanese only
		 *
		 * @argument strict  don't allow ascii whitespace and punctuation (default: false)
		 *
		 * return `true` if at least one japanese character is in string, and no other
		 * unicode ranges are found. ascii whitespace and punctuation is still allowed,
		 * but not counted as japanese. this behavior can be turned off by setting
		 * `strict` to true
		 */
		japaneseOnly(strict?: boolean): boolean

		/** @summary check if the string contains kanji characters */
		hasKanji(): boolean;

		/** @summary convert any half-width katakana to full-width */
		widenKatakana(): string;

		/** @summary convert any full-width katakana to hiragana */
		katakanaToHiragana(): string;

		/** @summary convert any kana (full and half-width) to full-width hiragana */
		normalizeKana(): string;
	}
}

enum StringOnlyReturnValue {
	TallyAdd,
	TallyIgnore,
	TallyStop,
}

/** @summary check tally for allowed scripts (internal use only) */
function stringOnly(input: string, check: (key: string, val: number) => StringOnlyReturnValue): boolean {
	var tally = input.rangeTally();
	var ok = false;
	for (var [key, val] of Object.entries(tally)) {
		switch(check(key, val)) {
			case StringOnlyReturnValue.TallyAdd: {
				ok = true;
				break;
			}
			case StringOnlyReturnValue.TallyIgnore: { break; }
			case StringOnlyReturnValue.TallyStop: { return false; }
		}
	}
	return ok;
}

String.prototype.hiraganaOnly = function(strict = false) {
	return stringOnly(this as string, (key, val) => {
		if (key == UnicodeRange.JapaneseFWHiragana)
			return StringOnlyReturnValue.TallyAdd; // count hiragana characters
		else if (!strict && key.startsWith("any-"))
			return StringOnlyReturnValue.TallyIgnore; // allow any- (ascii whitespace and punctuation)
		else if (val > 0)
			return StringOnlyReturnValue.TallyStop; // don't allow any other ranges
		return StringOnlyReturnValue.TallyIgnore;
	});
}

String.prototype.katakanaOnly = function(strict = false) {
	return stringOnly(this as string, (key, val) => {
		if ([UnicodeRange.JapaneseHWKatakana, UnicodeRange.JapaneseFWKatakana].includes(key as UnicodeRange))
			return StringOnlyReturnValue.TallyAdd; // count katakana characters
		else if (!strict && key.startsWith("any-"))
			return StringOnlyReturnValue.TallyIgnore; // allow any- (ascii whitespace and punctuation)
		else if (val > 0)
			return StringOnlyReturnValue.TallyStop; // don't allow any other ranges
		return StringOnlyReturnValue.TallyIgnore;
	});
}

String.prototype.kanjiOnly = function(strict = false) {
	return stringOnly(this as string, (key, val) => {
		if (key == UnicodeRange.JapaneseKanji)
			return StringOnlyReturnValue.TallyAdd; // count kanji characters
		else if (!strict && key.startsWith("any-"))
			return StringOnlyReturnValue.TallyIgnore; // allow any- (ascii whitespace and punctuation)
		else if (val > 0)
			return StringOnlyReturnValue.TallyStop; // don't allow any other ranges
		return StringOnlyReturnValue.TallyIgnore;
	});
}

String.prototype.kanaOnly = function(strict = false) {
	return stringOnly(this as string, (key, val) => {
		if ([UnicodeRange.JapaneseHWKatakana, UnicodeRange.JapaneseFWKatakana, UnicodeRange.JapaneseFWHiragana].includes(key as UnicodeRange))
			return StringOnlyReturnValue.TallyAdd; // count kana characters
		else if (!strict && key.startsWith("any-"))
			return StringOnlyReturnValue.TallyIgnore; // allow any- (ascii whitespace and punctuation)
		else if (val > 0)
			return StringOnlyReturnValue.TallyStop; // don't allow any other ranges
		return StringOnlyReturnValue.TallyIgnore;
	});
}

String.prototype.japaneseOnly = function(strict = false) {
	return stringOnly(this as string, (key, val) => {
		if (key.startsWith("jp-"))
			return StringOnlyReturnValue.TallyAdd; // count japanese characters
		else if (!strict && key.startsWith("any-"))
			return StringOnlyReturnValue.TallyIgnore; // allow any- (ascii whitespace and punctuation)
		else if (val > 0)
			return StringOnlyReturnValue.TallyStop; // don't allow any other ranges
		return StringOnlyReturnValue.TallyIgnore;
	});
}

String.prototype.widenKatakana = function() {
	const map: { [key: string]: string } = {
		"ァ": "ァ",
		"ア": "ア",
		"ィ": "ィ",
		"イ": "イ",
		"ゥ": "ゥ",
		"ウ": "ウ",
		"ェ": "ェ",
		"エ": "エ",
		"ォ": "ォ",
		"オ": "オ",
		"ガ": "ガ",
		"カ": "カ",
		"ギ": "ギ",
		"キ": "キ",
		"グ": "グ",
		"ク": "ク",
		"ゲ": "ゲ",
		"ケ": "ケ",
		"ゴ": "ゴ",
		"コ": "コ",
		"ザ": "ザ",
		"サ": "サ",
		"ジ": "ジ",
		"シ": "シ",
		"ズ": "ズ",
		"ス": "ス",
		"ゼ": "ゼ",
		"セ": "セ",
		"ゾ": "ゾ",
		"ソ": "ソ",
		"ダ": "ダ",
		"タ": "タ",
		"ヂ": "ヂ",
		"チ": "チ",
		"ヅ": "ヅ",
		"ッ": "ッ",
		"ツ": "ツ",
		"デ": "デ",
		"テ": "テ",
		"ド": "ド",
		"ト": "ト",
		"ナ": "ナ",
		"ニ": "ニ",
		"ヌ": "ヌ",
		"ネ": "ネ",
		"ノ": "ノ",
		"バ": "バ",
		"パ": "パ",
		"ハ": "ハ",
		"ビ": "ビ",
		"ピ": "ピ",
		"ヒ": "ヒ",
		"ブ": "ブ",
		"プ": "プ",
		"フ": "フ",
		"ベ": "ベ",
		"ペ": "ペ",
		"ヘ": "ヘ",
		"ボ": "ボ",
		"ポ": "ポ",
		"ホ": "ホ",
		"マ": "マ",
		"ミ": "ミ",
		"ム": "ム",
		"メ": "メ",
		"モ": "モ",
		"ャ": "ャ",
		"ヤ": "ヤ",
		"ュ": "ュ",
		"ユ": "ユ",
		"ョ": "ョ",
		"ヨ": "ヨ",
		"ラ": "ラ",
		"リ": "リ",
		"ル": "ル",
		"レ": "レ",
		"ロ": "ロ",
		"ワ": "ワ",
		"ヲ": "ヲ",
		"ン": "ン",
		"ヴ": "ヴ",
		"ヷ": "ヷ",
		"イ゙": "イ゙",
		"エ゙": "エ゙",
		"ヺ": "ヺ",
		"ー": "ー",
		" ": " ",
	};

	var out = "";
	outer:
	for (let i = 0; i < this.length; i++) {
		for (var key in map) {
			if (!this.substring(i).startsWith(key)) continue;
			out += map[key];
			i += key.length - 1;
			continue outer;
		}
		
		out += this[i];
	}
	return out;
}

String.prototype.katakanaToHiragana = function() {
	return this.map(char => {
		var code = char.codePointAt(0)!;
		if (0x30a1 <= code && code <= 0x30f6) return (code + (0x3041 - 0x30a1)).toChar();
		return char;
	})
}

String.prototype.normalizeKana = function() {
	return this.widenKatakana().katakanaToHiragana();
}

String.prototype.hasKanji = function() {
	for (var c of this)
		if ([UnicodeRange.JapaneseKanji].includes(c.range()))
			return true;
	return false;
}