base128-ascii 5.0.2 → 5.0.4

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 (2) hide show
  1. package/main.js +31 -15
  2. package/package.json +3 -1
package/main.js CHANGED
@@ -1,3 +1,31 @@
1
+ let _bytesToStr = (
2
+ (
3
+ typeof Buffer == 'function' &&
4
+ Buffer.prototype &&
5
+ typeof Buffer.prototype.latin1Slice == 'function'
6
+ )
7
+ ? typeof Deno == 'undefined'
8
+ ? (bytes) => Buffer.prototype.latin1Slice.call(bytes)
9
+ : (bytes) => {
10
+ // https://github.com/bddjr/base128/pull/5
11
+ try {
12
+ Buffer.prototype.latin1Slice.call(0)
13
+ } catch (e) {
14
+ // Deno >= 2.8.2
15
+ // Uncaught TypeError: expected ArrayBufferView
16
+ return (_bytesToStr = (bytes) => Buffer.prototype.latin1Slice.call(bytes))(bytes)
17
+ }
18
+ // Deno < 2.8.2
19
+ const td = new TextDecoder
20
+ return (_bytesToStr = (bytes) => td.decode(bytes))(bytes)
21
+ }
22
+ : (bytes) => {
23
+ // TextDecoder keeps the default UTF-8, which is already the fastest.
24
+ const td = new TextDecoder
25
+ return (_bytesToStr = (bytes) => td.decode(bytes))(bytes)
26
+ }
27
+ )
28
+
1
29
  export class EncodeResult {
2
30
  /**
3
31
  * @param {Uint8Array<ArrayBuffer>} bytes
@@ -5,6 +33,9 @@ export class EncodeResult {
5
33
  constructor(bytes) {
6
34
  this.bytes = bytes
7
35
  }
36
+ toString() {
37
+ return _bytesToStr(this.bytes)
38
+ }
8
39
  toJSTemplateLiterals() {
9
40
  return `\`${this.toString().replace(
10
41
  /[\r\\`]|\$\{|<\/script/g,
@@ -19,21 +50,6 @@ export class EncodeResult {
19
50
  }
20
51
  }
21
52
 
22
- if (
23
- typeof Buffer == 'function' &&
24
- Buffer.prototype &&
25
- typeof Buffer.prototype.latin1Slice == 'function'
26
- ) {
27
- EncodeResult.prototype.toString = function () {
28
- return Buffer.prototype.latin1Slice.call(this.bytes)
29
- }
30
- } else {
31
- let _textDecoder
32
- EncodeResult.prototype.toString = function () {
33
- return (_textDecoder || (_textDecoder = new TextDecoder)).decode(this.bytes)
34
- }
35
- }
36
-
37
53
  /**
38
54
  * @param {Uint8Array} input
39
55
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "base128-ascii",
3
- "version": "5.0.2",
3
+ "version": "5.0.4",
4
4
  "author": "bddjr",
5
5
  "license": "Unlicense",
6
6
  "type": "module",
@@ -11,6 +11,8 @@
11
11
  ],
12
12
  "scripts": {
13
13
  "test": "es-check es6 --checkFeatures --module main.js && tsc && node scripts/test.mjs",
14
+ "test:deno": "deno run --allow-read=. --allow-write=. scripts/test.mjs",
15
+ "test:bun": "bun scripts/test.mjs",
14
16
  "prepublishOnly": "node --run test"
15
17
  },
16
18
  "repository": {