-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
186 lines (158 loc) · 3.69 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
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
/**
* Convert binary number to decimal number.
* @param {*} binaryNum
*/
const binaryNum2Decimal = (binaryNum) => {
if (isNaN(binaryNum)) {
return;
}
return binaryNum.toString(10);
};
/**
* Convert binary number to hexadecimal number.
* @param {*} binaryNum
*/
const binaryNum2Hex = (binaryNum) => {
if (isNaN(binaryNum)) {
return;
}
return binaryNum.toString(16);
};
/**
* Get one byte hexadecimal value.
* @param {*} codepoint
*/
const getOneByteHexArr = (codepoint) => {
return [binaryNum2Hex(codepoint & 0x7f)];
};
/**
* Get two bytes hexadecimal value.
* @param {*} codepoint
*/
const getTwoBytesHexArr = (codepoint) => {
const fstByteLow = (codepoint >> 6) & 0x1f;
const fstByteHigh = 0xdf & 0xe0;
const secByteLow = codepoint & 0x3f;
const secByteHigh = 0xbf & 0xc0;
return [
binaryNum2Hex(fstByteHigh | fstByteLow),
binaryNum2Hex(secByteHigh | secByteLow),
];
};
/**
* Get three bytes hexadecimal value.
* @param {*} codepoint
*/
const getThreeBytesHexArr = (codepoint) => {
const fstByteLow = (codepoint >> 12) & 0x0f;
const fstByteHigh = 0xef & 0xe0;
const secByteLow = (codepoint >> 6) & 0x3f;
const secByteHigh = 0xbf & 0xc0;
const trdByteLow = codepoint & 0x3f;
const trdByteHigh = 0xbf & 0xc0;
return [
binaryNum2Hex(fstByteHigh | fstByteLow),
binaryNum2Hex(secByteHigh | secByteLow),
binaryNum2Hex(trdByteHigh | trdByteLow),
];
};
/**
* Get four bytes hexadecimal value.
* @param {*} codepoint
*/
const getFourBytesHexArr = (codepoint) => {
const fstByteLow = (codepoint >> 18) & 0x07;
const fstByteHigh = 0xf7 & 0xc0;
const secByteLow = (codepoint >> 12) & 0x3f;
const secByteHigh = 0xbf & 0xc0;
const trdByteLow = (codepoint >> 6) & 0x3f;
const trdByteHigh = 0xbf & 0xc0;
const fthByteLow = codepoint & 0x3f;
const fthByteHigh = 0xbf & 0xc0;
return [
binaryNum2Hex(fstByteHigh | fstByteLow),
binaryNum2Hex(secByteHigh | secByteLow),
binaryNum2Hex(trdByteHigh | trdByteLow),
binaryNum2Hex(fthByteHigh | fthByteLow),
];
};
/**
* Get code point bytes count in memory.
* @param {*} codepoint
*/
const getCodePointBytesCount = (codepoint) => {
if (isNaN(codepoint)) {
return;
}
const codePointRanges = [
{
min: 0x00000000,
max: 0x0000007f,
count: 1,
},
{
min: 0x00000080,
max: 0x000007ff,
count: 2,
},
{
min: 0x00000800,
max: 0x0000ffff,
count: 3,
},
{
min: 0x00010000,
max: 0x0010ffff,
count: 4,
},
];
return codePointRanges.find(
(range) =>
codepoint >= binaryNum2Decimal(range.min) &&
codepoint <= binaryNum2Decimal(range.max)
).count;
};
/**
* Get encoded UTF8 hex array.
* @param {*} codepoint
*/
const getUTF8EncodedArr = (codepoint) => {
const count = getCodePointBytesCount(codepoint);
const funMap = {
1: getOneByteHexArr,
2: getTwoBytesHexArr,
3: getThreeBytesHexArr,
4: getFourBytesHexArr,
};
return funMap[count](codepoint);
};
/**
* Get char UTF8 code with prefix.
* @param {*} chars hexArr
* @param {*} prefix single char code string prefix
*/
const getSingleCharUTF8Code = (hexArr = [], prefix = "\\x") => {
let res = "";
if (hexArr.length > 0) {
hexArr.forEach((hexItem) => {
res += prefix + hexItem.toUpperCase();
});
}
return res;
};
/**
* Get UTF8 string.
* @param {*} str
*/
exports.encode = (str, prefix = "\\x") => {
let res = "";
const chars = [...str];
if (chars.length > 0) {
chars.forEach((char) => {
const charCodePoint = char.codePointAt(0);
res += getSingleCharUTF8Code(getUTF8EncodedArr(charCodePoint), prefix);
});
}
return res;
};
export default { encode };