base128-ascii 5.0.3 → 5.0.5

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 +28 -14
  2. package/package.json +1 -1
package/main.js CHANGED
@@ -1,12 +1,25 @@
1
+ /** @type {(bytes: Uint8Array) => string} */
1
2
  let _bytesToStr = (
2
3
  (
3
4
  typeof Buffer == 'function' &&
4
5
  Buffer.prototype &&
5
- typeof Buffer.prototype.latin1Slice == 'function' &&
6
- // Exclude Deno. See https://github.com/bddjr/base128/pull/4
7
- typeof Deno == 'undefined'
6
+ typeof Buffer.prototype.latin1Slice == 'function'
8
7
  )
9
- ? (bytes) => Buffer.prototype.latin1Slice.call(bytes)
8
+ ? typeof Deno == 'undefined'
9
+ ? (bytes) => Buffer.prototype.latin1Slice.call(bytes)
10
+ : (bytes) => {
11
+ // https://github.com/bddjr/base128/pull/5
12
+ try {
13
+ Buffer.prototype.latin1Slice.call(0)
14
+ } catch (e) {
15
+ // Deno >= 2.8.2
16
+ // Uncaught TypeError: expected ArrayBufferView
17
+ return (_bytesToStr = (bytes) => Buffer.prototype.latin1Slice.call(bytes))(bytes)
18
+ }
19
+ // Deno < 2.8.2
20
+ const td = new TextDecoder
21
+ return (_bytesToStr = (bytes) => td.decode(bytes))(bytes)
22
+ }
10
23
  : (bytes) => {
11
24
  // TextDecoder keeps the default UTF-8, which is already the fastest.
12
25
  const td = new TextDecoder
@@ -14,10 +27,17 @@ let _bytesToStr = (
14
27
  }
15
28
  )
16
29
 
30
+ const _replacer = (match) => {
31
+ switch (match) {
32
+ case '\r': return '\\r';
33
+ case '\\': return '\\\\';
34
+ case '`': return '\\`';
35
+ case '${': return '\\${';
36
+ default: return '<\\/script';
37
+ }
38
+ }
39
+
17
40
  export class EncodeResult {
18
- /**
19
- * @param {Uint8Array<ArrayBuffer>} bytes
20
- */
21
41
  constructor(bytes) {
22
42
  this.bytes = bytes
23
43
  }
@@ -27,13 +47,7 @@ export class EncodeResult {
27
47
  toJSTemplateLiterals() {
28
48
  return `\`${this.toString().replace(
29
49
  /[\r\\`]|\$\{|<\/script/g,
30
- (match) => (
31
- match === '\r'
32
- ? '\\r'
33
- : match === '</script'
34
- ? '<\\/script'
35
- : '\\' + match
36
- )
50
+ _replacer
37
51
  )}\``
38
52
  }
39
53
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "base128-ascii",
3
- "version": "5.0.3",
3
+ "version": "5.0.5",
4
4
  "author": "bddjr",
5
5
  "license": "Unlicense",
6
6
  "type": "module",