base128-ascii 2.1.8 → 2.1.9

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,50 +1,50 @@
1
- Smaller than base64, only use ASCII, can run in web browser.
2
-
3
- The project was born for [vite-plugin-singlefile-compression](https://github.com/bddjr/vite-plugin-singlefile-compression).
4
-
5
- ## Setup
6
-
7
- ### npm
8
-
9
- ```
10
- npm i base128-ascii
11
- ```
12
-
13
- ```js
14
- import base128 from "base128-ascii"
15
- import fs from 'fs'
16
-
17
- const encodedTemplate = base128.encode(fs.readFileSync("example.gz")).toJSTemplateLiterals()
18
-
19
- const decoded = base128.decode(eval(encodedTemplate))
20
- ```
21
-
22
- ### Browser
23
-
24
- ```html browser
1
+ Smaller than base64, only use ASCII, can run in web browser.
2
+
3
+ The project was born for [vite-plugin-singlefile-compression](https://github.com/bddjr/vite-plugin-singlefile-compression).
4
+
5
+ ## Setup
6
+
7
+ ### npm
8
+
9
+ ```
10
+ npm i base128-ascii
11
+ ```
12
+
13
+ ```js
14
+ import base128 from "base128-ascii"
15
+ import fs from 'fs'
16
+
17
+ const encodedTemplate = base128.encode(fs.readFileSync("example.gz")).toJSTemplateLiterals()
18
+
19
+ const decoded = base128.decode(eval(encodedTemplate))
20
+ ```
21
+
22
+ ### Browser
23
+
24
+ ```html browser
25
25
  <script>
26
- {class EncodeOutput{constructor(t){this.uint8Array=t}toString(){return(new TextDecoder).decode(this.uint8Array)}toJSTemplateLiterals(){return`\`${this.toString().replace(/[\r\\`]|\${|<\/script/g,t=>"\r"==t?"\\r":"<\/script"==t?"<\\/script":"\\"+t)}\``}}self.base128={EncodeOutput,encode(t){"string"==typeof t&&(t=(new TextEncoder).encode(t));for(var e=new Uint8Array(Math.ceil(t.length/7*8)),r=0,n=0;r<t.length;)e[n++]=t[r]>>1,e[n++]=t[r++]<<6&127|t[r]>>2,e[n++]=t[r++]<<5&127|t[r]>>3,e[n++]=t[r++]<<4&127|t[r]>>4,e[n++]=t[r++]<<3&127|t[r]>>5,e[n++]=t[r++]<<2&127|t[r]>>6,e[n++]=t[r++]<<1&127|t[r]>>7,e[n++]=127&t[r++];return new EncodeOutput(e)},decode(t){for(var e,r=new Uint8Array(Math.floor(t.length/8*7)),n=0,o=0,c=()=>(e=t.charCodeAt(n++))>>7?e=0:e;n<t.length;)r[o++]=c()<<1|c()>>6,r[o++]=e<<2|c()>>5,r[o++]=e<<3|c()>>4,r[o++]=e<<4|c()>>3,r[o++]=e<<5|c()>>2,r[o++]=e<<6|c()>>1,r[o++]=e<<7|c();return r}}}
26
+ {class EncodeOutput{constructor(t){this.uint8Array=t}toString(){return(new TextDecoder).decode(this.uint8Array)}toJSTemplateLiterals(){return`\`${this.toString().replace(/[\r\\`]|\${|<\/script/g,t=>"\r"==t?"\\r":"<\/script"==t?"<\\/script":"\\"+t)}\``}}self.base128={EncodeOutput,encode(t){"string"==typeof t&&(t=(new TextEncoder).encode(t));for(var e=t.length,r=new Uint8Array(Math.ceil(e/7*8)),n=0,o=0;n<e;)r[o++]=t[n]>>1,r[o++]=t[n++]<<6&127|t[n]>>2,r[o++]=t[n++]<<5&127|t[n]>>3,r[o++]=t[n++]<<4&127|t[n]>>4,r[o++]=t[n++]<<3&127|t[n]>>5,r[o++]=t[n++]<<2&127|t[n]>>6,r[o++]=t[n++]<<1&127|t[n]>>7,r[o++]=127&t[n++];return new EncodeOutput(r)},decode(t){for(var e,r=t.length,n=new Uint8Array(Math.floor(r/8*7)),o=0,c=0,i=()=>(e=t.charCodeAt(o++))>127?e=0:e;o<r;)n[c++]=i()<<1|i()>>6,n[c++]=e<<2|i()>>5,n[c++]=e<<3|i()>>4,n[c++]=e<<4|i()>>3,n[c++]=e<<5|i()>>2,n[c++]=e<<6|i()>>1,n[c++]=e<<7|i();return n}}}
27
27
  </script>
28
- ```
29
-
30
- ---
31
-
32
- ## Effect
33
-
34
- Encode this jpg file, use base128 is `109.85 KiB` smaller than base64:
35
-
36
- ```
37
- screenshot-45.519.jpg
38
- file length: 682086
39
-
40
- encode:
41
- bytes length: 796961
42
- eval length: 779527
43
- decoded length: 682086
44
- equal: true
45
-
46
- base64:
47
- length: 909448
48
- ```
49
-
50
- ![img](img.jpg)
28
+ ```
29
+
30
+ ---
31
+
32
+ ## Effect
33
+
34
+ Encode this jpg file, use base128 is `109.85 KiB` smaller than base64:
35
+
36
+ ```
37
+ screenshot-45.519.jpg
38
+ file length: 682086
39
+
40
+ encode:
41
+ bytes length: 796961
42
+ eval length: 779527
43
+ decoded length: 682086
44
+ equal: true
45
+
46
+ base64:
47
+ length: 909448
48
+ ```
49
+
50
+ ![img](img.jpg)
@@ -0,0 +1,57 @@
1
+ {
2
+ class EncodeOutput {
3
+ constructor(out) {
4
+ this.uint8Array = out;
5
+ }
6
+ toString() {
7
+ return new TextDecoder().decode(this.uint8Array);
8
+ }
9
+ toJSTemplateLiterals() {
10
+ return `\`${this.toString().replace(/[\r\\`]|\${|<\/script/g, (match) => (match == '\r'
11
+ ? '\\r'
12
+ : match == '</script'
13
+ ? '<\\/script'
14
+ : '\\' + match))}\``;
15
+ }
16
+ }
17
+ self.base128 = {
18
+ EncodeOutput,
19
+ encode(input) {
20
+ if (typeof input == 'string')
21
+ input = new TextEncoder().encode(input);
22
+ var il = input.length, out = new Uint8Array(Math.ceil(il / 7 * 8)), ii = 0, oi = 0;
23
+ while (ii < il) {
24
+ // 0 1 2 3 4 5 6 7
25
+ // in 00000000 11111111 22222222 33333333 44444444 55555555 66666666
26
+ // out _0000000 _0111111 _1122222 _2223333 _3333444 _4444455 _5555556 _6666666
27
+ /* 0 */ out[oi++] = input[ii] >> 1;
28
+ /* 1 */ out[oi++] = input[ii++] << 6 & 127 | input[ii] >> 2;
29
+ /* 2 */ out[oi++] = input[ii++] << 5 & 127 | input[ii] >> 3;
30
+ /* 3 */ out[oi++] = input[ii++] << 4 & 127 | input[ii] >> 4;
31
+ /* 4 */ out[oi++] = input[ii++] << 3 & 127 | input[ii] >> 5;
32
+ /* 5 */ out[oi++] = input[ii++] << 2 & 127 | input[ii] >> 6;
33
+ /* 6 */ out[oi++] = input[ii++] << 1 & 127 | input[ii] >> 7;
34
+ /* 7 */ out[oi++] = input[ii++] & 127;
35
+ }
36
+ return new EncodeOutput(out);
37
+ },
38
+ decode(input) {
39
+ var il = input.length, out = new Uint8Array(Math.floor(il / 8 * 7)), ii = 0, oi = 0, cache, next = () => ((cache = input.charCodeAt(ii++)) > 127
40
+ ? cache = 0 // In HTML, 0 is likely to be converted to 65533 (�)
41
+ : cache);
42
+ while (ii < il) {
43
+ // 0 1 2 3 4 5 6 7
44
+ // in _0000000 _1111111 _2222222 _3333333 _4444444 _5555555 _6666666 _7777777
45
+ // out 00000001 11111122 22222333 33334444 44455555 55666666 67777777
46
+ /* 0 */ out[oi++] = next() << 1 | next() >> 6;
47
+ /* 1 */ out[oi++] = cache << 2 | next() >> 5;
48
+ /* 2 */ out[oi++] = cache << 3 | next() >> 4;
49
+ /* 3 */ out[oi++] = cache << 4 | next() >> 3;
50
+ /* 4 */ out[oi++] = cache << 5 | next() >> 2;
51
+ /* 5 */ out[oi++] = cache << 6 | next() >> 1;
52
+ /* 6 */ out[oi++] = cache << 7 | next();
53
+ }
54
+ return out;
55
+ }
56
+ };
57
+ }
@@ -1 +1 @@
1
- {class EncodeOutput{constructor(t){this.uint8Array=t}toString(){return(new TextDecoder).decode(this.uint8Array)}toJSTemplateLiterals(){return`\`${this.toString().replace(/[\r\\`]|\${|<\/script/g,t=>"\r"==t?"\\r":"<\/script"==t?"<\\/script":"\\"+t)}\``}}self.base128={EncodeOutput,encode(t){"string"==typeof t&&(t=(new TextEncoder).encode(t));for(var e=new Uint8Array(Math.ceil(t.length/7*8)),r=0,n=0;r<t.length;)e[n++]=t[r]>>1,e[n++]=t[r++]<<6&127|t[r]>>2,e[n++]=t[r++]<<5&127|t[r]>>3,e[n++]=t[r++]<<4&127|t[r]>>4,e[n++]=t[r++]<<3&127|t[r]>>5,e[n++]=t[r++]<<2&127|t[r]>>6,e[n++]=t[r++]<<1&127|t[r]>>7,e[n++]=127&t[r++];return new EncodeOutput(e)},decode(t){for(var e,r=new Uint8Array(Math.floor(t.length/8*7)),n=0,o=0,c=()=>(e=t.charCodeAt(n++))>>7?e=0:e;n<t.length;)r[o++]=c()<<1|c()>>6,r[o++]=e<<2|c()>>5,r[o++]=e<<3|c()>>4,r[o++]=e<<4|c()>>3,r[o++]=e<<5|c()>>2,r[o++]=e<<6|c()>>1,r[o++]=e<<7|c();return r}}}
1
+ {class EncodeOutput{constructor(t){this.uint8Array=t}toString(){return(new TextDecoder).decode(this.uint8Array)}toJSTemplateLiterals(){return`\`${this.toString().replace(/[\r\\`]|\${|<\/script/g,t=>"\r"==t?"\\r":"<\/script"==t?"<\\/script":"\\"+t)}\``}}self.base128={EncodeOutput,encode(t){"string"==typeof t&&(t=(new TextEncoder).encode(t));for(var e=t.length,r=new Uint8Array(Math.ceil(e/7*8)),n=0,o=0;n<e;)r[o++]=t[n]>>1,r[o++]=t[n++]<<6&127|t[n]>>2,r[o++]=t[n++]<<5&127|t[n]>>3,r[o++]=t[n++]<<4&127|t[n]>>4,r[o++]=t[n++]<<3&127|t[n]>>5,r[o++]=t[n++]<<2&127|t[n]>>6,r[o++]=t[n++]<<1&127|t[n]>>7,r[o++]=127&t[n++];return new EncodeOutput(r)},decode(t){for(var e,r=t.length,n=new Uint8Array(Math.floor(r/8*7)),o=0,c=0,i=()=>(e=t.charCodeAt(o++))>127?e=0:e;o<r;)n[c++]=i()<<1|i()>>6,n[c++]=e<<2|i()>>5,n[c++]=e<<3|i()>>4,n[c++]=e<<4|i()>>3,n[c++]=e<<5|i()>>2,n[c++]=e<<6|i()>>1,n[c++]=e<<7|i();return n}}}
package/dist/index.js CHANGED
@@ -16,8 +16,8 @@ export class EncodeOutput {
16
16
  export function encode(input) {
17
17
  if (typeof input == 'string')
18
18
  input = new TextEncoder().encode(input);
19
- var out = new Uint8Array(Math.ceil(input.length / 7 * 8)), ii = 0, oi = 0;
20
- while (ii < input.length) {
19
+ var il = input.length, out = new Uint8Array(Math.ceil(il / 7 * 8)), ii = 0, oi = 0;
20
+ while (ii < il) {
21
21
  // 0 1 2 3 4 5 6 7
22
22
  // in 00000000 11111111 22222222 33333333 44444444 55555555 66666666
23
23
  // out _0000000 _0111111 _1122222 _2223333 _3333444 _4444455 _5555556 _6666666
@@ -33,10 +33,10 @@ export function encode(input) {
33
33
  return new EncodeOutput(out);
34
34
  }
35
35
  export function decode(input) {
36
- var out = new Uint8Array(Math.floor(input.length / 8 * 7)), ii = 0, oi = 0, cache, next = () => ((cache = input.charCodeAt(ii++)) >> 7
36
+ var il = input.length, out = new Uint8Array(Math.floor(il / 8 * 7)), ii = 0, oi = 0, cache, next = () => ((cache = input.charCodeAt(ii++)) > 127
37
37
  ? cache = 0 // In HTML, 0 is likely to be converted to 65533 (�)
38
38
  : cache);
39
- while (ii < input.length) {
39
+ while (ii < il) {
40
40
  // 0 1 2 3 4 5 6 7
41
41
  // in _0000000 _1111111 _2222222 _3333333 _4444444 _5555555 _6666666 _7777777
42
42
  // out 00000001 11111122 22222333 33334444 44455555 55666666 67777777
package/package.json CHANGED
@@ -1,32 +1,35 @@
1
- {
2
- "name": "base128-ascii",
3
- "version": "2.1.8",
4
- "main": "dist/index.js",
5
- "browser": "dist/browser.min.js",
6
- "type": "module",
7
- "scripts": {
8
- "build": "rimraf dist && tsc && node scripts/build-for-browser.js",
9
- "test": "npm run build && node scripts/test.js",
10
- "prepublishOnly": "npm run test"
11
- },
12
- "repository": {
13
- "type": "git",
14
- "url": "git+https://github.com/bddjr/base128.git"
15
- },
16
- "author": "bddjr",
17
- "license": "Unlicense",
18
- "files": [
19
- "dist"
20
- ],
21
- "keywords": [
22
- "base128",
23
- "base64",
24
- "ascii"
25
- ],
26
- "devDependencies": {
27
- "@types/node": "^24.0.3",
28
- "rimraf": "^6.0.1",
29
- "terser": "^5.44.1",
30
- "typescript": "^5.7.3"
31
- }
32
- }
1
+ {
2
+ "name": "base128-ascii",
3
+ "version": "2.1.9",
4
+ "main": "dist/index.js",
5
+ "browser": "dist/browser.min.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "build": "rimraf dist && tsc && node scripts/build-for-browser.js && es-check es2015 --module dist/**/*.js --not dist/browser.* && es-check es2015 dist/browser.*",
9
+ "test": "npm run build && node scripts/test.js",
10
+ "prepublishOnly": "npm run test"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/bddjr/base128.git"
15
+ },
16
+ "author": "bddjr",
17
+ "license": "Unlicense",
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "keywords": [
22
+ "base128",
23
+ "base64",
24
+ "ascii",
25
+ "encode",
26
+ "decode"
27
+ ],
28
+ "devDependencies": {
29
+ "@types/node": "^24.0.3",
30
+ "es-check": "^9.5.3",
31
+ "rimraf": "^6.0.1",
32
+ "terser": "^5.44.1",
33
+ "typescript": "^5.7.3"
34
+ }
35
+ }