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
|
import { UnicodeRange } from "../util/string.ts";
enum StringOnlyReturnValue {
TallyAdd,
TallyIgnore,
TallyStop,
}
export default class JapaneseString extends String {
/** @summary check tally for allowed scripts (internal use only) */
private stringOnly(check: (key: string, val: number) => StringOnlyReturnValue): boolean {
var tally = this.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;
}
/**
* @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 = false) {
return this.stringOnly((key, val) => {
if (key == UnicodeRange.JapaneseHiragana)
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;
});
}
/**
* @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 = false) {
return this.stringOnly((key, val) => {
if (key == UnicodeRange.JapaneseKatakana)
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;
});
}
/**
* @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 = false) {
return this.stringOnly((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;
});
}
/**
* @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 = false) {
return this.stringOnly((key, val) => {
if (key == UnicodeRange.JapaneseHiragana || key == UnicodeRange.JapaneseKatakana)
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;
});
}
/**
* @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 = false) {
return this.stringOnly((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;
});
}
}
|