base128-ascii 2.2.0 → 3.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.
Files changed (4) hide show
  1. package/README.md +4 -2
  2. package/main.d.ts +46 -8
  3. package/main.js +27 -25
  4. package/package.json +2 -2
package/README.md CHANGED
@@ -14,9 +14,11 @@ npm i base128-ascii
14
14
  import base128 from "base128-ascii"
15
15
  import fs from "fs"
16
16
 
17
- const encodedTemplate = base128.encode(fs.readFileSync("example.gz")).toJSTemplateLiterals()
17
+ const input = fs.readFileSync("example.gz")
18
18
 
19
- const decoded = base128.decode(eval(encodedTemplate))
19
+ const encodedTemplate = base128.encode(input).toJSTemplateLiterals()
20
+
21
+ const decodedBytes = base128.decode(eval(encodedTemplate))
20
22
  ```
21
23
 
22
24
  ---
package/main.d.ts CHANGED
@@ -1,14 +1,52 @@
1
- export declare class EncodeOutput {
2
- constructor(out: Uint8Array);
3
- uint8Array: Uint8Array;
1
+ export declare class Base128Bytes {
2
+ constructor();
3
+ constructor(length: number);
4
+ constructor(array: ArrayLike<number>);
5
+ constructor<TArrayBuffer extends ArrayBufferLike = ArrayBuffer>(buffer: TArrayBuffer, byteOffset?: number, length?: number);
6
+ constructor(buffer: ArrayBuffer, byteOffset?: number, length?: number);
7
+ constructor(array: ArrayLike<number> | ArrayBuffer);
8
+
9
+ /**
10
+ * Returns a base128 string.
11
+ */
4
12
  toString(): string;
13
+
14
+ /**
15
+ * Returns a base128 [Template literals](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Template_literals).
16
+ */
5
17
  toJSTemplateLiterals(): string;
18
+
19
+ /**
20
+ * The ArrayBuffer instance referenced by the array.
21
+ */
22
+ get buffer(): ArrayBufferLike;
23
+
24
+ /**
25
+ * The length in bytes of the array.
26
+ */
27
+ get byteLength(): number;
28
+
29
+ /**
30
+ * The offset in bytes of the array.
31
+ */
32
+ get byteOffset(): number;
33
+
34
+ /**
35
+ * The length of the array.
36
+ */
37
+ get length(): number;
38
+
39
+ [index: number]: number;
6
40
  }
7
- export declare function encode(input: Uint8Array | string): EncodeOutput;
41
+
42
+ export declare function encode(input: Uint8Array | string): Base128Bytes;
43
+
8
44
  export declare function decode(input: string): Uint8Array;
9
- declare const base128: {
10
- EncodeOutput: typeof EncodeOutput;
11
- encode: typeof encode;
12
- decode: typeof decode;
45
+
46
+ declare const base128 = {
47
+ Base128Bytes,
48
+ encode,
49
+ decode,
13
50
  };
51
+
14
52
  export default base128;
package/main.js CHANGED
@@ -1,28 +1,30 @@
1
1
  //@ts-nocheck
2
2
 
3
- export class EncodeOutput {
4
- /**
5
- * @param {Uint8Array} out
6
- */
7
- constructor(out) {
8
- this.uint8Array = out
3
+ export const Base128Bytes = (() => {
4
+ class Base128Bytes {
5
+ constructor() {
6
+ return Reflect.construct(Uint8Array, arguments, Base128Bytes)
7
+ }
8
+ toString() {
9
+ return new TextDecoder().decode(this)
10
+ }
11
+ toJSTemplateLiterals() {
12
+ return `\`${this.toString().replace(
13
+ /[\r\\`]|\$\{|<\/script/g,
14
+ (match) => (
15
+ match == '\r'
16
+ ? '\\r'
17
+ : match == '</script'
18
+ ? '<\\/script'
19
+ : '\\' + match
20
+ )
21
+ )}\``
22
+ }
9
23
  }
10
- toString() {
11
- return new TextDecoder().decode(this.uint8Array)
12
- }
13
- toJSTemplateLiterals() {
14
- return `\`${this.toString().replace(
15
- /[\r\\`]|\$\{|<\/script/g,
16
- (match) => (
17
- match == '\r'
18
- ? '\\r'
19
- : match == '</script'
20
- ? '<\\/script'
21
- : '\\' + match
22
- )
23
- )}\``
24
- }
25
- }
24
+ const { buffer, byteLength, byteOffset, length } = Object.getOwnPropertyDescriptors(Object.getPrototypeOf(Uint8Array.prototype))
25
+ Object.defineProperties(Base128Bytes.prototype, { buffer, byteLength, byteOffset, length })
26
+ return Base128Bytes
27
+ })()
26
28
 
27
29
  /**
28
30
  * @param {Uint8Array | string} input
@@ -31,7 +33,7 @@ export function encode(input) {
31
33
  if (typeof input == 'string')
32
34
  input = new TextEncoder().encode(input)
33
35
  var il = input.length
34
- , out = new Uint8Array(Math.ceil(il / 7 * 8))
36
+ , out = new Base128Bytes(Math.ceil(il / 7 * 8))
35
37
  , ii = 0
36
38
  , oi = 0
37
39
  while (ii < il) {
@@ -48,7 +50,7 @@ export function encode(input) {
48
50
  /* 6 */ out[oi++] = (input[ii++] << 1 | input[ii] >> 7) & 127
49
51
  /* 7 */ out[oi++] = input[ii++] & 127
50
52
  }
51
- return new EncodeOutput(out)
53
+ return out
52
54
  }
53
55
 
54
56
  /**
@@ -82,7 +84,7 @@ export function decode(input) {
82
84
  }
83
85
 
84
86
  export default {
85
- EncodeOutput,
87
+ Base128Bytes,
86
88
  encode,
87
89
  decode
88
90
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "base128-ascii",
3
- "version": "2.2.0",
3
+ "version": "3.1.0",
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 *.js && node scripts/test.mjs",
13
+ "test": "es-check es6 --checkFeatures --module main.js && node scripts/test.mjs",
14
14
  "prepublishOnly": "node --run test"
15
15
  },
16
16
  "repository": {