@thi.ng/transducers-binary 2.1.81 → 2.1.82

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-09T19:12:04Z
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
@@ -68,7 +68,7 @@ For Node.js REPL:
68
68
  const transducersBinary = await import("@thi.ng/transducers-binary");
69
69
  ```
70
70
 
71
- Package sizes (brotli'd, pre-treeshake): ESM: 2.51 KB
71
+ Package sizes (brotli'd, pre-treeshake): ESM: 2.50 KB
72
72
 
73
73
  ## Dependencies
74
74
 
package/api.js CHANGED
@@ -1 +0,0 @@
1
- export {};
package/base64.js CHANGED
@@ -3,84 +3,101 @@ import { iterator, iterator1, __iter } from "@thi.ng/transducers/iterator";
3
3
  import { isReduced, reduced } from "@thi.ng/transducers/reduced";
4
4
  const B64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
5
5
  const B64_SAFE = B64_CHARS.substring(0, 62) + "-_";
6
- export function base64Decode(src) {
7
- return src
8
- ? new Uint8Array([...iterator1(base64Decode(), src)])
9
- : (rfn) => {
10
- const r = rfn[2];
11
- let bc = 0, bs = 0;
12
- return compR(rfn, (acc, x) => {
13
- switch (x) {
14
- case "-":
15
- x = "+";
16
- break;
17
- case "_":
18
- x = "/";
19
- break;
20
- case "=":
21
- return reduced(acc);
22
- default:
23
- }
24
- const y = B64_CHARS.indexOf(x);
25
- bs = bc & 3 ? (bs << 6) + y : y;
26
- if (bc++ & 3) {
27
- acc = r(acc, 255 & (bs >> ((-2 * bc) & 6)));
28
- }
29
- return acc;
30
- });
31
- };
6
+ function base64Decode(src) {
7
+ return src ? new Uint8Array([...iterator1(base64Decode(), src)]) : (rfn) => {
8
+ const r = rfn[2];
9
+ let bc = 0, bs = 0;
10
+ return compR(rfn, (acc, x) => {
11
+ switch (x) {
12
+ case "-":
13
+ x = "+";
14
+ break;
15
+ case "_":
16
+ x = "/";
17
+ break;
18
+ case "=":
19
+ return reduced(acc);
20
+ default:
21
+ }
22
+ const y = B64_CHARS.indexOf(x);
23
+ bs = bc & 3 ? (bs << 6) + y : y;
24
+ if (bc++ & 3) {
25
+ acc = r(acc, 255 & bs >> (-2 * bc & 6));
26
+ }
27
+ return acc;
28
+ });
29
+ };
32
30
  }
33
- export function base64Encode(...args) {
34
- const iter = __iter(base64Encode, args, iterator);
35
- if (iter) {
36
- return [...iter].join("");
37
- }
38
- return ([init, complete, reduce]) => {
39
- let state = 0;
40
- let b;
41
- const opts = { safe: false, buffer: 1024, ...args[0] };
42
- const chars = opts.safe ? B64_SAFE : B64_CHARS;
43
- const buf = [];
44
- return [
45
- init,
46
- (acc) => {
47
- switch (state) {
48
- case 1:
49
- buf.push(chars[(b >> 18) & 0x3f], chars[(b >> 12) & 0x3f], "=", "=");
50
- break;
51
- case 2:
52
- buf.push(chars[(b >> 18) & 0x3f], chars[(b >> 12) & 0x3f], chars[(b >> 6) & 0x3f], "=");
53
- break;
54
- default:
55
- }
56
- while (buf.length && !isReduced(acc)) {
57
- acc = reduce(acc, buf.shift());
58
- }
59
- return complete(acc);
60
- },
61
- (acc, x) => {
62
- switch (state) {
63
- case 0:
64
- state = 1;
65
- b = x << 16;
66
- break;
67
- case 1:
68
- state = 2;
69
- b += x << 8;
70
- break;
71
- default:
72
- state = 0;
73
- b += x;
74
- buf.push(chars[(b >> 18) & 0x3f], chars[(b >> 12) & 0x3f], chars[(b >> 6) & 0x3f], chars[b & 0x3f]);
75
- if (buf.length >= opts.buffer) {
76
- for (let i = 0, n = buf.length; i < n && !isReduced(acc); i++) {
77
- acc = reduce(acc, buf[i]);
78
- }
79
- buf.length = 0;
80
- }
81
- }
82
- return acc;
83
- },
84
- ];
85
- };
31
+ function base64Encode(...args) {
32
+ const iter = __iter(base64Encode, args, iterator);
33
+ if (iter) {
34
+ return [...iter].join("");
35
+ }
36
+ return ([init, complete, reduce]) => {
37
+ let state = 0;
38
+ let b;
39
+ const opts = { safe: false, buffer: 1024, ...args[0] };
40
+ const chars = opts.safe ? B64_SAFE : B64_CHARS;
41
+ const buf = [];
42
+ return [
43
+ init,
44
+ (acc) => {
45
+ switch (state) {
46
+ case 1:
47
+ buf.push(
48
+ chars[b >> 18 & 63],
49
+ chars[b >> 12 & 63],
50
+ "=",
51
+ "="
52
+ );
53
+ break;
54
+ case 2:
55
+ buf.push(
56
+ chars[b >> 18 & 63],
57
+ chars[b >> 12 & 63],
58
+ chars[b >> 6 & 63],
59
+ "="
60
+ );
61
+ break;
62
+ default:
63
+ }
64
+ while (buf.length && !isReduced(acc)) {
65
+ acc = reduce(acc, buf.shift());
66
+ }
67
+ return complete(acc);
68
+ },
69
+ (acc, x) => {
70
+ switch (state) {
71
+ case 0:
72
+ state = 1;
73
+ b = x << 16;
74
+ break;
75
+ case 1:
76
+ state = 2;
77
+ b += x << 8;
78
+ break;
79
+ default:
80
+ state = 0;
81
+ b += x;
82
+ buf.push(
83
+ chars[b >> 18 & 63],
84
+ chars[b >> 12 & 63],
85
+ chars[b >> 6 & 63],
86
+ chars[b & 63]
87
+ );
88
+ if (buf.length >= opts.buffer) {
89
+ for (let i = 0, n = buf.length; i < n && !isReduced(acc); i++) {
90
+ acc = reduce(acc, buf[i]);
91
+ }
92
+ buf.length = 0;
93
+ }
94
+ }
95
+ return acc;
96
+ }
97
+ ];
98
+ };
86
99
  }
100
+ export {
101
+ base64Decode,
102
+ base64Encode
103
+ };
package/bits.js CHANGED
@@ -1,24 +1,27 @@
1
1
  import { compR } from "@thi.ng/transducers/compr";
2
2
  import { iterator, __iter } from "@thi.ng/transducers/iterator";
3
3
  import { isReduced } from "@thi.ng/transducers/reduced";
4
- export function bits(...args) {
5
- return (__iter(bits, args, iterator) ||
6
- ((rfn) => {
7
- const reduce = rfn[2];
8
- const size = args[0] || 8;
9
- const msb = args[1] !== false;
10
- return compR(rfn, msb
11
- ? (acc, x) => {
12
- for (let i = size; i-- > 0 && !isReduced(acc);) {
13
- acc = reduce(acc, (x >>> i) & 1);
14
- }
15
- return acc;
16
- }
17
- : (acc, x) => {
18
- for (let i = 0; i < size && !isReduced(acc); i++) {
19
- acc = reduce(acc, (x >>> i) & 1);
20
- }
21
- return acc;
22
- });
23
- }));
4
+ function bits(...args) {
5
+ return __iter(bits, args, iterator) || ((rfn) => {
6
+ const reduce = rfn[2];
7
+ const size = args[0] || 8;
8
+ const msb = args[1] !== false;
9
+ return compR(
10
+ rfn,
11
+ msb ? (acc, x) => {
12
+ for (let i = size; i-- > 0 && !isReduced(acc); ) {
13
+ acc = reduce(acc, x >>> i & 1);
14
+ }
15
+ return acc;
16
+ } : (acc, x) => {
17
+ for (let i = 0; i < size && !isReduced(acc); i++) {
18
+ acc = reduce(acc, x >>> i & 1);
19
+ }
20
+ return acc;
21
+ }
22
+ );
23
+ });
24
24
  }
25
+ export {
26
+ bits
27
+ };
package/bytes.js CHANGED
@@ -1,237 +1,267 @@
1
- import { bytes16, bytes24, bytes32, bytesF32, bytesF64, } from "@thi.ng/binary/bytes";
1
+ import {
2
+ bytes16,
3
+ bytes24,
4
+ bytes32,
5
+ bytesF32,
6
+ bytesF64
7
+ } from "@thi.ng/binary/bytes";
2
8
  import { unsupported } from "@thi.ng/errors/unsupported";
3
9
  import { iterator } from "@thi.ng/transducers/iterator";
4
10
  import { mapcat } from "@thi.ng/transducers/mapcat";
5
11
  import { reduce } from "@thi.ng/transducers/reduce";
6
12
  import { utf8Encode } from "./utf8.js";
7
- export const i8 = (x) => ["i8", x];
8
- export const i8array = (x) => ["i8a", x];
9
- export const u8 = (x) => ["u8", x];
10
- export const u8array = (x) => ["u8a", x];
11
- export const i16 = (x, le = false) => ["i16", x, le];
12
- export const i16array = (x, le = false) => [
13
- "i16a",
14
- x,
15
- le,
13
+ const i8 = (x) => ["i8", x];
14
+ const i8array = (x) => ["i8a", x];
15
+ const u8 = (x) => ["u8", x];
16
+ const u8array = (x) => ["u8a", x];
17
+ const i16 = (x, le = false) => ["i16", x, le];
18
+ const i16array = (x, le = false) => [
19
+ "i16a",
20
+ x,
21
+ le
16
22
  ];
17
- export const u16 = (x, le = false) => ["u16", x, le];
18
- export const u16array = (x, le = false) => [
19
- "u16a",
20
- x,
21
- le,
23
+ const u16 = (x, le = false) => ["u16", x, le];
24
+ const u16array = (x, le = false) => [
25
+ "u16a",
26
+ x,
27
+ le
22
28
  ];
23
- export const i24 = (x, le = false) => ["i24", x, le];
24
- export const i24array = (x, le = false) => [
25
- "i24a",
26
- x,
27
- le,
29
+ const i24 = (x, le = false) => ["i24", x, le];
30
+ const i24array = (x, le = false) => [
31
+ "i24a",
32
+ x,
33
+ le
28
34
  ];
29
- export const u24 = (x, le = false) => ["u24", x, le];
30
- export const u24array = (x, le = false) => [
31
- "u24a",
32
- x,
33
- le,
35
+ const u24 = (x, le = false) => ["u24", x, le];
36
+ const u24array = (x, le = false) => [
37
+ "u24a",
38
+ x,
39
+ le
34
40
  ];
35
- export const i32 = (x, le = false) => ["i32", x, le];
36
- export const i32array = (x, le = false) => [
37
- "i32a",
38
- x,
39
- le,
41
+ const i32 = (x, le = false) => ["i32", x, le];
42
+ const i32array = (x, le = false) => [
43
+ "i32a",
44
+ x,
45
+ le
40
46
  ];
41
- export const u32 = (x, le = false) => ["u32", x, le];
42
- export const u32array = (x, le = false) => [
43
- "u32a",
44
- x,
45
- le,
47
+ const u32 = (x, le = false) => ["u32", x, le];
48
+ const u32array = (x, le = false) => [
49
+ "u32a",
50
+ x,
51
+ le
46
52
  ];
47
- export const f32 = (x, le = false) => ["f32", x, le];
48
- export const f32array = (x, le = false) => [
49
- "f32a",
50
- x,
51
- le,
53
+ const f32 = (x, le = false) => ["f32", x, le];
54
+ const f32array = (x, le = false) => [
55
+ "f32a",
56
+ x,
57
+ le
52
58
  ];
53
- export const f64 = (x, le = false) => ["f64", x, le];
54
- export const f64array = (x, le = false) => [
55
- "f64a",
56
- x,
57
- le,
59
+ const f64 = (x, le = false) => ["f64", x, le];
60
+ const f64array = (x, le = false) => [
61
+ "f64a",
62
+ x,
63
+ le
58
64
  ];
59
- export const str = (x) => ["str", x];
60
- export function asBytes(src) {
61
- return src
62
- ? iterator(asBytes(), src)
63
- : mapcat((x) => {
64
- const val = x[1];
65
- const le = x[2];
66
- switch (x[0]) {
67
- case "i8":
68
- case "u8":
69
- return [val];
70
- case "i8a":
71
- case "u8a":
72
- return x[1];
73
- case "i16":
74
- case "u16":
75
- return bytes16(val, le);
76
- case "i16a":
77
- case "u16a":
78
- return mapcat((x) => bytes16(x, le), x[1]);
79
- case "i24":
80
- case "u24":
81
- return bytes24(val, le);
82
- case "i24a":
83
- case "u24a":
84
- return mapcat((x) => bytes24(x, le), x[1]);
85
- case "i32":
86
- case "u32":
87
- return bytes32(val, le);
88
- case "i32a":
89
- case "u32a":
90
- return mapcat((x) => bytes32(x, le), x[1]);
91
- case "f32":
92
- return bytesF32(val, le);
93
- case "f32a":
94
- return mapcat((x) => bytesF32(x, le), x[1]);
95
- case "f64":
96
- return bytesF64(val, le);
97
- case "f64a":
98
- return mapcat((x) => bytesF64(x, le), x[1]);
99
- case "str":
100
- return utf8Encode(x[1]);
101
- default:
102
- unsupported(`invalid struct item: ${x[0]}`);
103
- }
104
- });
65
+ const str = (x) => ["str", x];
66
+ function asBytes(src) {
67
+ return src ? iterator(asBytes(), src) : mapcat((x) => {
68
+ const val = x[1];
69
+ const le = x[2];
70
+ switch (x[0]) {
71
+ case "i8":
72
+ case "u8":
73
+ return [val];
74
+ case "i8a":
75
+ case "u8a":
76
+ return x[1];
77
+ case "i16":
78
+ case "u16":
79
+ return bytes16(val, le);
80
+ case "i16a":
81
+ case "u16a":
82
+ return mapcat((x2) => bytes16(x2, le), x[1]);
83
+ case "i24":
84
+ case "u24":
85
+ return bytes24(val, le);
86
+ case "i24a":
87
+ case "u24a":
88
+ return mapcat((x2) => bytes24(x2, le), x[1]);
89
+ case "i32":
90
+ case "u32":
91
+ return bytes32(val, le);
92
+ case "i32a":
93
+ case "u32a":
94
+ return mapcat((x2) => bytes32(x2, le), x[1]);
95
+ case "f32":
96
+ return bytesF32(val, le);
97
+ case "f32a":
98
+ return mapcat((x2) => bytesF32(x2, le), x[1]);
99
+ case "f64":
100
+ return bytesF64(val, le);
101
+ case "f64a":
102
+ return mapcat((x2) => bytesF64(x2, le), x[1]);
103
+ case "str":
104
+ return utf8Encode(x[1]);
105
+ default:
106
+ unsupported(`invalid struct item: ${x[0]}`);
107
+ }
108
+ });
105
109
  }
106
- export function bytes(cap = 1024, src) {
107
- let view;
108
- let pos = 0;
109
- const ensure = (acc, size) => {
110
- if (pos + size <= cap)
111
- return acc;
112
- cap *= 2;
113
- const buf = new Uint8Array(cap);
114
- buf.set(acc);
115
- view = new DataView(buf.buffer);
116
- return buf;
117
- };
118
- const setArray = (fn, stride, acc, x, le) => {
119
- const n = x.length;
120
- acc = ensure(acc, stride * n);
121
- for (let i = 0; i < n; i++, pos += stride) {
122
- view[fn](pos, x[i], le);
110
+ function bytes(cap = 1024, src) {
111
+ let view;
112
+ let pos = 0;
113
+ const ensure = (acc, size) => {
114
+ if (pos + size <= cap)
115
+ return acc;
116
+ cap *= 2;
117
+ const buf = new Uint8Array(cap);
118
+ buf.set(acc);
119
+ view = new DataView(buf.buffer);
120
+ return buf;
121
+ };
122
+ const setArray = (fn, stride, acc, x, le) => {
123
+ const n = x.length;
124
+ acc = ensure(acc, stride * n);
125
+ for (let i = 0; i < n; i++, pos += stride) {
126
+ view[fn](pos, x[i], le);
127
+ }
128
+ return acc;
129
+ };
130
+ return src ? reduce(bytes(cap), src) : [
131
+ () => new Uint8Array(cap),
132
+ (acc) => acc.subarray(0, pos),
133
+ (acc, [type, x, le = false]) => {
134
+ if (!view || view.buffer !== acc.buffer) {
135
+ cap = acc.byteLength;
136
+ view = new DataView(acc.buffer, acc.byteOffset);
137
+ }
138
+ switch (type) {
139
+ case "i8":
140
+ acc = ensure(acc, 1);
141
+ view.setInt8(pos, x);
142
+ pos++;
143
+ break;
144
+ case "i8a": {
145
+ const n = x.length;
146
+ acc = ensure(acc, n);
147
+ new Int8Array(acc.buffer, acc.byteOffset).set(
148
+ x,
149
+ pos
150
+ );
151
+ pos += n;
152
+ break;
123
153
  }
124
- return acc;
125
- };
126
- return src
127
- ? reduce(bytes(cap), src)
128
- : [
129
- () => new Uint8Array(cap),
130
- (acc) => acc.subarray(0, pos),
131
- (acc, [type, x, le = false]) => {
132
- if (!view || view.buffer !== acc.buffer) {
133
- cap = acc.byteLength;
134
- view = new DataView(acc.buffer, acc.byteOffset);
135
- }
136
- switch (type) {
137
- case "i8":
138
- acc = ensure(acc, 1);
139
- view.setInt8(pos, x);
140
- pos++;
141
- break;
142
- case "i8a": {
143
- const n = x.length;
144
- acc = ensure(acc, n);
145
- new Int8Array(acc.buffer, acc.byteOffset).set(x, pos);
146
- pos += n;
147
- break;
148
- }
149
- case "u8":
150
- acc = ensure(acc, 1);
151
- view.setUint8(pos, x);
152
- pos++;
153
- break;
154
- case "u8a": {
155
- const n = x.length;
156
- acc = ensure(acc, n);
157
- acc.set(x, pos);
158
- pos += n;
159
- break;
160
- }
161
- case "i16":
162
- acc = ensure(acc, 2);
163
- view.setInt16(pos, x, le);
164
- pos += 2;
165
- break;
166
- case "i16a":
167
- acc = setArray("setInt16", 2, acc, x, le);
168
- break;
169
- case "u16":
170
- acc = ensure(acc, 2);
171
- view.setUint16(pos, x, le);
172
- pos += 2;
173
- break;
174
- case "u16a":
175
- acc = setArray("setUint16", 2, acc, x, le);
176
- break;
177
- case "i24":
178
- acc = ensure(acc, 4);
179
- view.setInt32(pos, x, le);
180
- pos += 3;
181
- break;
182
- case "i24a":
183
- acc = setArray("setInt32", 3, acc, x, le);
184
- break;
185
- case "u24":
186
- acc = ensure(acc, 4);
187
- view.setUint32(pos, x, le);
188
- pos += 3;
189
- break;
190
- case "u24a":
191
- acc = setArray("setUint32", 3, acc, x, le);
192
- break;
193
- case "i32":
194
- acc = ensure(acc, 4);
195
- view.setInt32(pos, x, le);
196
- pos += 4;
197
- break;
198
- case "i32a":
199
- acc = setArray("setInt32", 4, acc, x, le);
200
- break;
201
- case "u32":
202
- acc = ensure(acc, 4);
203
- view.setUint32(pos, x, le);
204
- pos += 4;
205
- break;
206
- case "u32a":
207
- acc = setArray("setUint32", 4, acc, x, le);
208
- break;
209
- case "f32":
210
- acc = ensure(acc, 4);
211
- view.setFloat32(pos, x, le);
212
- pos += 4;
213
- break;
214
- case "f32a":
215
- acc = setArray("setFloat32", 4, acc, x, le);
216
- break;
217
- case "f64":
218
- acc = ensure(acc, 8);
219
- view.setFloat64(pos, x, le);
220
- pos += 8;
221
- break;
222
- case "f64a":
223
- acc = setArray("setFloat64", 8, acc, x, le);
224
- break;
225
- case "str": {
226
- let utf = utf8Encode(x);
227
- acc = ensure(acc, utf.length);
228
- acc.set(utf, pos);
229
- pos += utf.length;
230
- break;
231
- }
232
- default:
233
- }
234
- return acc;
235
- },
236
- ];
154
+ case "u8":
155
+ acc = ensure(acc, 1);
156
+ view.setUint8(pos, x);
157
+ pos++;
158
+ break;
159
+ case "u8a": {
160
+ const n = x.length;
161
+ acc = ensure(acc, n);
162
+ acc.set(x, pos);
163
+ pos += n;
164
+ break;
165
+ }
166
+ case "i16":
167
+ acc = ensure(acc, 2);
168
+ view.setInt16(pos, x, le);
169
+ pos += 2;
170
+ break;
171
+ case "i16a":
172
+ acc = setArray("setInt16", 2, acc, x, le);
173
+ break;
174
+ case "u16":
175
+ acc = ensure(acc, 2);
176
+ view.setUint16(pos, x, le);
177
+ pos += 2;
178
+ break;
179
+ case "u16a":
180
+ acc = setArray("setUint16", 2, acc, x, le);
181
+ break;
182
+ case "i24":
183
+ acc = ensure(acc, 4);
184
+ view.setInt32(pos, x, le);
185
+ pos += 3;
186
+ break;
187
+ case "i24a":
188
+ acc = setArray("setInt32", 3, acc, x, le);
189
+ break;
190
+ case "u24":
191
+ acc = ensure(acc, 4);
192
+ view.setUint32(pos, x, le);
193
+ pos += 3;
194
+ break;
195
+ case "u24a":
196
+ acc = setArray("setUint32", 3, acc, x, le);
197
+ break;
198
+ case "i32":
199
+ acc = ensure(acc, 4);
200
+ view.setInt32(pos, x, le);
201
+ pos += 4;
202
+ break;
203
+ case "i32a":
204
+ acc = setArray("setInt32", 4, acc, x, le);
205
+ break;
206
+ case "u32":
207
+ acc = ensure(acc, 4);
208
+ view.setUint32(pos, x, le);
209
+ pos += 4;
210
+ break;
211
+ case "u32a":
212
+ acc = setArray("setUint32", 4, acc, x, le);
213
+ break;
214
+ case "f32":
215
+ acc = ensure(acc, 4);
216
+ view.setFloat32(pos, x, le);
217
+ pos += 4;
218
+ break;
219
+ case "f32a":
220
+ acc = setArray("setFloat32", 4, acc, x, le);
221
+ break;
222
+ case "f64":
223
+ acc = ensure(acc, 8);
224
+ view.setFloat64(pos, x, le);
225
+ pos += 8;
226
+ break;
227
+ case "f64a":
228
+ acc = setArray("setFloat64", 8, acc, x, le);
229
+ break;
230
+ case "str": {
231
+ let utf = utf8Encode(x);
232
+ acc = ensure(acc, utf.length);
233
+ acc.set(utf, pos);
234
+ pos += utf.length;
235
+ break;
236
+ }
237
+ default:
238
+ }
239
+ return acc;
240
+ }
241
+ ];
237
242
  }
243
+ export {
244
+ asBytes,
245
+ bytes,
246
+ f32,
247
+ f32array,
248
+ f64,
249
+ f64array,
250
+ i16,
251
+ i16array,
252
+ i24,
253
+ i24array,
254
+ i32,
255
+ i32array,
256
+ i8,
257
+ i8array,
258
+ str,
259
+ u16,
260
+ u16array,
261
+ u24,
262
+ u24array,
263
+ u32,
264
+ u32array,
265
+ u8,
266
+ u8array
267
+ };
package/hex-dump.js CHANGED
@@ -6,19 +6,29 @@ import { map } from "@thi.ng/transducers/map";
6
6
  import { mapIndexed } from "@thi.ng/transducers/map-indexed";
7
7
  import { padLast } from "@thi.ng/transducers/pad-last";
8
8
  import { partition } from "@thi.ng/transducers/partition";
9
- export function hexDump(...args) {
10
- const iter = __iter(hexDump, args, iterator);
11
- if (iter) {
12
- return iter;
13
- }
14
- const { cols, address } = { cols: 16, address: 0, ...args[0] };
15
- return comp(padLast(cols, 0), map(juxt(U8, (x) => (x > 31 && x < 127 ? String.fromCharCode(x) : "."))), partition(cols, true), map(juxt((x) => x.map((y) => y[0]).join(" "), (x) => x.map((y) => y[1]).join(""))), mapIndexed((i, [h, a]) => `${U32(address + i * cols)} | ${h} | ${a}`));
9
+ function hexDump(...args) {
10
+ const iter = __iter(hexDump, args, iterator);
11
+ if (iter) {
12
+ return iter;
13
+ }
14
+ const { cols, address } = { cols: 16, address: 0, ...args[0] };
15
+ return comp(
16
+ padLast(cols, 0),
17
+ map(
18
+ juxt(U8, (x) => x > 31 && x < 127 ? String.fromCharCode(x) : ".")
19
+ ),
20
+ partition(cols, true),
21
+ map(
22
+ juxt(
23
+ (x) => x.map((y) => y[0]).join(" "),
24
+ (x) => x.map((y) => y[1]).join("")
25
+ )
26
+ ),
27
+ mapIndexed((i, [h, a]) => `${U32(address + i * cols)} | ${h} | ${a}`)
28
+ );
16
29
  }
17
- /**
18
- * Convenience wrapper for {@link hexDump}, return the hexdump as a
19
- * single result string.
20
- *
21
- * @param opts -
22
- * @param src -
23
- */
24
- export const hexDumpString = (opts, src) => [...hexDump(opts, src)].join("\n");
30
+ const hexDumpString = (opts, src) => [...hexDump(opts, src)].join("\n");
31
+ export {
32
+ hexDump,
33
+ hexDumpString
34
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/transducers-binary",
3
- "version": "2.1.81",
3
+ "version": "2.1.82",
4
4
  "description": "Binary data related transducers & reducers",
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",
@@ -33,16 +35,17 @@
33
35
  "test": "bun test"
34
36
  },
35
37
  "dependencies": {
36
- "@thi.ng/binary": "^3.3.40",
37
- "@thi.ng/compose": "^2.1.50",
38
- "@thi.ng/errors": "^2.4.5",
39
- "@thi.ng/hex": "^2.3.23",
40
- "@thi.ng/random": "^3.6.17",
41
- "@thi.ng/strings": "^3.7.2",
42
- "@thi.ng/transducers": "^8.8.14"
38
+ "@thi.ng/binary": "^3.4.0",
39
+ "@thi.ng/compose": "^2.1.51",
40
+ "@thi.ng/errors": "^2.4.6",
41
+ "@thi.ng/hex": "^2.3.24",
42
+ "@thi.ng/random": "^3.6.18",
43
+ "@thi.ng/strings": "^3.7.3",
44
+ "@thi.ng/transducers": "^8.8.15"
43
45
  },
44
46
  "devDependencies": {
45
47
  "@microsoft/api-extractor": "^7.38.3",
48
+ "esbuild": "^0.19.8",
46
49
  "rimraf": "^5.0.5",
47
50
  "tools": "^0.0.1",
48
51
  "typedoc": "^0.25.4",
@@ -111,5 +114,5 @@
111
114
  ],
112
115
  "year": 2018
113
116
  },
114
- "gitHead": "25f2ac8ff795a432a930119661b364d4d93b59a0\n"
117
+ "gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
115
118
  }
package/partition-bits.js CHANGED
@@ -1,65 +1,62 @@
1
1
  import { iterator, __iter } from "@thi.ng/transducers/iterator";
2
2
  import { isReduced } from "@thi.ng/transducers/reduced";
3
- export function partitionBits(...args) {
4
- return (__iter(partitionBits, args, iterator) ||
5
- ((rfn) => {
6
- const destSize = args[0];
7
- const srcSize = args[1] || 8;
8
- return destSize < srcSize
9
- ? small(rfn, destSize, srcSize)
10
- : destSize > srcSize
11
- ? large(rfn, destSize, srcSize)
12
- : rfn;
13
- }));
3
+ function partitionBits(...args) {
4
+ return __iter(partitionBits, args, iterator) || ((rfn) => {
5
+ const destSize = args[0];
6
+ const srcSize = args[1] || 8;
7
+ return destSize < srcSize ? small(rfn, destSize, srcSize) : destSize > srcSize ? large(rfn, destSize, srcSize) : rfn;
8
+ });
14
9
  }
15
10
  const small = ([init, complete, reduce], n, wordSize) => {
16
- const maxb = wordSize - n;
17
- const m1 = (1 << wordSize) - 1;
18
- const m2 = (1 << n) - 1;
19
- let r = 0;
20
- let y = 0;
21
- return [
22
- init,
23
- (acc) => complete(r > 0 ? reduce(acc, y) : acc),
24
- (acc, x) => {
25
- let b = 0;
26
- do {
27
- acc = reduce(acc, y + ((x >>> (maxb + r)) & m2));
28
- b += n - r;
29
- x = (x << (n - r)) & m1;
30
- y = 0;
31
- r = 0;
32
- } while (b <= maxb && !isReduced(acc));
33
- r = wordSize - b;
34
- y = r > 0 ? (x >>> maxb) & m2 : 0;
35
- return acc;
36
- },
37
- ];
11
+ const maxb = wordSize - n;
12
+ const m1 = (1 << wordSize) - 1;
13
+ const m2 = (1 << n) - 1;
14
+ let r = 0;
15
+ let y = 0;
16
+ return [
17
+ init,
18
+ (acc) => complete(r > 0 ? reduce(acc, y) : acc),
19
+ (acc, x) => {
20
+ let b = 0;
21
+ do {
22
+ acc = reduce(acc, y + (x >>> maxb + r & m2));
23
+ b += n - r;
24
+ x = x << n - r & m1;
25
+ y = 0;
26
+ r = 0;
27
+ } while (b <= maxb && !isReduced(acc));
28
+ r = wordSize - b;
29
+ y = r > 0 ? x >>> maxb & m2 : 0;
30
+ return acc;
31
+ }
32
+ ];
38
33
  };
39
34
  const large = ([init, complete, reduce], n, wordSize) => {
40
- const m1 = (1 << wordSize) - 1;
41
- let r = 0;
42
- let y = 0;
43
- return [
44
- init,
45
- (acc) => complete(r > 0 ? reduce(acc, y) : acc),
46
- (acc, x) => {
47
- if (r + wordSize <= n) {
48
- y |= (x & m1) << (n - wordSize - r);
49
- r += wordSize;
50
- if (r === n) {
51
- acc = reduce(acc, y);
52
- y = 0;
53
- r = 0;
54
- }
55
- }
56
- else {
57
- const k = n - r;
58
- r = wordSize - k;
59
- acc = reduce(acc, y | ((x >>> r) & ((1 << k) - 1)));
60
- y = (x & ((1 << r) - 1)) << (n - r);
61
- }
62
- return acc;
63
- },
64
- ];
35
+ const m1 = (1 << wordSize) - 1;
36
+ let r = 0;
37
+ let y = 0;
38
+ return [
39
+ init,
40
+ (acc) => complete(r > 0 ? reduce(acc, y) : acc),
41
+ (acc, x) => {
42
+ if (r + wordSize <= n) {
43
+ y |= (x & m1) << n - wordSize - r;
44
+ r += wordSize;
45
+ if (r === n) {
46
+ acc = reduce(acc, y);
47
+ y = 0;
48
+ r = 0;
49
+ }
50
+ } else {
51
+ const k = n - r;
52
+ r = wordSize - k;
53
+ acc = reduce(acc, y | x >>> r & (1 << k) - 1);
54
+ y = (x & (1 << r) - 1) << n - r;
55
+ }
56
+ return acc;
57
+ }
58
+ ];
59
+ };
60
+ export {
61
+ partitionBits
65
62
  };
package/random-bits.js CHANGED
@@ -1,16 +1,6 @@
1
1
  import { SYSTEM } from "@thi.ng/random/system";
2
2
  import { repeatedly } from "@thi.ng/transducers/repeatedly";
3
- /**
4
- * Returns an iterator of random bits, with 1's occurring w/ given probability
5
- * `prob` (in the [0,1] interval). If `num` is given, only that many bits will
6
- * be produced.
7
- *
8
- * By default, uses system PRNG, but a custom
9
- * [`IRandom`](https://docs.thi.ng/umbrella/random/interfaces/IRandom.html) impl
10
- * can be provided via `rnd` arg.
11
- *
12
- * @param prob -
13
- * @param num -
14
- * @param rnd -
15
- */
16
- export const randomBits = (prob, num, rnd = SYSTEM) => repeatedly(() => (rnd.probability(prob) ? 1 : 0), num);
3
+ const randomBits = (prob, num, rnd = SYSTEM) => repeatedly(() => rnd.probability(prob) ? 1 : 0, num);
4
+ export {
5
+ randomBits
6
+ };
package/utf8.js CHANGED
@@ -1,145 +1,148 @@
1
- import { fromUtf8CodePoint, utf8Length as $utf8Length, } from "@thi.ng/strings/utf8";
1
+ import {
2
+ fromUtf8CodePoint,
3
+ utf8Length as $utf8Length
4
+ } from "@thi.ng/strings/utf8";
2
5
  import { compR } from "@thi.ng/transducers/compr";
3
6
  import { iterator, iterator1 } from "@thi.ng/transducers/iterator";
4
7
  import { isReduced } from "@thi.ng/transducers/reduced";
5
- export function utf8Decode(src) {
6
- return src
7
- ? [...iterator1(utf8Decode(), src)].join("")
8
- : (rfn) => {
9
- const r = rfn[2];
10
- let state = 0;
11
- let u0;
12
- let u1;
13
- let u2;
14
- let u3;
15
- let u4;
16
- return compR(rfn, (acc, x) => {
17
- switch (state) {
18
- case 0:
19
- default:
20
- if (x < 0x80) {
21
- return r(acc, String.fromCharCode(x));
22
- }
23
- u0 = x;
24
- state = 1;
25
- break;
26
- case 1:
27
- u1 = x & 0x3f;
28
- if ((u0 & 0xe0) === 0xc0) {
29
- state = 0;
30
- return r(acc, String.fromCharCode(((u0 & 0x1f) << 6) | u1));
31
- }
32
- state = 2;
33
- break;
34
- case 2:
35
- u2 = x & 0x3f;
36
- if ((u0 & 0xf0) === 0xe0) {
37
- state = 0;
38
- return r(acc, String.fromCharCode(((u0 & 0x0f) << 12) | (u1 << 6) | u2));
39
- }
40
- state = 3;
41
- break;
42
- case 3:
43
- u3 = x & 0x3f;
44
- if ((u0 & 0xf8) === 0xf0) {
45
- state = 0;
46
- return r(acc, fromUtf8CodePoint(((u0 & 7) << 18) |
47
- (u1 << 12) |
48
- (u2 << 6) |
49
- u3));
50
- }
51
- state = 4;
52
- break;
53
- case 4:
54
- u4 = x & 0x3f;
55
- if ((u0 & 0xfc) === 0xf8) {
56
- state = 0;
57
- return r(acc, fromUtf8CodePoint(((u0 & 3) << 24) |
58
- (u1 << 18) |
59
- (u2 << 12) |
60
- (u3 << 6) |
61
- u4));
62
- }
63
- state = 5;
64
- break;
65
- case 5:
66
- state = 0;
67
- return r(acc, fromUtf8CodePoint(((u0 & 1) << 30) |
68
- (u1 << 24) |
69
- (u2 << 18) |
70
- (u3 << 12) |
71
- (u4 << 6) |
72
- (x & 0x3f)));
73
- }
74
- return acc;
75
- });
76
- };
8
+ function utf8Decode(src) {
9
+ return src ? [...iterator1(utf8Decode(), src)].join("") : (rfn) => {
10
+ const r = rfn[2];
11
+ let state = 0;
12
+ let u0;
13
+ let u1;
14
+ let u2;
15
+ let u3;
16
+ let u4;
17
+ return compR(rfn, (acc, x) => {
18
+ switch (state) {
19
+ case 0:
20
+ default:
21
+ if (x < 128) {
22
+ return r(acc, String.fromCharCode(x));
23
+ }
24
+ u0 = x;
25
+ state = 1;
26
+ break;
27
+ case 1:
28
+ u1 = x & 63;
29
+ if ((u0 & 224) === 192) {
30
+ state = 0;
31
+ return r(
32
+ acc,
33
+ String.fromCharCode((u0 & 31) << 6 | u1)
34
+ );
35
+ }
36
+ state = 2;
37
+ break;
38
+ case 2:
39
+ u2 = x & 63;
40
+ if ((u0 & 240) === 224) {
41
+ state = 0;
42
+ return r(
43
+ acc,
44
+ String.fromCharCode(
45
+ (u0 & 15) << 12 | u1 << 6 | u2
46
+ )
47
+ );
48
+ }
49
+ state = 3;
50
+ break;
51
+ case 3:
52
+ u3 = x & 63;
53
+ if ((u0 & 248) === 240) {
54
+ state = 0;
55
+ return r(
56
+ acc,
57
+ fromUtf8CodePoint(
58
+ (u0 & 7) << 18 | u1 << 12 | u2 << 6 | u3
59
+ )
60
+ );
61
+ }
62
+ state = 4;
63
+ break;
64
+ case 4:
65
+ u4 = x & 63;
66
+ if ((u0 & 252) === 248) {
67
+ state = 0;
68
+ return r(
69
+ acc,
70
+ fromUtf8CodePoint(
71
+ (u0 & 3) << 24 | u1 << 18 | u2 << 12 | u3 << 6 | u4
72
+ )
73
+ );
74
+ }
75
+ state = 5;
76
+ break;
77
+ case 5:
78
+ state = 0;
79
+ return r(
80
+ acc,
81
+ fromUtf8CodePoint(
82
+ (u0 & 1) << 30 | u1 << 24 | u2 << 18 | u3 << 12 | u4 << 6 | x & 63
83
+ )
84
+ );
85
+ }
86
+ return acc;
87
+ });
88
+ };
77
89
  }
78
- export function utf8Encode(src) {
79
- return src != null
80
- ? new Uint8Array([...iterator(utf8Encode(), src)])
81
- : (rfn) => {
82
- const r = rfn[2];
83
- return compR(rfn, (acc, x) => {
84
- let u = x.charCodeAt(0), buf;
85
- if (u >= 0xd800 && u <= 0xdfff) {
86
- u =
87
- (0x10000 + ((u & 0x3ff) << 10)) |
88
- (x.charCodeAt(1) & 0x3ff);
89
- }
90
- if (u < 0x80) {
91
- return r(acc, u);
92
- }
93
- else if (u < 0x800) {
94
- buf = [0xc0 | (u >> 6), 0x80 | (u & 0x3f)];
95
- }
96
- else if (u < 0x10000) {
97
- buf = [
98
- 0xe0 | (u >> 12),
99
- 0x80 | ((u >> 6) & 0x3f),
100
- 0x80 | (u & 0x3f),
101
- ];
102
- }
103
- else if (u < 0x200000) {
104
- buf = [
105
- 0xf0 | (u >> 18),
106
- 0x80 | ((u >> 12) & 0x3f),
107
- 0x80 | ((u >> 6) & 0x3f),
108
- 0x80 | (u & 0x3f),
109
- ];
110
- }
111
- else if (u < 0x4000000) {
112
- buf = [
113
- 0xf8 | (u >> 24),
114
- 0x80 | ((u >> 18) & 0x3f),
115
- 0x80 | ((u >> 12) & 0x3f),
116
- 0x80 | ((u >> 6) & 0x3f),
117
- 0x80 | (u & 0x3f),
118
- ];
119
- }
120
- else {
121
- buf = [
122
- 0xfc | (u >> 30),
123
- 0x80 | ((u >> 24) & 0x3f),
124
- 0x80 | ((u >> 18) & 0x3f),
125
- 0x80 | ((u >> 12) & 0x3f),
126
- 0x80 | ((u >> 6) & 0x3f),
127
- 0x80 | (u & 0x3f),
128
- ];
129
- }
130
- for (let i = 0, n = buf.length; i < n; i++) {
131
- acc = r(acc, buf[i]);
132
- if (isReduced(acc)) {
133
- break;
134
- }
135
- }
136
- return acc;
137
- });
138
- };
90
+ function utf8Encode(src) {
91
+ return src != null ? new Uint8Array([...iterator(utf8Encode(), src)]) : (rfn) => {
92
+ const r = rfn[2];
93
+ return compR(rfn, (acc, x) => {
94
+ let u = x.charCodeAt(0), buf;
95
+ if (u >= 55296 && u <= 57343) {
96
+ u = 65536 + ((u & 1023) << 10) | x.charCodeAt(1) & 1023;
97
+ }
98
+ if (u < 128) {
99
+ return r(acc, u);
100
+ } else if (u < 2048) {
101
+ buf = [192 | u >> 6, 128 | u & 63];
102
+ } else if (u < 65536) {
103
+ buf = [
104
+ 224 | u >> 12,
105
+ 128 | u >> 6 & 63,
106
+ 128 | u & 63
107
+ ];
108
+ } else if (u < 2097152) {
109
+ buf = [
110
+ 240 | u >> 18,
111
+ 128 | u >> 12 & 63,
112
+ 128 | u >> 6 & 63,
113
+ 128 | u & 63
114
+ ];
115
+ } else if (u < 67108864) {
116
+ buf = [
117
+ 248 | u >> 24,
118
+ 128 | u >> 18 & 63,
119
+ 128 | u >> 12 & 63,
120
+ 128 | u >> 6 & 63,
121
+ 128 | u & 63
122
+ ];
123
+ } else {
124
+ buf = [
125
+ 252 | u >> 30,
126
+ 128 | u >> 24 & 63,
127
+ 128 | u >> 18 & 63,
128
+ 128 | u >> 12 & 63,
129
+ 128 | u >> 6 & 63,
130
+ 128 | u & 63
131
+ ];
132
+ }
133
+ for (let i = 0, n = buf.length; i < n; i++) {
134
+ acc = r(acc, buf[i]);
135
+ if (isReduced(acc)) {
136
+ break;
137
+ }
138
+ }
139
+ return acc;
140
+ });
141
+ };
139
142
  }
140
- /**
141
- * Re-export of [utf8Length()](https://docs.thi.ng/umbrella/strings/functions/utf8Length.html).
142
- *
143
- * @deprecated migrated to thi.ng/strings pkg
144
- */
145
- export const utf8Length = $utf8Length;
143
+ const utf8Length = $utf8Length;
144
+ export {
145
+ utf8Decode,
146
+ utf8Encode,
147
+ utf8Length
148
+ };