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