unexpectedly 0.0.7 → 1.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.
- package/README.md +57 -20
- package/bin/unexpectedly.js +32 -0
- package/index.js +108 -76
- package/modtype.js +60 -0
- package/package.json +29 -21
- package/runner.js +129 -58
- package/testFile.peg.js +429 -491
- package/utils.js +41 -26
- package/bin/unexpectedly +0 -5
package/utils.js
CHANGED
|
@@ -1,37 +1,52 @@
|
|
|
1
|
-
|
|
1
|
+
import {Buffer} from 'node:buffer';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Convert an array to an object, using a map function. If the function
|
|
5
|
+
* returns an array, it is [key, value]. Otherwise, the value returned is
|
|
6
|
+
* assigned to the original key.
|
|
7
|
+
*
|
|
8
|
+
* @param {any[]} ar Input array.
|
|
9
|
+
* @param {(key) => [key: string,value: any]|any} f Map function.
|
|
10
|
+
* @returns {object} Created object.
|
|
11
|
+
*/
|
|
12
|
+
export function mapObj(ar, f) {
|
|
4
13
|
return ar.reduce((last, v) => {
|
|
5
|
-
const nv = f(v)
|
|
14
|
+
const nv = f(v);
|
|
6
15
|
if (Array.isArray(nv)) {
|
|
7
|
-
|
|
16
|
+
// eslint-disable-next-line prefer-destructuring
|
|
17
|
+
last[nv[0]] = nv[1];
|
|
8
18
|
} else {
|
|
9
|
-
last[v] = nv
|
|
19
|
+
last[v] = nv;
|
|
10
20
|
}
|
|
11
|
-
return last
|
|
12
|
-
}, {})
|
|
21
|
+
return last;
|
|
22
|
+
}, {});
|
|
13
23
|
}
|
|
14
24
|
|
|
15
25
|
// TODO: deal with endian-ness, using DataView
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Convert a TypedArray to a hex string.
|
|
28
|
+
*
|
|
29
|
+
* @param {TypedArray} buf The array to convert.
|
|
30
|
+
* @returns {string} Hex.
|
|
31
|
+
*/
|
|
32
|
+
export function hexlify(buf) {
|
|
33
|
+
const b = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
34
|
+
return `${b.toString('hex')}`;
|
|
22
35
|
}
|
|
23
36
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Convert a hex string to a TypedArray.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} hex Hex string, may be prefixed with `0x`.
|
|
41
|
+
* @param {Function} [Typ=Uint8ClampedArray] Constructor for TypedArray type
|
|
42
|
+
* you want created.
|
|
43
|
+
* @returns {TypedArray} Constructed arrray.
|
|
44
|
+
*/
|
|
45
|
+
export function unhexlify(hex, Typ = Uint8ClampedArray) {
|
|
46
|
+
const buf = Buffer.from(hex.replace(/^0x/, ''), 'hex');
|
|
47
|
+
return new Typ(
|
|
48
|
+
buf.buffer,
|
|
49
|
+
buf.byteOffset,
|
|
50
|
+
buf.byteLength / Typ.BYTES_PER_ELEMENT
|
|
51
|
+
);
|
|
37
52
|
}
|