base128-ascii 3.1.1 → 4.0.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 (3) hide show
  1. package/main.d.ts +5 -28
  2. package/main.js +51 -29
  3. package/package.json +1 -1
package/main.d.ts CHANGED
@@ -1,10 +1,5 @@
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);
1
+ export declare class EncodeResult {
2
+ constructor(bytes: Uint8Array);
8
3
 
9
4
  /**
10
5
  * Returns a base128 string.
@@ -16,35 +11,17 @@ export declare class Base128Bytes {
16
11
  */
17
12
  toJSTemplateLiterals(): string;
18
13
 
19
- /**
20
- * The ArrayBuffer instance referenced by the array.
21
- */
22
14
  get buffer(): ArrayBufferLike;
23
15
 
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;
16
+ readonly bytes: Uint8Array;
40
17
  }
41
18
 
42
- export declare function encode(input: Uint8Array | string): Base128Bytes;
19
+ export declare function encode(input: Uint8Array | string | ArrayLike<number> | ArrayBuffer | Pick<ArrayBufferView, "buffer">): EncodeResult;
43
20
 
44
21
  export declare function decode(input: string): Uint8Array;
45
22
 
46
23
  declare const base128 = {
47
- Base128Bytes,
24
+ EncodeResult,
48
25
  encode,
49
26
  decode,
50
27
  };
package/main.js CHANGED
@@ -1,41 +1,60 @@
1
1
  //@ts-nocheck
2
2
 
3
- export const Base128Bytes = (() => {
4
- class Base128Bytes {
5
- constructor() {
6
- return Reflect.construct(Uint8Array, arguments, new.target)
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
- )}\``
3
+ export class EncodeResult {
4
+ /**
5
+ * @param {Uint8Array} bytes
6
+ */
7
+ constructor(bytes) {
8
+ if (!(bytes instanceof Uint8Array)) {
9
+ throw TypeError(`EncodeResult: Must input Uint8Array`)
22
10
  }
11
+ Object.defineProperty(this, "bytes", { value: bytes, enumerable: true })
12
+ }
13
+ toString() {
14
+ return new TextDecoder().decode(this.bytes)
23
15
  }
24
- const p = Object.getPrototypeOf(Uint8Array.prototype)
25
- for (const key of ["buffer", "byteLength", "byteOffset", "length"]) {
26
- Object.defineProperty(Base128Bytes.prototype, key, Object.getOwnPropertyDescriptor(p, key))
16
+ toJSTemplateLiterals() {
17
+ return `\`${this.toString().replace(
18
+ /[\r\\`]|\$\{|<\/script/g,
19
+ (match) => (
20
+ match == '\r'
21
+ ? '\\r'
22
+ : match == '</script'
23
+ ? '<\\/script'
24
+ : '\\' + match
25
+ )
26
+ )}\``
27
27
  }
28
- return Base128Bytes
29
- })()
28
+ get buffer() {
29
+ return this.bytes.buffer
30
+ }
31
+ }
30
32
 
31
33
  /**
32
- * @param {Uint8Array | string} input
34
+ * @param {Uint8Array | string | ArrayLike<number> | ArrayBuffer | Pick<ArrayBufferView, "buffer">} input
33
35
  */
34
36
  export function encode(input) {
35
- if (typeof input == 'string')
37
+ if (input == null) {
38
+ throw TypeError(`encode: Cannot input null or undefined`)
39
+ }
40
+ if (input instanceof Uint8Array) {
41
+ // Uint8Array | Buffer
42
+ } else if (typeof input == 'string') {
43
+ // string
36
44
  input = new TextEncoder().encode(input)
45
+ } else if (input instanceof ArrayBuffer) {
46
+ // ArrayBuffer
47
+ input = new Uint8Array(input)
48
+ } else if (input.buffer instanceof ArrayBuffer) {
49
+ // TypedArray | DataView | Pick<ArrayBufferView, "buffer">
50
+ input = new Uint8Array(input.buffer)
51
+ }
52
+ // else ArrayLike<number>
37
53
  var il = input.length
38
- , out = new Base128Bytes(Math.ceil(il / 7 * 8))
54
+ if (typeof il != 'number') {
55
+ throw TypeError(`encode: typeof input.length must be number`)
56
+ }
57
+ var out = new Uint8Array(Math.ceil(il / 7 * 8))
39
58
  , ii = 0
40
59
  , oi = 0
41
60
  while (ii < il) {
@@ -52,13 +71,16 @@ export function encode(input) {
52
71
  /* 6 */ out[oi++] = (input[ii++] << 1 | input[ii] >> 7) & 127
53
72
  /* 7 */ out[oi++] = input[ii++] & 127
54
73
  }
55
- return out
74
+ return new EncodeResult(out)
56
75
  }
57
76
 
58
77
  /**
59
78
  * @param {string} input
60
79
  */
61
80
  export function decode(input) {
81
+ if (typeof input != 'string') {
82
+ throw TypeError(`decode: Must input string`)
83
+ }
62
84
  var il = input.length
63
85
  , out = new Uint8Array(il / 8 * 7)
64
86
  , ii = 0
@@ -86,7 +108,7 @@ export function decode(input) {
86
108
  }
87
109
 
88
110
  export default {
89
- Base128Bytes,
111
+ EncodeResult,
90
112
  encode,
91
113
  decode
92
114
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "base128-ascii",
3
- "version": "3.1.1",
3
+ "version": "4.0.0",
4
4
  "author": "bddjr",
5
5
  "license": "Unlicense",
6
6
  "type": "module",