base128-ascii 2.0.3 → 2.1.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.
Files changed (3) hide show
  1. package/README.md +5 -13
  2. package/dist/index.js +18 -21
  3. package/package.json +34 -34
package/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  Smaller than base64, only use ASCII, can run in web browser.
2
2
 
3
+ The project was born for [vite-plugin-singlefile-compression](https://github.com/bddjr/vite-plugin-singlefile-compression).
4
+
3
5
  ## Setup
4
6
 
5
7
  ```
@@ -17,20 +19,16 @@ const decoded = base128.decode(eval(encodedTemplate))
17
19
 
18
20
  ---
19
21
 
20
- Char Code (ASCII) :
21
-
22
- ```js
23
- '\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'
24
- ```
22
+ ## Effect
25
23
 
26
- Encode this jpg file, use base128 is 102KiB smaller than base64:
24
+ Encode this jpg file, use base128 is `109.85 KiB` smaller than base64:
27
25
 
28
26
  ```
29
27
  screenshot-45.519.jpg
30
28
  file length: 682086
31
29
 
32
30
  encode:
33
- bytes length: 804860
31
+ bytes length: 796961
34
32
  eval length: 779527
35
33
  decoded length: 682086
36
34
  equal: true
@@ -40,9 +38,3 @@ length: 909448
40
38
  ```
41
39
 
42
40
  ![](img.jpg)
43
-
44
- ---
45
-
46
- The project was born for [vite-plugin-singlefile-compression](https://github.com/bddjr/vite-plugin-singlefile-compression).
47
-
48
- [Unlicense](https://unlicense.org).
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  export function encode(input) {
2
- let ii = 0, oi = 0, str, jstl;
3
2
  const out = new Uint8Array(Math.ceil(input.length / 7 * 8));
3
+ let ii = 0, oi = 0;
4
4
  while (ii < input.length) {
5
5
  // 0 1 2 3 4 5 6 7
6
6
  // in 00000000 11111111 22222222 33333333 44444444 55555555 66666666
@@ -14,41 +14,38 @@ export function encode(input) {
14
14
  /* 6 */ out[oi++] = input[ii++] << 1 & 127 | input[ii] >> 7;
15
15
  /* 7 */ out[oi++] = input[ii++] & 127;
16
16
  }
17
+ let str, jstl;
17
18
  return {
18
19
  uint8Array: out,
19
20
  toString() {
20
21
  return str ??= new TextDecoder().decode(out);
21
22
  },
22
23
  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
- })}\``;
24
+ return jstl ??= `\`${this.toString().replace(/[\r\\`]|\${|<\/script/g, (match) => (match == '\r'
25
+ ? '\\r'
26
+ : match == '</script'
27
+ ? '<\\/script'
28
+ : '\\' + match))}\``;
35
29
  }
36
30
  };
37
31
  }
38
32
  export function decode(input) {
33
+ const out = new Uint8Array(Math.floor(input.length / 8 * 7));
39
34
  let ii = 0, oi = 0, cache;
40
- const update = () => cache = input.charCodeAt(ii++), out = new Uint8Array(Math.floor(input.length / 8 * 7));
35
+ const next = () => ((cache = input.charCodeAt(ii++)) >> 7
36
+ ? cache = 0 // In HTML, 0 is likely to be converted to 65533
37
+ : cache);
41
38
  while (ii < input.length) {
42
39
  // 0 1 2 3 4 5 6 7
43
40
  // in _0000000 _1111111 _2222222 _3333333 _4444444 _5555555 _6666666 _7777777
44
41
  // 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();
42
+ /* 0 */ out[oi++] = next() << 1 | next() >> 6;
43
+ /* 1 */ out[oi++] = cache << 2 | next() >> 5;
44
+ /* 2 */ out[oi++] = cache << 3 | next() >> 4;
45
+ /* 3 */ out[oi++] = cache << 4 | next() >> 3;
46
+ /* 4 */ out[oi++] = cache << 5 | next() >> 2;
47
+ /* 5 */ out[oi++] = cache << 6 | next() >> 1;
48
+ /* 6 */ out[oi++] = cache << 7 | next();
52
49
  }
53
50
  return out;
54
51
  }
package/package.json CHANGED
@@ -1,34 +1,34 @@
1
- {
2
- "name": "base128-ascii",
3
- "version": "2.0.3",
4
- "main": "dist/index.js",
5
- "type": "module",
6
- "scripts": {
7
- "build": "rimraf dist && tsc",
8
- "test": "npm run build && node test.js",
9
- "prepublishOnly": "npm run build"
10
- },
11
- "repository": {
12
- "type": "git",
13
- "url": "git+https://github.com/bddjr/base128.git"
14
- },
15
- "author": "bddjr",
16
- "license": "Unlicense",
17
- "description": "",
18
- "files": [
19
- "dist",
20
- "README.md",
21
- "package.json",
22
- "package-lock.json"
23
- ],
24
- "keywords": [
25
- "base128",
26
- "base64",
27
- "ascii"
28
- ],
29
- "dependencies": {
30
- "@types/node": "^22.10.5",
31
- "rimraf": "^6.0.1",
32
- "typescript": "^5.7.3"
33
- }
34
- }
1
+ {
2
+ "name": "base128-ascii",
3
+ "version": "2.1.0",
4
+ "main": "dist/index.js",
5
+ "type": "module",
6
+ "scripts": {
7
+ "build": "rimraf dist && tsc",
8
+ "test": "npm run build && node test.js",
9
+ "prepublishOnly": "npm run build"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/bddjr/base128.git"
14
+ },
15
+ "author": "bddjr",
16
+ "license": "Unlicense",
17
+ "description": "",
18
+ "files": [
19
+ "dist",
20
+ "README.md",
21
+ "package.json",
22
+ "package-lock.json"
23
+ ],
24
+ "keywords": [
25
+ "base128",
26
+ "base64",
27
+ "ascii"
28
+ ],
29
+ "dependencies": {
30
+ "@types/node": "^22.10.5",
31
+ "rimraf": "^6.0.1",
32
+ "typescript": "^5.7.3"
33
+ }
34
+ }