-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathKUtils.js
More file actions
402 lines (377 loc) Β· 12.9 KB
/
KUtils.js
File metadata and controls
402 lines (377 loc) Β· 12.9 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
importPackage(java.io);
importPackage(java.net);
importPackage(android.os);
importPackage(android.text);
const sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();
const cho = ["γ±", "γ²", "γ΄", "γ·", "γΈ", "γΉ", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
" ];
const jung = ["γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
‘", "γ
’", "γ
£" ];
const jong = [ "", "γ±", "γ²", "γ³", "γ΄", "γ΅", "γΆ", "γ·", "γΉ", "γΊ", "γ»", "γΌ", "γ½", "γΎ", "γΏ", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
", "γ
" ];
const eng = "qwertyuiopasdfghjklzxcvbnm";
const kor = "γ
γ
γ·γ±γ
γ
γ
γ
γ
γ
γ
γ΄γ
γΉγ
γ
γ
γ
γ
£γ
γ
γ
γ
γ
γ
γ
‘";
const kor2engFilter = {
"γ²": "R",
"γΈ": "E",
"γ
": "Q",
"γ
": "T",
"γ
": "W",
"γ
": "O",
"γ
": "P",
"γ³": "γ±γ
",
"γ΅": "γ΄γ
",
"γΆ": "γ΄γ
",
"γΊ": "γΉγ±",
"γ»": "γΉγ
",
"γΌ": "γΉγ
",
"γ½": "γΉγ
",
"γΎ": "γΉγ
",
"γΏ": "γΉγ
",
"γ
": "γΉγ
",
"γ
": "γ
γ
",
"γ
": "γ
γ
",
"γ
": "γ
γ
",
"γ
": "γ
γ
£",
"γ
": "γ
γ
",
"γ
": "γ
γ
",
"γ
": "γ
γ
£",
"γ
’": "γ
‘γ
£"
}
const eng2korFilter = {
"Q": "γ
",
"W": "γ
",
"E": "γΈ",
"R": "γ²",
"T": "γ
",
"O": "γ
",
"P": "γ
"
}
Array.prototype.size = function(){
return this.length;
}
Array.prototype.isEmpty = function(){
return this.size() > 0
}
Array.prototype.isNotEmpty = function(){
return !this.isEmpty;
}
String.prototype.replaceAll = function(a, b){
return this.split(a).join(b);
}
String.prototype.checkSpell = function(){
var main = KUtils.getHtml("https://m.search.naver.com/p/csearch/ocontent/spellchecker.nhn?_callback=jQuery1124036706497182107567_1539623139313&q=" + this.replaceAll(" ", "+"));
return main.split("notag_html\":\"")[1].split("\"")[0];
}
String.prototype.replaceLast = function(a, b){
let isContains = this.contains(a);
if(isContains){
let lastIndex = this.lastIndexOf(a);
let string1 = this.substring(0, lastIndex);
let string2 = this.substring((lastIndex + a.length), this.length);
return string1 + b + string2;
}
else return this;
}
String.prototype.toInt = function(){
try{
return Number(this);
}
catch(e){
throw(e);
}
}
String.prototype.toBoolean = function(){
if(this == "true" || this == "false"){
return this.equals("true");
}
else throw("Type error!\nPrototype is must be true or false.");
}
String.prototype.trimAllLine = function(){
let preData = this.split("\n");
let data = [];
for(let i=0;i<preData.length;i++){
let cash = preData[i].trim();
data.push(cash);
}
return data.join("\n");
}
String.prototype.removeHtmlTag = function(){
return Html.fromHtml(this).toString();
}
String.prototype.contains = function(text){
return this.includes(text);
}
String.prototype.kotlin2js = function kotlin2js() {
return "\"" + this.replaceAll("\n", "ββ‘β‘β").match(/\$\{([^}]*)\}|\$([^\W]*)|\}?([^$]*)/gim).map(function(e){
if(typeof e !== "string") return "";
if(e.startsWith("$") && e.endsWith("}")){
let value = e.substr(1);
value = value.substr(1, value.lastIndexOf("}") - 1);
return "\"+(" + value + ")+\"";
}
else if(e.startsWith("$") && !e.endsWith("}")){
return "\"+" + e.substr(1) + "+\"";
}
else return e;
}).join("").replaceAll("ββ‘β‘β", "\\n") + "\"";
};
kotlin2js = (text) => {
text = text.replaceAll("\n", "ββ‘β‘β");
return "\"" + text.match(/\$\{([^}]*)\}|\$([^\W]*)|\}?([^$]*)/gim).map(function(e){
if(typeof e !== "string") return "";
if(e.startsWith("$") && e.endsWith("}")){
let value = e.substr(1);
value = value.substr(1, value.lastIndexOf("}") - 1);
return "\"+(" + value + ")+\"";
}
else if(e.startsWith("$") && !e.endsWith("}")){
return "\"+" + e.substr(1) + "+\"";
} else return e;
}).join("").replaceAll("ββ‘β‘β", "\\n") + "\"";
}
Array.prototype.contains = function(text){
let isContains = this.indexOf(text);
return (isContains == -1 ? false : true);
}
Array.prototype.remove = function(element, removeAll){
let index = this.indexOf(element);
if(index == -1) return;
if(!removeAll){
this.splice(index, 1);
}
else {
this.remove(element);
if(this.indexOf(element) > -1) this.remove(element, true);
}
}
const KUtils = {
save: (path, content, createRootFolder) => {
if(createRootFolder && createRootFolder != undefined){
let rootPath = path.replaceLast("/","β").split("β")[0];
KUtils.makeFolder(rootPath);
}
let file = new File(sdcard + "/" + path);
let output = new FileOutputStream(file);
let string = new java.lang.String(content);
output.write(string.getBytes());
output.close();
},
read: (path, notExist) => {
let file = new File(sdcard + "/" + path);
if(!file.exists()) return notExist;
let input = new FileInputStream(file);
let output = new InputStreamReader(input);
let reader = new BufferedReader(output);
let string = reader.readLine();
while(reader.readLine() != null){
string += "\n" + reader.readLine();
}
input.close();
output.close();
reader.close();
return string + "";
},
makeFolder: (path) => new File(sdcard + "/" + path).mkdirs(),
removeFolder: (path) => new File(sdcard + "/" + path).delete(),
makeFile: (path) => new File(sdcard + "/" + path).createNewFile(),
getHtml: (address) => {
try{
let policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
if(!address.contains("http")) address = "http://" + address;
let url = new URL(address);
let con = url.openConnection();
con.setConnectTimeout(5000);
con.setUseCaches(false);
let isr = new InputStreamReader(con.getInputStream());
let br = new BufferedReader(isr);
let str = br.readLine();
while(br.readLine() != null){
str += "\n" + br.readLine();
}
isr.close();
br.close();
con.disconnect();
return str + "";
}
catch(e){
throw e;
}
},
makeRandom: (min, max) => {
max = max + 1;
return Math.floor(Math.random() * (max - min) + min);
},
fastLog: (content) => {
let pre = KUtils.readLog();
KUtils.save("KUtils/Log/log.log", pre + "\n" + content, true)
},
readLog:_ => KUtils.read("KUtils/Log/log.log", "Log is empty."),
splitKor: (text) => Kor.split(text),
joinKor: (text) => Kor.longJoin(text),
similarityKor: (text, text2) => Kor.checkSameWord(text, text2),
kor2eng: (text) => Kor.kor2eng(text),
eng2kor: (text) => Kor.eng2kor(text)
}
const Kor = {
split: (str) => {
let string = "";
for(let i=0;i<str.length;i++){
if(str[i].match(/[κ°-ν£]/) == null) {
string += str[i];
continue;
}
let CompleteCode = str.charCodeAt(i);
let UniValue = CompleteCode - 0xAC00;
let Jong = UniValue % 28;
let Jung = ((UniValue - Jong) / 28 ) % 21;
let Cho = parseInt(((UniValue - Jong) / 28) / 21);
string += cho[Cho];
string += jung[Jung];
string += jong[Jong];
}
return string;
},
join: (str) => {
if(str.length == 3){
return String.fromCharCode((cho.indexOf(str[0]) * 588 + 44032) + (jung.indexOf(str[1]) * 28) + jong.indexOf(str[2]));
}
if(str.length == 2){
return String.fromCharCode((cho.indexOf(str[0]) * 588 + 44032) + (jung.indexOf(str[1]) * 28));
}
},
kSplit: (a) => {
var string = [];
for(var i=0;i<a.length;i++){
var c = a.substr(i);
if((cho.indexOf(c[0]) != -1 && jung.indexOf(c[1]) != -1 && jong.indexOf(c[2]) != -1 && jung.indexOf(c[3]) != -1) || (cho.indexOf(c[0]) != -1 && jung.indexOf(c[1]) != -1 && jong.indexOf(c[2]) == -1)){
string.push(c.substr(0, 2));
i += 2;
c = a.substr(i);
}
if(cho.indexOf(c[0]) != -1 && jung.indexOf(c[1]) != -1 && jong.indexOf(c[2]) != -1 && jung.indexOf(c[3]) == -1){
string.push(c.substr(0, 3));
i += 3;
c = a.substr(i);
}
else if(jong.indexOf(c[0]) == -1 && jung.indexOf(c[0]) == -1){
string.push(c[0]);
i++;
}
}
return string;
},
longJoin: (str) => {
var a = Kor.kSplit(str);
var string = "";
for(var i=0;i<a.length;i++){
if(a[i] !== undefined){
if(a[i].replace(/[γ±-γ
γ
-γ
£]/g,"") == ""){
string += Kor.join(a[i]);
}
else string += a[i];
}
}
return string;
},
checkSameWord: (str, comp) => {
comp = Kor.korDivide(comp, true);
let res = Kor.korDivide(str, true);
let i, j = 0;
let sim = 0;
let per = [];
for(let rp=0;rp<2;rp++) {
for(i=0;i<comp.length;i++) {
for(let k=j;k<res.length;k++) {
if(k-j>= 2) break;
if(comp[i] == res[k]) {
sim++;
j =k + 1;
break;
}
}
}
per[rp] = sim / (comp.length >= res.length ? comp.length : res.length) * 100;
let temp = comp;
comp = res;
res = temp;
sim = 0;
}
return Math.floor(per[0] >= per[1] ? per[0] : per[1]);
},
korDivide: (a, b) => {
try{
if(b == undefined) {
b = false;
}
let res = "";
for(let i=0;i<a.length;i++) {
if(a[i].match(/[κ°-ν£]/) == null){
res += a[i];
}
let temp = a.charCodeAt(i) - 44032;
let Jong = temp % 28;
let Jung = ((temp - Jong) / 28) % 21;
let Cho = (((temp - Jong) / 28) -Jung) / 21;
let jungsung = jung[Jung];
let jongsung = jong[Jong];
if(b) {
jongsung = jongsung.replace(/[γ
γ
γ
γ
γ
]/g, "γ·");
jongsung = jongsung.replace(/γΆ/g, "γ΄");
jongsung = jongsung.replace(/γ»/g, "γ
");
jongsung = jongsung.replace(/γ
/g, "γ
");
jungsung = jungsung.replace(/γ
γ
/g, "γ
");
jungsung = jungsung.replace(/γ
/g, "γ
");
}
res += cho[Cho] + jungsung + jongsung;
}
return res;
}
catch(e){
throw("Type error!\nParameter language is must be Korean.");
}
},
kor2eng: (text) => {
let textSplit = KUtils.splitKor(text);
let string = "";
textSplit.split("").map(function(e){
let cash = eng[kor.indexOf(e)];
if(cash != undefined) string += cash;
else {
let filterObj = Object.keys(kor2engFilter);
let filterIndex = filterObj.indexOf(e);
let filterChar = kor2engFilter[filterObj[filterIndex]];
if(filterChar == undefined){
string += e;
return;
}
if(filterChar.match(/[A-Z]/) == null){
filterChar.split("").map(function(e){
string += eng[kor.indexOf(e)];
});
}
else string += filterChar
}
});
return string;
},
eng2kor: (text) => {
let string = "";
text.split("").map(function(e){
let cash = kor[eng.indexOf(e)];
Log.d(cash);
if(cash != undefined) string += cash;
else {
let filterObj = Object.keys(eng2korFilter);
let filterIndex = filterObj.indexOf(e);
let filterChar = eng2korFilter[filterObj[filterIndex]];
if(filterChar == undefined){
string += e;
return;
}
else string += filterChar
}
});
var data = Kor.longJoin(string);
if(data.length > 0) return data;
else return "μμ΄ -> νκΈ λ³νμ μ€ν¨νμ΄μ π";
}
}