@thi.ng/hex 2.3.22 → 2.3.24

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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-12-03T12:13:31Z
3
+ - **Last updated**: 2023-12-11T10:07:09Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
package/README.md CHANGED
@@ -53,7 +53,7 @@ For Node.js REPL:
53
53
  const hex = await import("@thi.ng/hex");
54
54
  ```
55
55
 
56
- Package sizes (brotli'd, pre-treeshake): ESM: 578 bytes
56
+ Package sizes (brotli'd, pre-treeshake): ESM: 631 bytes
57
57
 
58
58
  ## Dependencies
59
59
 
package/index.js CHANGED
@@ -1,191 +1,76 @@
1
- const P32 = 0x100000000;
2
- /**
3
- * Hex digits
4
- */
5
- export const HEX = "0123456789abcdef";
6
- /**
7
- * Returns 4bit uint as hex string
8
- *
9
- * @param x -
10
- */
11
- export const U4 = (x) => HEX[x & 0xf];
12
- /**
13
- * Returns 8bit uint as hex string
14
- *
15
- * @param x -
16
- */
17
- export const U8 = (x) => HEX[(x >>> 4) & 0xf] + HEX[x & 0xf];
18
- /**
19
- * Returns hex string of 8bit uint, read from given byte array at index `i`.
20
- *
21
- * @param x -
22
- * @param i -
23
- */
24
- export const U8A = (x, i) => U8(x[i]);
25
- /**
26
- * Returns 16bit uint as hex string
27
- *
28
- * @param x -
29
- */
30
- export const U16 = (x) => U8(x >>> 8) + U8(x & 0xff);
31
- /**
32
- * Returns hex string of 16bit uint, read in big-endian order from given byte
33
- * array at index `i`.
34
- *
35
- * @param x -
36
- * @param i -
37
- */
38
- export const U16BE = (x, i) => U8(x[i]) + U8(x[i + 1]);
39
- /**
40
- * Returns hex string of 16bit uint, read in litte-endian order from given byte
41
- * array at index `i`.
42
- *
43
- * @param x -
44
- * @param i -
45
- */
46
- export const U16LE = (x, i) => U8(x[i + 1]) + U8(x[i]);
47
- /**
48
- * Returns 24bit uint as hex string
49
- *
50
- * @param x -
51
- */
52
- export const U24 = (x) => U8(x >>> 16) + U16(x);
53
- /**
54
- * Returns hex string of 24bit uint, read in big-endian order from given byte
55
- * array at index `i`.
56
- *
57
- * @param x -
58
- * @param i -
59
- */
60
- export const U24BE = (x, i) => U8(x[i]) + U16BE(x, i + 1);
61
- /**
62
- * Returns hex string of 24bit uint, read in litte-endian order from given byte
63
- * array at index `i`.
64
- *
65
- * @param x -
66
- * @param i -
67
- */
68
- export const U24LE = (x, i) => U8(x[i + 2]) + U16LE(x, i);
69
- /**
70
- * Returns 32bit uint as hex string
71
- *
72
- * @param x -
73
- */
74
- export const U32 = (x) => U16(x >>> 16) + U16(x);
75
- /**
76
- * Returns hex string of 32bit uint, read in big-endian order from given byte
77
- * array at index `i`.
78
- *
79
- * @param x -
80
- * @param i -
81
- */
82
- export const U32BE = (x, i) => U16BE(x, i) + U16BE(x, i + 2);
83
- /**
84
- * Returns hex string of 32bit uint, read in litte-endian order from given byte
85
- * array at index `i`.
86
- *
87
- * @param x -
88
- * @param i -
89
- */
90
- export const U32LE = (x, i) => U16LE(x, i + 2) + U16LE(x, i);
91
- /**
92
- * Returns 48bit uint as hex string
93
- *
94
- * @param x -
95
- */
96
- export const U48 = (x) => U48HL(x / P32, x % P32);
97
- /**
98
- * Similar to {@link U48}, but takes the 64bit arg as 2x 32bit values.
99
- *
100
- * @param hi -
101
- * @param lo -
102
- */
103
- export const U48HL = (hi, lo) => U16(hi) + U32(lo);
104
- /**
105
- * Returns hex string of 48bit uint, read in big-endian order from given byte
106
- * array at index `i`.
107
- *
108
- * @param x -
109
- * @param i -
110
- */
111
- export const U48BE = (x, i) => U16BE(x, i) + U32BE(x, i + 2);
112
- /**
113
- * Returns hex string of 48bit uint, read in litte-endian order from given byte
114
- * array at index `i`.
115
- *
116
- * @param x -
117
- * @param i -
118
- */
119
- export const U48LE = (x, i) => U16LE(x, i + 4) + U32LE(x, i);
120
- /**
121
- * Returns 64bit uint as hex string.
122
- *
123
- * @remarks
124
- * Note: JS numbers are only integer precise up to `2**53 - 1`. Use
125
- * {@link U64BE} or {@link U64LE} for byte array based values (full 64bit range
126
- * supported). Alternatively, use `BigInt(x).toString(16)`.
127
- *
128
- * Also see {@link U64BIG} for `bigint` version.
129
- *
130
- * @param x -
131
- */
132
- export const U64 = (x) => U64HL(x / P32, x % P32);
133
- /**
134
- * Returns 64bit bigint as hex string.
135
- *
136
- * @param x
137
- */
138
- export const U64BIG = (x) => U64HL(Number(x >> BigInt(32)), Number(x & BigInt(P32 - 1)));
139
- /**
140
- * Similar to {@link U64}, but takes the 64bit arg as 2x 32bit values.
141
- *
142
- * @param hi -
143
- * @param lo -
144
- */
145
- export const U64HL = (hi, lo) => U32(hi) + U32(lo);
146
- /**
147
- * Returns hex string of 64bit uint, read in big-endian order from given byte
148
- * array at index `i`.
149
- *
150
- * @param x -
151
- * @param i -
152
- */
153
- export const U64BE = (x, i) => U32BE(x, i) + U32BE(x, i + 4);
154
- /**
155
- * Returns hex string of 64bit uint, read in litte-endian order from given byte
156
- * array at index `i`.
157
- *
158
- * @param x -
159
- * @param i -
160
- */
161
- export const U64LE = (x, i) => U32LE(x, i + 4) + U32LE(x, i);
162
- /**
163
- * Returns UUID formatted string of given byte array from optional start index
164
- * `i` (default: 0). Array must have min. length 16 (starting from `i`).
165
- *
166
- * @param id -
167
- * @param i -
168
- */
169
- export const uuid = (id, i = 0) =>
170
- // prettier-ignore
171
- `${U32BE(id, i)}-${U16BE(id, i + 4)}-${U16BE(id, i + 6)}-${U16BE(id, i + 8)}-${U48BE(id, i + 10)}`;
172
- export const hexdump = (bytes, addr, len, width, ascii) => hexdumpLines(bytes, addr, len, width, ascii).join("\n");
173
- export const hexdumpLines = (bytes, addr, len, width = 16, ascii = true) => {
174
- len = Math.min(len, bytes.length - addr);
175
- let res = [];
176
- while (len > 0) {
177
- const row = [...bytes.subarray(addr, addr + Math.min(len, width))];
178
- const pad = len < width && ascii ? new Array(width - len).fill(" ") : [];
179
- res.push([
180
- U32(addr),
181
- ...row.map(U8),
182
- ...pad,
183
- row
184
- .map((x) => x >= 0x20 && x < 0x80 ? String.fromCharCode(x) : ".")
185
- .join(""),
186
- ].join(" "));
187
- addr += width;
188
- len -= width;
189
- }
190
- return res;
1
+ const P32 = 4294967296;
2
+ const HEX = "0123456789abcdef";
3
+ const U4 = (x) => HEX[x & 15];
4
+ const U8 = (x) => HEX[x >>> 4 & 15] + HEX[x & 15];
5
+ const U8A = (x, i) => U8(x[i]);
6
+ const U16 = (x) => U8(x >>> 8) + U8(x & 255);
7
+ const U16BE = (x, i) => U8(x[i]) + U8(x[i + 1]);
8
+ const U16LE = (x, i) => U8(x[i + 1]) + U8(x[i]);
9
+ const U24 = (x) => U8(x >>> 16) + U16(x);
10
+ const U24BE = (x, i) => U8(x[i]) + U16BE(x, i + 1);
11
+ const U24LE = (x, i) => U8(x[i + 2]) + U16LE(x, i);
12
+ const U32 = (x) => U16(x >>> 16) + U16(x);
13
+ const U32BE = (x, i) => U16BE(x, i) + U16BE(x, i + 2);
14
+ const U32LE = (x, i) => U16LE(x, i + 2) + U16LE(x, i);
15
+ const U48 = (x) => U48HL(x / P32, x % P32);
16
+ const U48HL = (hi, lo) => U16(hi) + U32(lo);
17
+ const U48BE = (x, i) => U16BE(x, i) + U32BE(x, i + 2);
18
+ const U48LE = (x, i) => U16LE(x, i + 4) + U32LE(x, i);
19
+ const U64 = (x) => U64HL(x / P32, x % P32);
20
+ const U64BIG = (x) => U64HL(Number(x >> BigInt(32)), Number(x & BigInt(P32 - 1)));
21
+ const U64HL = (hi, lo) => U32(hi) + U32(lo);
22
+ const U64BE = (x, i) => U32BE(x, i) + U32BE(x, i + 4);
23
+ const U64LE = (x, i) => U32LE(x, i + 4) + U32LE(x, i);
24
+ const uuid = (id, i = 0) => (
25
+ // prettier-ignore
26
+ `${U32BE(id, i)}-${U16BE(id, i + 4)}-${U16BE(id, i + 6)}-${U16BE(id, i + 8)}-${U48BE(id, i + 10)}`
27
+ );
28
+ const hexdump = (bytes, addr, len, width, ascii) => hexdumpLines(bytes, addr, len, width, ascii).join("\n");
29
+ const hexdumpLines = (bytes, addr, len, width = 16, ascii = true) => {
30
+ len = Math.min(len, bytes.length - addr);
31
+ let res = [];
32
+ while (len > 0) {
33
+ const row = [...bytes.subarray(addr, addr + Math.min(len, width))];
34
+ const pad = len < width && ascii ? new Array(width - len).fill(" ") : [];
35
+ res.push(
36
+ [
37
+ U32(addr),
38
+ ...row.map(U8),
39
+ ...pad,
40
+ row.map(
41
+ (x) => x >= 32 && x < 128 ? String.fromCharCode(x) : "."
42
+ ).join("")
43
+ ].join(" ")
44
+ );
45
+ addr += width;
46
+ len -= width;
47
+ }
48
+ return res;
49
+ };
50
+ export {
51
+ HEX,
52
+ U16,
53
+ U16BE,
54
+ U16LE,
55
+ U24,
56
+ U24BE,
57
+ U24LE,
58
+ U32,
59
+ U32BE,
60
+ U32LE,
61
+ U4,
62
+ U48,
63
+ U48BE,
64
+ U48HL,
65
+ U48LE,
66
+ U64,
67
+ U64BE,
68
+ U64BIG,
69
+ U64HL,
70
+ U64LE,
71
+ U8,
72
+ U8A,
73
+ hexdump,
74
+ hexdumpLines,
75
+ uuid
191
76
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/hex",
3
- "version": "2.3.22",
3
+ "version": "2.3.24",
4
4
  "description": "Hex string formatters for 4/8/16/24/32/48/64bit words",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -24,7 +24,9 @@
24
24
  "author": "Karsten Schmidt (https://thi.ng)",
25
25
  "license": "Apache-2.0",
26
26
  "scripts": {
27
- "build": "yarn clean && tsc --declaration",
27
+ "build": "yarn build:esbuild && yarn build:decl",
28
+ "build:decl": "tsc --declaration --emitDeclarationOnly",
29
+ "build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
28
30
  "clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
29
31
  "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
30
32
  "doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
@@ -34,7 +36,7 @@
34
36
  },
35
37
  "devDependencies": {
36
38
  "@microsoft/api-extractor": "^7.38.3",
37
- "@thi.ng/testament": "^0.4.3",
39
+ "esbuild": "^0.19.8",
38
40
  "rimraf": "^5.0.5",
39
41
  "tools": "^0.0.1",
40
42
  "typedoc": "^0.25.4",
@@ -68,5 +70,5 @@
68
70
  ],
69
71
  "year": 2020
70
72
  },
71
- "gitHead": "04d1de79f256d7a53c6b5fd157b37f49bc88e11d\n"
73
+ "gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
72
74
  }