base128-ascii 5.0.0 → 5.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.
Files changed (3) hide show
  1. package/main.d.ts +8 -3
  2. package/main.js +27 -26
  3. package/package.json +4 -3
package/main.d.ts CHANGED
@@ -1,6 +1,4 @@
1
- export declare class EncodeResult {
2
- constructor(bytes: Uint8Array<ArrayBuffer>);
3
-
1
+ interface EncodeResultPrototype {
4
2
  /**
5
3
  * Returns a base128 string.
6
4
  */
@@ -10,10 +8,17 @@ export declare class EncodeResult {
10
8
  * Returns a base128 [Template literals](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Template_literals).
11
9
  */
12
10
  toJSTemplateLiterals(): string;
11
+ }
13
12
 
13
+ interface EncodeResult extends EncodeResultPrototype {
14
14
  bytes: Uint8Array<ArrayBuffer>;
15
15
  }
16
16
 
17
+ export declare var EncodeResult: {
18
+ new(bytes: Uint8Array<ArrayBuffer>): EncodeResult;
19
+ prototype: EncodeResultPrototype;
20
+ };
21
+
17
22
  export declare function encode(input: Uint8Array): EncodeResult;
18
23
 
19
24
  export declare function decode(input: string): Uint8Array<ArrayBuffer>;
package/main.js CHANGED
@@ -1,5 +1,3 @@
1
- //@ts-nocheck
2
-
3
1
  export class EncodeResult {
4
2
  /**
5
3
  * @param {Uint8Array<ArrayBuffer>} bytes
@@ -7,16 +5,13 @@ export class EncodeResult {
7
5
  constructor(bytes) {
8
6
  this.bytes = bytes
9
7
  }
10
- toString() {
11
- return new TextDecoder().decode(this.bytes)
12
- }
13
8
  toJSTemplateLiterals() {
14
9
  return `\`${this.toString().replace(
15
10
  /[\r\\`]|\$\{|<\/script/g,
16
11
  (match) => (
17
- match == '\r'
12
+ match === '\r'
18
13
  ? '\\r'
19
- : match == '</script'
14
+ : match === '</script'
20
15
  ? '<\\/script'
21
16
  : '\\' + match
22
17
  )
@@ -24,6 +19,17 @@ export class EncodeResult {
24
19
  }
25
20
  }
26
21
 
22
+ if (typeof Buffer == 'function' && Buffer.prototype && typeof Buffer.prototype.latin1Slice == 'function') {
23
+ EncodeResult.prototype.toString = function () {
24
+ return Buffer.prototype.latin1Slice.call(this.bytes)
25
+ }
26
+ } else {
27
+ let _textDecoder
28
+ EncodeResult.prototype.toString = function () {
29
+ return (_textDecoder || (_textDecoder = new TextDecoder)).decode(this.bytes)
30
+ }
31
+ }
32
+
27
33
  /**
28
34
  * @param {Uint8Array} input
29
35
  */
@@ -37,14 +43,14 @@ export function encode(input) {
37
43
  // in 00000000 11111111 22222222 33333333 44444444 55555555 66666666
38
44
  // out _0000000 _0111111 _1122222 _2223333 _3333444 _4444455 _5555556 _6666666
39
45
 
40
- /* 0 */ out[oi++] = input[ii] >> 1 & 127
41
- /* 1 */ out[oi++] = (input[ii++] << 6 | input[ii] >> 2) & 127
42
- /* 2 */ out[oi++] = (input[ii++] << 5 | input[ii] >> 3) & 127
43
- /* 3 */ out[oi++] = (input[ii++] << 4 | input[ii] >> 4) & 127
44
- /* 4 */ out[oi++] = (input[ii++] << 3 | input[ii] >> 5) & 127
45
- /* 5 */ out[oi++] = (input[ii++] << 2 | input[ii] >> 6) & 127
46
- /* 6 */ out[oi++] = (input[ii++] << 1 | input[ii] >> 7) & 127
47
- /* 7 */ out[oi++] = input[ii++] & 127
46
+ /* 0 */ out[oi++] = 127 & input[ii] >> 1
47
+ /* 1 */ out[oi++] = 127 & (input[ii++] << 6 | input[ii] >> 2)
48
+ /* 2 */ out[oi++] = 127 & (input[ii++] << 5 | input[ii] >> 3)
49
+ /* 3 */ out[oi++] = 127 & (input[ii++] << 4 | input[ii] >> 4)
50
+ /* 4 */ out[oi++] = 127 & (input[ii++] << 3 | input[ii] >> 5)
51
+ /* 5 */ out[oi++] = 127 & (input[ii++] << 2 | input[ii] >> 6)
52
+ /* 6 */ out[oi++] = 127 & (input[ii++] << 1 | input[ii] >> 7)
53
+ /* 7 */ out[oi++] = 127 & input[ii++]
48
54
  }
49
55
  return new EncodeResult(out)
50
56
  }
@@ -57,24 +63,19 @@ export function decode(input) {
57
63
  , out = new Uint8Array(il / 8 * 7)
58
64
  , ii = 0
59
65
  , oi = 0
66
+ , k
60
67
  , cache
61
- , next = () => (
62
- (cache = input.charCodeAt(ii++)) > 127
68
+ , next = _ =>
69
+ (cache = input.charCodeAt(ii++)) >> 7
63
70
  ? cache = 0 // In HTML, 0 is likely to be converted to 65533 (�)
64
71
  : cache
65
- )
66
72
  while (ii < il) {
67
73
  // 0 1 2 3 4 5 6 7
68
74
  // in _0000000 _1111111 _2222222 _3333333 _4444444 _5555555 _6666666 _7777777
69
75
  // out 00000001 11111122 22222333 33334444 44455555 55666666 67777777
70
-
71
- /* 0 */ out[oi++] = next() << 1 | next() >> 6
72
- /* 1 */ out[oi++] = cache << 2 | next() >> 5
73
- /* 2 */ out[oi++] = cache << 3 | next() >> 4
74
- /* 3 */ out[oi++] = cache << 4 | next() >> 3
75
- /* 4 */ out[oi++] = cache << 5 | next() >> 2
76
- /* 5 */ out[oi++] = cache << 6 | next() >> 1
77
- /* 6 */ out[oi++] = cache << 7 | next()
76
+ for (next(), k = 7; k;) {
77
+ out[oi++] = cache << 7 - --k | next() >> k
78
+ }
78
79
  }
79
80
  return out
80
81
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "base128-ascii",
3
- "version": "5.0.0",
3
+ "version": "5.0.1",
4
4
  "author": "bddjr",
5
5
  "license": "Unlicense",
6
6
  "type": "module",
@@ -10,7 +10,7 @@
10
10
  "main.d.ts"
11
11
  ],
12
12
  "scripts": {
13
- "test": "es-check es6 --checkFeatures --module main.js && node scripts/test.mjs",
13
+ "test": "es-check es6 --checkFeatures --module main.js && tsc && node scripts/test.mjs",
14
14
  "prepublishOnly": "node --run test"
15
15
  },
16
16
  "repository": {
@@ -28,6 +28,7 @@
28
28
  "btoa"
29
29
  ],
30
30
  "devDependencies": {
31
- "es-check": "^9.5.3"
31
+ "es-check": "^9.5.3",
32
+ "typescript": "^6.0.3"
32
33
  }
33
34
  }