base128-ascii 1.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -10
- package/dist/index.js +29 -46
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
Smaller than Base64, only use ASCII, can run in web browser.
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
```
|
|
4
|
+
npm i base128-ascii
|
|
5
|
+
```
|
|
6
6
|
|
|
7
7
|
```js
|
|
8
|
-
|
|
8
|
+
import base128 from "base128-ascii"
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
Char Code (ASCII) :
|
|
12
14
|
|
|
13
15
|
```js
|
|
14
|
-
|
|
15
|
-
`0LF3!Tl7`
|
|
16
|
+
'\0\1\2\3\4\5\6\7\b\t\n\v\f\r\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7F'
|
|
16
17
|
```
|
|
17
18
|
|
|
18
19
|
Test effect, Base128 is 102KiB smaller than Base64:
|
|
@@ -21,9 +22,9 @@ Test effect, Base128 is 102KiB smaller than Base64:
|
|
|
21
22
|
screenshot-45.519.jpg
|
|
22
23
|
file length: 682086
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
bytes length:
|
|
26
|
-
eval length:
|
|
25
|
+
encode:
|
|
26
|
+
bytes length: 804860
|
|
27
|
+
eval length: 779527
|
|
27
28
|
decoded length: 682086
|
|
28
29
|
equal: true
|
|
29
30
|
|
package/dist/index.js
CHANGED
|
@@ -1,35 +1,26 @@
|
|
|
1
1
|
export function encode(input) {
|
|
2
|
-
let
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
out[oi++] =
|
|
14
|
-
out[oi++] =
|
|
15
|
-
out[oi++] =
|
|
16
|
-
out[oi++] = Number((chunk >> 28n) & 127n);
|
|
17
|
-
out[oi++] = Number((chunk >> 21n) & 127n);
|
|
18
|
-
out[oi++] = Number((chunk >> 14n) & 127n);
|
|
19
|
-
out[oi++] = Number((chunk >> 7n) & 127n);
|
|
20
|
-
out[oi++] = Number(chunk & 127n);
|
|
2
|
+
let ii = 0, oi = 0, str, jstl;
|
|
3
|
+
const out = new Uint8Array(Math.ceil(input.length / 7 * 8));
|
|
4
|
+
while (ii < input.length) {
|
|
5
|
+
// 0 1 2 3 4 5 6 7
|
|
6
|
+
// in 00000000 11111111 22222222 33333333 44444444 55555555 66666666
|
|
7
|
+
// out _0000000 _0111111 _1122222 _2223333 _3333444 _4444455 _5555556 _6666666
|
|
8
|
+
/* 0 */ out[oi++] = input[ii] >> 1;
|
|
9
|
+
/* 1 */ out[oi++] = input[ii++] << 6 & 127 | input[ii] >> 2;
|
|
10
|
+
/* 2 */ out[oi++] = input[ii++] << 5 & 127 | input[ii] >> 3;
|
|
11
|
+
/* 3 */ out[oi++] = input[ii++] << 4 & 127 | input[ii] >> 4;
|
|
12
|
+
/* 4 */ out[oi++] = input[ii++] << 3 & 127 | input[ii] >> 5;
|
|
13
|
+
/* 5 */ out[oi++] = input[ii++] << 2 & 127 | input[ii] >> 6;
|
|
14
|
+
/* 6 */ out[oi++] = input[ii++] << 1 & 127 | input[ii] >> 7;
|
|
15
|
+
/* 7 */ out[oi++] = input[ii++] & 127;
|
|
21
16
|
}
|
|
22
|
-
if (suffixSize)
|
|
23
|
-
out = out.slice(0, Math.ceil(suffixSize / 7 * 8) - 8);
|
|
24
|
-
let str;
|
|
25
|
-
let jstl;
|
|
26
17
|
return {
|
|
27
18
|
uint8Array: out,
|
|
28
19
|
toString() {
|
|
29
20
|
return str ??= new TextDecoder().decode(out);
|
|
30
21
|
},
|
|
31
22
|
toJSTemplateLiterals() {
|
|
32
|
-
return jstl ??=
|
|
23
|
+
return jstl ??= `\`${this.toString().replace(/[\r\\`]|\${|\0\d?|<\/script/g, (match) => {
|
|
33
24
|
if (match == '\r')
|
|
34
25
|
return '\\r';
|
|
35
26
|
if (match == '</script')
|
|
@@ -40,33 +31,25 @@ export function encode(input) {
|
|
|
40
31
|
return '\\0';
|
|
41
32
|
}
|
|
42
33
|
return '\\' + match;
|
|
43
|
-
})
|
|
34
|
+
})}\``;
|
|
44
35
|
}
|
|
45
36
|
};
|
|
46
37
|
}
|
|
47
38
|
export function decode(input) {
|
|
48
|
-
|
|
49
|
-
const out = new Uint8Array(Math.
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
out[oi++] =
|
|
60
|
-
out[oi++] =
|
|
61
|
-
out[oi++] = Number((chunk >> 32n) & 255n);
|
|
62
|
-
out[oi++] = Number((chunk >> 24n) & 255n);
|
|
63
|
-
out[oi++] = Number((chunk >> 16n) & 255n);
|
|
64
|
-
out[oi++] = Number((chunk >> 8n) & 255n);
|
|
65
|
-
out[oi++] = Number(chunk & 255n);
|
|
39
|
+
let ii = 0, oi = 0, cache;
|
|
40
|
+
const update = () => cache = input.charCodeAt(ii++), out = new Uint8Array(Math.floor(input.length / 8 * 7));
|
|
41
|
+
while (ii < input.length) {
|
|
42
|
+
// 0 1 2 3 4 5 6 7
|
|
43
|
+
// in _0000000 _1111111 _2222222 _3333333 _4444444 _5555555 _6666666 _7777777
|
|
44
|
+
// out 00000001 11111122 22222333 33334444 44455555 55666666 67777777
|
|
45
|
+
/* 0 */ out[oi++] = update() << 1 | update() >> 6;
|
|
46
|
+
/* 1 */ out[oi++] = cache << 2 | update() >> 5;
|
|
47
|
+
/* 2 */ out[oi++] = cache << 3 | update() >> 4;
|
|
48
|
+
/* 3 */ out[oi++] = cache << 4 | update() >> 3;
|
|
49
|
+
/* 4 */ out[oi++] = cache << 5 | update() >> 2;
|
|
50
|
+
/* 5 */ out[oi++] = cache << 6 | update() >> 1;
|
|
51
|
+
/* 6 */ out[oi++] = cache << 7 | update();
|
|
66
52
|
}
|
|
67
|
-
const suffixSize = u8a[0] - 48;
|
|
68
|
-
if (suffixSize)
|
|
69
|
-
return out.slice(0, suffixSize - 7);
|
|
70
53
|
return out;
|
|
71
54
|
}
|
|
72
55
|
const base128 = {
|