@thi.ng/transducers-binary 2.1.103 → 2.1.105
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 +1 -1
- package/README.md +37 -22
- package/bits.d.ts +4 -1
- package/hex-dump.d.ts +1 -0
- package/package.json +11 -10
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -101,26 +101,24 @@ directory are using this package:
|
|
|
101
101
|
|
|
102
102
|
[Generated API docs](https://docs.thi.ng/umbrella/transducers-binary/)
|
|
103
103
|
|
|
104
|
-
```ts
|
|
105
|
-
import * as tx from "@thi.ng/transducers";
|
|
106
|
-
import * as txb from "@thi.ng/transducers-binary";
|
|
107
|
-
```
|
|
108
|
-
|
|
109
104
|
### Random bits
|
|
110
105
|
|
|
111
106
|
```ts
|
|
107
|
+
import { take } from "@thi.ng/transducers";
|
|
108
|
+
import { randomBits } from "@thi.ng/transducers-binary";
|
|
109
|
+
|
|
112
110
|
// 10 samples with 50% probability of drawing a 1
|
|
113
|
-
[...
|
|
111
|
+
[...randomBits(0.5, 10)]
|
|
114
112
|
// [ 1, 0, 1, 1, 0, 1, 0, 1, 1, 0 ]
|
|
115
113
|
|
|
116
114
|
// infinite iterator without 2nd arg, so limit with `take()`
|
|
117
|
-
[...
|
|
115
|
+
[...take(10, randomBits(0.1))]
|
|
118
116
|
// [ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 ]
|
|
119
117
|
|
|
120
118
|
import { Smush32 } from "@thi.ng/random";
|
|
121
119
|
|
|
122
120
|
// with seeded PRNG
|
|
123
|
-
[...
|
|
121
|
+
[...randomBits(0.5, 10, new Smush32(12345678))]
|
|
124
122
|
// [ 0, 0, 1, 1, 0, 0, 0, 0, 1, 0 ]
|
|
125
123
|
```
|
|
126
124
|
|
|
@@ -131,9 +129,11 @@ transducers. [See code
|
|
|
131
129
|
here](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers-binary/src/hex-dump.ts).
|
|
132
130
|
|
|
133
131
|
```ts
|
|
132
|
+
import { hexDump } from "@thi.ng/transducers-binary";
|
|
133
|
+
|
|
134
134
|
src = [65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 33, 48, 49, 50, 51, 126, 122, 121, 120]
|
|
135
135
|
|
|
136
|
-
[...
|
|
136
|
+
[...hexDump({ cols: 8, address: 0x100 }, src)]
|
|
137
137
|
// [ '00000100 | 41 42 43 44 45 46 47 48 | ABCDEFGH',
|
|
138
138
|
// '00000108 | 49 4a 21 30 31 32 33 7e | IJ!0123~',
|
|
139
139
|
// '00000110 | 7a 79 78 00 00 00 00 00 | zyx.....' ]
|
|
@@ -147,6 +147,9 @@ reducer transforms a stream of declarative data definitions (optionally
|
|
|
147
147
|
with Little-Endian encoding) into an `Uint8Array`.
|
|
148
148
|
|
|
149
149
|
```ts
|
|
150
|
+
import * as tx from "@thi.ng/transducers";
|
|
151
|
+
import * as txb from "@thi.ng/transducers-binary";
|
|
152
|
+
|
|
150
153
|
const bytes = txb.bytes(
|
|
151
154
|
// initial buffer capacity (grows on demand)
|
|
152
155
|
32,
|
|
@@ -175,18 +178,21 @@ console.log(tx.str("\n", txb.hexDump({}, bytes)));
|
|
|
175
178
|
Decompose / transform a stream of fixed size words into their bits:
|
|
176
179
|
|
|
177
180
|
```ts
|
|
178
|
-
|
|
181
|
+
import { comp, map, partition, str, transduce } from "@thi.ng/transducers";
|
|
182
|
+
import { bits } from "@thi.ng/transducers-binary";
|
|
183
|
+
|
|
184
|
+
[...bits(8, [0xf0, 0xaa])];
|
|
179
185
|
// [ 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0 ]
|
|
180
186
|
|
|
181
187
|
console.log(
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
+
transduce(
|
|
189
|
+
comp(
|
|
190
|
+
bits(8),
|
|
191
|
+
map((x) => (x ? "#" : ".")),
|
|
192
|
+
partition(8),
|
|
193
|
+
map((x) => x.join(""))
|
|
188
194
|
),
|
|
189
|
-
|
|
195
|
+
str("\n"),
|
|
190
196
|
[0x00, 0x18, 0x3c, 0x66, 0x66, 0x7e, 0x66, 0x00]
|
|
191
197
|
)
|
|
192
198
|
);
|
|
@@ -204,7 +210,10 @@ Extended to transform longer strings (taken from the [bitmap-font
|
|
|
204
210
|
example](https://github.com/thi-ng/umbrella/tree/develop/examples/bitmap-font),
|
|
205
211
|
[live demo](https://demo.thi.ng/umbrella/bitmap-font/)):
|
|
206
212
|
|
|
207
|
-
```
|
|
213
|
+
```js
|
|
214
|
+
import * as tx from "@thi.ng/transducers";
|
|
215
|
+
import { bits } from "@thi.ng/transducers-binary";
|
|
216
|
+
|
|
208
217
|
// font lookup table
|
|
209
218
|
const chars = {
|
|
210
219
|
a: [0x00, 0x18, 0x3c, 0x66, 0x66, 0x7e, 0x66, 0x00],
|
|
@@ -218,7 +227,7 @@ const xfJoin = tx.map((x) => x.join(""));
|
|
|
218
227
|
const xfChar = (i) =>
|
|
219
228
|
tx.comp(
|
|
220
229
|
tx.pluck(i),
|
|
221
|
-
|
|
230
|
+
bits(8),
|
|
222
231
|
tx.map((x) => (x ? "#" : ".")),
|
|
223
232
|
tx.partition(8),
|
|
224
233
|
xfJoin
|
|
@@ -258,10 +267,16 @@ strings, these transducers stepwise convert byte values to base64 and
|
|
|
258
267
|
back.
|
|
259
268
|
|
|
260
269
|
```ts
|
|
270
|
+
import * as tx from "@thi.ng/transducers";
|
|
271
|
+
import {
|
|
272
|
+
base64Decode, base64Encode,
|
|
273
|
+
utf8Decode, utf8Encode
|
|
274
|
+
} from "@thi.ng/transducers-binary";
|
|
275
|
+
|
|
261
276
|
// here we first add an offset (0x80) to allow negative values to be encoded
|
|
262
277
|
// (URL safe results can be produced via opt arg to `base64Encode`)
|
|
263
278
|
enc = tx.transduce(
|
|
264
|
-
tx.comp(tx.map((x) => x + 0x80),
|
|
279
|
+
tx.comp(tx.map((x) => x + 0x80), base64Encode()),
|
|
265
280
|
tx.str(),
|
|
266
281
|
tx.range(-8, 8)
|
|
267
282
|
);
|
|
@@ -281,13 +296,13 @@ enc = tx.transduce(
|
|
|
281
296
|
// [ -8, -7, -6, -5, -4, -3, -2, -1 ]
|
|
282
297
|
|
|
283
298
|
buf = tx.transduce(
|
|
284
|
-
tx.comp(
|
|
299
|
+
tx.comp(utf8Encode(), base64Encode()),
|
|
285
300
|
tx.str(),
|
|
286
301
|
"beer (🍺) or hot beverage (☕️)"
|
|
287
302
|
);
|
|
288
303
|
// "YmVlciAo8J+Nuikgb3IgaG90IGJldmVyYWdlICjimJXvuI4p"
|
|
289
304
|
|
|
290
|
-
tx.transduce(tx.comp(
|
|
305
|
+
tx.transduce(tx.comp(base64Decode(), utf8Decode()), tx.str(), buf);
|
|
291
306
|
// "beer (🍺) or hot beverage (☕️)"
|
|
292
307
|
```
|
|
293
308
|
|
package/bits.d.ts
CHANGED
|
@@ -4,7 +4,10 @@ import type { Transducer } from "@thi.ng/transducers";
|
|
|
4
4
|
* word size (default 8) and order (MSB first or LSB first). Only the
|
|
5
5
|
* lowest `wordSize` bits of each value are used (max 32).
|
|
6
6
|
*
|
|
7
|
-
* ```
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { bits } from "@thi.ng/transducers-binary";
|
|
9
|
+
* import { comp, iterator, partition } from "@thi.ng/transducers";
|
|
10
|
+
*
|
|
8
11
|
* [...bits(8, [0xf0, 0xaa])]
|
|
9
12
|
* // [ 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0 ]
|
|
10
13
|
* [...iterator(comp(bits(8), partition(4)), [0xf0, 0xaa])]
|
package/hex-dump.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/transducers-binary",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.105",
|
|
4
4
|
"description": "Binary data related transducers & reducers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -32,16 +32,17 @@
|
|
|
32
32
|
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
|
|
33
33
|
"doc:readme": "bun ../../tools/src/module-stats.ts && bun ../../tools/src/readme.ts",
|
|
34
34
|
"pub": "yarn npm publish --access public",
|
|
35
|
-
"test": "bun test"
|
|
35
|
+
"test": "bun test",
|
|
36
|
+
"tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
|
|
36
37
|
},
|
|
37
38
|
"dependencies": {
|
|
38
|
-
"@thi.ng/binary": "^3.4.
|
|
39
|
-
"@thi.ng/compose": "^2.1.
|
|
40
|
-
"@thi.ng/errors": "^2.4.
|
|
41
|
-
"@thi.ng/hex": "^2.3.
|
|
42
|
-
"@thi.ng/random": "^3.6.
|
|
43
|
-
"@thi.ng/strings": "^3.7.
|
|
44
|
-
"@thi.ng/transducers": "^8.9.
|
|
39
|
+
"@thi.ng/binary": "^3.4.17",
|
|
40
|
+
"@thi.ng/compose": "^2.1.67",
|
|
41
|
+
"@thi.ng/errors": "^2.4.20",
|
|
42
|
+
"@thi.ng/hex": "^2.3.39",
|
|
43
|
+
"@thi.ng/random": "^3.6.35",
|
|
44
|
+
"@thi.ng/strings": "^3.7.21",
|
|
45
|
+
"@thi.ng/transducers": "^8.9.10"
|
|
45
46
|
},
|
|
46
47
|
"devDependencies": {
|
|
47
48
|
"@microsoft/api-extractor": "^7.40.1",
|
|
@@ -113,5 +114,5 @@
|
|
|
113
114
|
],
|
|
114
115
|
"year": 2018
|
|
115
116
|
},
|
|
116
|
-
"gitHead": "
|
|
117
|
+
"gitHead": "a421058a65ba76608d94129ac29451bfedaf201c\n"
|
|
117
118
|
}
|