base128-ascii 4.0.1 → 5.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.
- package/main.d.ts +11 -8
- package/main.js +30 -57
- package/package.json +4 -3
package/main.d.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
constructor(bytes: Uint8Array);
|
|
3
|
-
|
|
1
|
+
interface EncodeResultPrototype {
|
|
4
2
|
/**
|
|
5
3
|
* Returns a base128 string.
|
|
6
4
|
*/
|
|
@@ -10,15 +8,20 @@ export declare class EncodeResult {
|
|
|
10
8
|
* Returns a base128 [Template literals](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Template_literals).
|
|
11
9
|
*/
|
|
12
10
|
toJSTemplateLiterals(): string;
|
|
11
|
+
}
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
readonly bytes: Uint8Array;
|
|
13
|
+
interface EncodeResult extends EncodeResultPrototype {
|
|
14
|
+
bytes: Uint8Array<ArrayBuffer>;
|
|
17
15
|
}
|
|
18
16
|
|
|
19
|
-
export declare
|
|
17
|
+
export declare var EncodeResult: {
|
|
18
|
+
new(bytes: Uint8Array<ArrayBuffer>): EncodeResult;
|
|
19
|
+
prototype: EncodeResultPrototype;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export declare function encode(input: Uint8Array): EncodeResult;
|
|
20
23
|
|
|
21
|
-
export declare function decode(input: string): Uint8Array
|
|
24
|
+
export declare function decode(input: string): Uint8Array<ArrayBuffer>;
|
|
22
25
|
|
|
23
26
|
declare const base128: {
|
|
24
27
|
EncodeResult: typeof EncodeResult,
|
package/main.js
CHANGED
|
@@ -1,60 +1,41 @@
|
|
|
1
|
-
//@ts-nocheck
|
|
2
|
-
|
|
3
1
|
export class EncodeResult {
|
|
4
2
|
/**
|
|
5
|
-
* @param {Uint8Array} bytes
|
|
3
|
+
* @param {Uint8Array<ArrayBuffer>} bytes
|
|
6
4
|
*/
|
|
7
5
|
constructor(bytes) {
|
|
8
|
-
|
|
9
|
-
throw TypeError(`EncodeResult: Must input Uint8Array`)
|
|
10
|
-
}
|
|
11
|
-
Object.defineProperty(this, "bytes", { value: bytes, enumerable: true })
|
|
12
|
-
}
|
|
13
|
-
toString() {
|
|
14
|
-
return new TextDecoder().decode(this.bytes)
|
|
6
|
+
this.bytes = bytes
|
|
15
7
|
}
|
|
16
8
|
toJSTemplateLiterals() {
|
|
17
9
|
return `\`${this.toString().replace(
|
|
18
10
|
/[\r\\`]|\$\{|<\/script/g,
|
|
19
11
|
(match) => (
|
|
20
|
-
match
|
|
12
|
+
match === '\r'
|
|
21
13
|
? '\\r'
|
|
22
|
-
: match
|
|
14
|
+
: match === '</script'
|
|
23
15
|
? '<\\/script'
|
|
24
16
|
: '\\' + match
|
|
25
17
|
)
|
|
26
18
|
)}\``
|
|
27
19
|
}
|
|
28
|
-
|
|
29
|
-
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (typeof Buffer == 'function' && Buffer.prototype && typeof Buffer.prototype.latin1Slice == 'function') {
|
|
23
|
+
EncodeResult.prototype.toString = function () {
|
|
24
|
+
return Buffer.prototype.latin1Slice.call(this.bytes)
|
|
25
|
+
}
|
|
26
|
+
} else {
|
|
27
|
+
let _textDecoder
|
|
28
|
+
EncodeResult.prototype.toString = function () {
|
|
29
|
+
return (_textDecoder || (_textDecoder = new TextDecoder)).decode(this.bytes)
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
|
-
* @param {Uint8Array
|
|
34
|
+
* @param {Uint8Array} input
|
|
35
35
|
*/
|
|
36
36
|
export function encode(input) {
|
|
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
|
|
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>
|
|
53
37
|
var il = input.length
|
|
54
|
-
|
|
55
|
-
throw TypeError(`encode: typeof input.length must be number`)
|
|
56
|
-
}
|
|
57
|
-
var out = new Uint8Array(Math.ceil(il / 7 * 8))
|
|
38
|
+
, out = new Uint8Array(Math.ceil(il / 7 * 8))
|
|
58
39
|
, ii = 0
|
|
59
40
|
, oi = 0
|
|
60
41
|
while (ii < il) {
|
|
@@ -62,14 +43,14 @@ export function encode(input) {
|
|
|
62
43
|
// in 00000000 11111111 22222222 33333333 44444444 55555555 66666666
|
|
63
44
|
// out _0000000 _0111111 _1122222 _2223333 _3333444 _4444455 _5555556 _6666666
|
|
64
45
|
|
|
65
|
-
/* 0 */ out[oi++] = input[ii] >> 1
|
|
66
|
-
/* 1 */ out[oi++] = (input[ii++] << 6 | input[ii] >> 2)
|
|
67
|
-
/* 2 */ out[oi++] = (input[ii++] << 5 | input[ii] >> 3)
|
|
68
|
-
/* 3 */ out[oi++] = (input[ii++] << 4 | input[ii] >> 4)
|
|
69
|
-
/* 4 */ out[oi++] = (input[ii++] << 3 | input[ii] >> 5)
|
|
70
|
-
/* 5 */ out[oi++] = (input[ii++] << 2 | input[ii] >> 6)
|
|
71
|
-
/* 6 */ out[oi++] = (input[ii++] << 1 | input[ii] >> 7)
|
|
72
|
-
/* 7 */ out[oi++] = input[ii++]
|
|
46
|
+
/* 0 */ out[oi++] = 127 & input[ii] >> 1
|
|
47
|
+
/* 1 */ out[oi++] = 127 & (input[ii++] << 6 | input[ii] >> 2)
|
|
48
|
+
/* 2 */ out[oi++] = 127 & (input[ii++] << 5 | input[ii] >> 3)
|
|
49
|
+
/* 3 */ out[oi++] = 127 & (input[ii++] << 4 | input[ii] >> 4)
|
|
50
|
+
/* 4 */ out[oi++] = 127 & (input[ii++] << 3 | input[ii] >> 5)
|
|
51
|
+
/* 5 */ out[oi++] = 127 & (input[ii++] << 2 | input[ii] >> 6)
|
|
52
|
+
/* 6 */ out[oi++] = 127 & (input[ii++] << 1 | input[ii] >> 7)
|
|
53
|
+
/* 7 */ out[oi++] = 127 & input[ii++]
|
|
73
54
|
}
|
|
74
55
|
return new EncodeResult(out)
|
|
75
56
|
}
|
|
@@ -78,31 +59,23 @@ export function encode(input) {
|
|
|
78
59
|
* @param {string} input
|
|
79
60
|
*/
|
|
80
61
|
export function decode(input) {
|
|
81
|
-
if (typeof input != 'string') {
|
|
82
|
-
throw TypeError(`decode: Must input string`)
|
|
83
|
-
}
|
|
84
62
|
var il = input.length
|
|
85
63
|
, out = new Uint8Array(il / 8 * 7)
|
|
86
64
|
, ii = 0
|
|
87
65
|
, oi = 0
|
|
66
|
+
, k
|
|
88
67
|
, cache
|
|
89
|
-
, next =
|
|
90
|
-
(cache = input.charCodeAt(ii++))
|
|
68
|
+
, next = _ =>
|
|
69
|
+
(cache = input.charCodeAt(ii++)) >> 7
|
|
91
70
|
? cache = 0 // In HTML, 0 is likely to be converted to 65533 (�)
|
|
92
71
|
: cache
|
|
93
|
-
)
|
|
94
72
|
while (ii < il) {
|
|
95
73
|
// 0 1 2 3 4 5 6 7
|
|
96
74
|
// in _0000000 _1111111 _2222222 _3333333 _4444444 _5555555 _6666666 _7777777
|
|
97
75
|
// out 00000001 11111122 22222333 33334444 44455555 55666666 67777777
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
/* 2 */ out[oi++] = cache << 3 | next() >> 4
|
|
102
|
-
/* 3 */ out[oi++] = cache << 4 | next() >> 3
|
|
103
|
-
/* 4 */ out[oi++] = cache << 5 | next() >> 2
|
|
104
|
-
/* 5 */ out[oi++] = cache << 6 | next() >> 1
|
|
105
|
-
/* 6 */ out[oi++] = cache << 7 | next()
|
|
76
|
+
for (next(), k = 7; k;) {
|
|
77
|
+
out[oi++] = cache << 7 - --k | next() >> k
|
|
78
|
+
}
|
|
106
79
|
}
|
|
107
80
|
return out
|
|
108
81
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "base128-ascii",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.1",
|
|
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 main.js && node scripts/test.mjs",
|
|
13
|
+
"test": "es-check es6 --checkFeatures --module main.js && tsc && node scripts/test.mjs",
|
|
14
14
|
"prepublishOnly": "node --run test"
|
|
15
15
|
},
|
|
16
16
|
"repository": {
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
"btoa"
|
|
29
29
|
],
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"es-check": "^9.5.3"
|
|
31
|
+
"es-check": "^9.5.3",
|
|
32
|
+
"typescript": "^6.0.3"
|
|
32
33
|
}
|
|
33
34
|
}
|