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/utils.js CHANGED
@@ -1,37 +1,52 @@
1
- 'use strict'
1
+ import {Buffer} from 'node:buffer';
2
2
 
3
- function mapObj (ar, f) {
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
- last[nv[0]] = nv[1]
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
- function hexlify (buf) {
17
- const nibbles = buf.BYTES_PER_ELEMENT * 2
18
- const pad = '0'.repeat(nibbles)
19
- return Array.prototype.slice.call(buf)
20
- .map(i => (pad + i.toString(16)).slice(-nibbles))
21
- .join('')
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
- function unhexlify (str, typ = Uint8ClampedArray) {
25
- str = str.replace(/^0x/i, '')
26
- const nibbles = typ.BYTES_PER_ELEMENT * 2
27
- return typ.from({ length: str.length / nibbles }, i => {
28
- const off = i * nibbles
29
- return parseInt(str.slice(off, off + nibbles), 16)
30
- })
31
- }
32
-
33
- module.exports = {
34
- mapObj,
35
- hexlify,
36
- unhexlify
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
  }
package/bin/unexpectedly DELETED
@@ -1,5 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- const suite = require('../index')
4
-
5
- suite(...process.argv.slice(2)).catch(console.error)