base128-ascii 0.0.2 → 2.0.0

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 CHANGED
@@ -1,29 +1,30 @@
1
1
  Smaller than Base64, only use ASCII, can run in web browser.
2
2
 
3
- ---
4
-
5
- Char Code (ASCII) :
3
+ ```
4
+ npm i base128-ascii
5
+ ```
6
6
 
7
7
  ```js
8
- '\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';
8
+ import base128 from "base128-ascii"
9
9
  ```
10
10
 
11
- The first byte indicates the length of the last incomplete block, 0 means no incomplete block.
11
+ ---
12
+
13
+ Char Code (ASCII) :
12
14
 
13
15
  ```js
14
- `4LF3 `
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
- Test effect, Base128 is 10KB smaller than Base64:
19
+ Test effect, Base128 is 102KiB smaller than Base64:
19
20
 
20
21
  ```
21
22
  screenshot-45.519.jpg
22
23
  file length: 682086
23
24
 
24
- encodeToTemplateLiterals:
25
- bytes length: 804861
26
- eval length: 779528
25
+ encode:
26
+ bytes length: 804860
27
+ eval length: 779527
27
28
  decoded length: 682086
28
29
  equal: true
29
30
 
package/dist/index.d.ts CHANGED
@@ -1,10 +1,11 @@
1
- export declare function encode(input: Uint8Array): string;
2
- export declare function stringToTemplateLiterals(input: string): string;
3
- export declare function encodeToTemplateLiterals(input: Uint8Array): string;
1
+ export declare function encode(input: Uint8Array): {
2
+ uint8Array: Uint8Array<ArrayBuffer>;
3
+ toString(): string;
4
+ toJSTemplateLiterals(): string;
5
+ };
4
6
  export declare function decode(input: string): Uint8Array<ArrayBuffer>;
5
7
  declare const base128: {
6
8
  encode: typeof encode;
7
- encodeToTemplateLiterals: typeof encodeToTemplateLiterals;
8
9
  decode: typeof decode;
9
10
  };
10
11
  export default base128;
package/dist/index.js CHANGED
@@ -1,68 +1,62 @@
1
1
  export function encode(input) {
2
- let out = new Uint8Array(Math.ceil(input.length / 7) * 8 + 1);
3
- const suffixSize = input.length % 7;
4
- out[0] = suffixSize + 48;
5
- for (let ii = 0, oi = 1; ii < input.length;) {
6
- let chunk = BigInt(input[ii++] || 0) << 48n;
7
- chunk |= BigInt(input[ii++] || 0) << 40n;
8
- chunk |= BigInt(input[ii++] || 0) << 32n;
9
- chunk |= BigInt(input[ii++] || 0) << 24n;
10
- chunk |= BigInt(input[ii++] || 0) << 16n;
11
- chunk |= BigInt(input[ii++] || 0) << 8n;
12
- chunk |= BigInt(input[ii++] || 0);
13
- out[oi++] = Number(chunk >> 49n);
14
- out[oi++] = Number((chunk >> 42n) & 127n);
15
- out[oi++] = Number((chunk >> 35n) & 127n);
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
- return new TextDecoder().decode(out);
25
- }
26
- export function stringToTemplateLiterals(input) {
27
- return '`' + input.replace(/[\r\\`]|\${|\0\d?/g, (match) => match[0] == '\0'
28
- ? match.length == 2
29
- ? '\\x00' + match[1]
30
- : '\\0'
31
- : match == '\r'
32
- ? '\\r'
33
- : '\\' + match) + '`';
34
- }
35
- export function encodeToTemplateLiterals(input) {
36
- return stringToTemplateLiterals(encode(input));
17
+ return {
18
+ uint8Array: out,
19
+ toString() {
20
+ return str ??= new TextDecoder().decode(out);
21
+ },
22
+ toJSTemplateLiterals() {
23
+ return jstl ??= '`' + this.toString().replace(/[\r\\`]|\${|\0\d?|<\/script/g, (match) => {
24
+ if (match == '\r')
25
+ return '\\r';
26
+ if (match == '</script')
27
+ return '<\\/script';
28
+ if (match[0] == '\0') {
29
+ if (match.length == 2)
30
+ return '\\x00' + match[1];
31
+ return '\\0';
32
+ }
33
+ return '\\' + match;
34
+ }) + '`';
35
+ }
36
+ };
37
37
  }
38
38
  export function decode(input) {
39
- const u8a = new TextEncoder().encode(input);
40
- const out = new Uint8Array(Math.ceil((u8a.length - 1) / 8) * 7);
41
- for (let ii = 1, oi = 0; ii < u8a.length;) {
42
- let chunk = BigInt(u8a[ii++] || 0) << 49n;
43
- chunk |= BigInt(u8a[ii++] || 0) << 42n;
44
- chunk |= BigInt(u8a[ii++] || 0) << 35n;
45
- chunk |= BigInt(u8a[ii++] || 0) << 28n;
46
- chunk |= BigInt(u8a[ii++] || 0) << 21n;
47
- chunk |= BigInt(u8a[ii++] || 0) << 14n;
48
- chunk |= BigInt(u8a[ii++] || 0) << 7n;
49
- chunk |= BigInt(u8a[ii++] || 0);
50
- out[oi++] = Number(chunk >> 48n);
51
- out[oi++] = Number((chunk >> 40n) & 255n);
52
- out[oi++] = Number((chunk >> 32n) & 255n);
53
- out[oi++] = Number((chunk >> 24n) & 255n);
54
- out[oi++] = Number((chunk >> 16n) & 255n);
55
- out[oi++] = Number((chunk >> 8n) & 255n);
56
- out[oi++] = Number(chunk & 255n);
39
+ let ii = 0, oi = 0, cache;
40
+ const out = new Uint8Array(Math.floor(input.length / 8 * 7));
41
+ function update() {
42
+ return cache = input.charCodeAt(ii++) || 0;
43
+ }
44
+ while (ii < input.length) {
45
+ // 0 1 2 3 4 5 6 7
46
+ // in _0000000 _1111111 _2222222 _3333333 _4444444 _5555555 _6666666 _7777777
47
+ // out 00000001 11111122 22222333 33334444 44455555 55666666 67777777
48
+ /* 0 */ out[oi++] = update() << 1 | update() >> 6;
49
+ /* 1 */ out[oi++] = cache << 2 | update() >> 5;
50
+ /* 2 */ out[oi++] = cache << 3 | update() >> 4;
51
+ /* 3 */ out[oi++] = cache << 4 | update() >> 3;
52
+ /* 4 */ out[oi++] = cache << 5 | update() >> 2;
53
+ /* 5 */ out[oi++] = cache << 6 | update() >> 1;
54
+ /* 6 */ out[oi++] = cache << 7 | update();
57
55
  }
58
- const suffixSize = u8a[0] - 48;
59
- if (suffixSize)
60
- return out.slice(0, suffixSize - 7);
61
56
  return out;
62
57
  }
63
58
  const base128 = {
64
59
  encode,
65
- encodeToTemplateLiterals,
66
60
  decode
67
61
  };
68
62
  export default base128;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "base128-ascii",
3
- "version": "0.0.2",
3
+ "version": "2.0.0",
4
4
  "main": "dist/index.js",
5
5
  "type": "module",
6
6
  "scripts": {