base128-ascii 2.0.2 → 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.
package/LICENSE.txt CHANGED
@@ -1,8 +1,24 @@
1
- The MIT License (MIT)
2
- Copyright © 2025 bddjr
1
+ This is free and unencumbered software released into the public domain.
3
2
 
4
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
5
7
 
6
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
7
15
 
8
- THE SOFTWARE IS PROVIDED AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <https://unlicense.org/>
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
  ```
@@ -8,6 +10,7 @@ npm i base128-ascii
8
10
 
9
11
  ```js
10
12
  import base128 from "base128-ascii"
13
+ import fs from 'fs'
11
14
 
12
15
  const encodedTemplate = base128.encode(Uint8Array.from(fs.readFileSync("example.gz"))).toJSTemplateLiterals()
13
16
 
@@ -16,20 +19,16 @@ const decoded = base128.decode(eval(encodedTemplate))
16
19
 
17
20
  ---
18
21
 
19
- Char Code (ASCII) :
20
-
21
- ```js
22
- '\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'
23
- ```
22
+ ## Effect
24
23
 
25
- 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:
26
25
 
27
26
  ```
28
27
  screenshot-45.519.jpg
29
28
  file length: 682086
30
29
 
31
30
  encode:
32
- bytes length: 804860
31
+ bytes length: 796961
33
32
  eval length: 779527
34
33
  decoded length: 682086
35
34
  equal: true
@@ -39,9 +38,3 @@ length: 909448
39
38
  ```
40
39
 
41
40
  ![](img.jpg)
42
-
43
- ---
44
-
45
- The project was born for [vite-plugin-singlefile-compression](https://github.com/bddjr/vite-plugin-singlefile-compression).
46
-
47
- Made by bddjr, using MIT License.
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.2",
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": "MIT",
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
+ }