Last change
on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Line | |
---|
1 | function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
|
---|
2 |
|
---|
3 | function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); }
|
---|
4 |
|
---|
5 | function con(n) {
|
---|
6 | return 0x80 | n & 0x3f;
|
---|
7 | }
|
---|
8 |
|
---|
9 | export function encode(str) {
|
---|
10 | var arr = str.split("").map(function (x) {
|
---|
11 | return x.charCodeAt(0);
|
---|
12 | });
|
---|
13 | return _encode(arr);
|
---|
14 | }
|
---|
15 |
|
---|
16 | function _encode(arr) {
|
---|
17 | if (arr.length === 0) {
|
---|
18 | return [];
|
---|
19 | }
|
---|
20 |
|
---|
21 | var _arr = _toArray(arr),
|
---|
22 | n = _arr[0],
|
---|
23 | ns = _arr.slice(1);
|
---|
24 |
|
---|
25 | if (n < 0) {
|
---|
26 | throw new Error("utf8");
|
---|
27 | }
|
---|
28 |
|
---|
29 | if (n < 0x80) {
|
---|
30 | return [n].concat(_toConsumableArray(_encode(ns)));
|
---|
31 | }
|
---|
32 |
|
---|
33 | if (n < 0x800) {
|
---|
34 | return [0xc0 | n >>> 6, con(n)].concat(_toConsumableArray(_encode(ns)));
|
---|
35 | }
|
---|
36 |
|
---|
37 | if (n < 0x10000) {
|
---|
38 | return [0xe0 | n >>> 12, con(n >>> 6), con(n)].concat(_toConsumableArray(_encode(ns)));
|
---|
39 | }
|
---|
40 |
|
---|
41 | if (n < 0x110000) {
|
---|
42 | return [0xf0 | n >>> 18, con(n >>> 12), con(n >>> 6), con(n)].concat(_toConsumableArray(_encode(ns)));
|
---|
43 | }
|
---|
44 |
|
---|
45 | throw new Error("utf8");
|
---|
46 | } |
---|
Note:
See
TracBrowser
for help on using the repository browser.