-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
122 lines (108 loc) · 2.11 KB
/
index.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
112
113
114
115
116
117
118
119
120
121
122
//initialize
const ansiSymbolMap = [];
const unicodeSymbolMap = [];
for (let i = 178; i <= 252; i += 2) {
//uppercase
ansiSymbolMap.push(i);
unicodeSymbolMap.push(1328 + (i - 176) / 2);
//lowercase
ansiSymbolMap.push(i + 1);
unicodeSymbolMap.push(1376 + (i - 176) / 2);
}
const customsMap = [{
//և
ansi: 168,
unicode: 0x587,
}, {
//•
ansi: 183,
unicode: 8226,
}, {
//գ
ansi: 8226,
unicode: 1379,
}, {
//՚
ansi: 39,
unicode: 0x55A,
}, {
//՛
ansi: 176,
unicode: 0x55B
}, {
//՜
ansi: 175,
unicode: 0x55C
}, {
//՝
ansi: 170,
unicode: 0x55D
}, {
//՞
ansi: 177,
unicode: 0x55E
}, {
//։
ansi: 163,
unicode: 0x589
}, {
//֊
ansi: 173,
unicode: 0x58A
}, {
//«
ansi: 167,
unicode: 0xAB
}, {
//»
ansi: 166,
unicode: 0xBB
}, {
//,
ansi: 171,
unicode: 0x2C
}, {
//.
ansi: 169,
unicode: 0x2E
}, {
//…
ansi: 174,
unicode: 0x2026
}];
for (let custom of customsMap) {
ansiSymbolMap.push(custom.ansi);
unicodeSymbolMap.push(custom.unicode);
}
const converter = (input, isAnsiToUnicode) => {
let arrFrom = isAnsiToUnicode ? ansiSymbolMap : unicodeSymbolMap;
let arrTo = isAnsiToUnicode ? unicodeSymbolMap : ansiSymbolMap;
let output = '';
for (let i = 0; i < input.length; i++) {
const symbol = input.charCodeAt(i);
let isInFrom = false;
for (let j = 0; j < arrFrom.length; j++) {
if (symbol === arrFrom[j]) {
if (arrTo[j] === '') {
isInFrom = true;
break;
}
output += String.fromCharCode(arrTo[j]);
isInFrom = true;
break;
}
}
if (!isInFrom) {
output += String.fromCharCode(symbol);
}
}
return output;
};
const ansiToUnicode = (text) => {
return converter(text, true);
};
const unicodeToAnsi = (text) => {
return converter(text, false);
};
exports.toUnicode = ansiToUnicode;
exports.toAnsi = unicodeToAnsi;