@thi.ng/transducers 8.0.6 → 8.1.1

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/permutations.js CHANGED
@@ -56,7 +56,7 @@ export const permutationsN = (n, m = n, offsets) => {
56
56
  illegalArgs(`insufficient offsets, got ${offsets.length}, needed ${n}`);
57
57
  }
58
58
  const seqs = [];
59
- while (--n >= 0) {
59
+ while (n-- > 0) {
60
60
  const o = offsets ? offsets[n] : 0;
61
61
  seqs[n] = range(o, o + m);
62
62
  }
package/rechunk.d.ts ADDED
@@ -0,0 +1,47 @@
1
+ import type { Transducer } from "./api.js";
2
+ /**
3
+ * Stateful transducer to rechunk/split strings using optional provided regexp
4
+ * (or using `/\r?\n/` (line breaks) by default).
5
+ *
6
+ * @remarks
7
+ * Each incoming string is appended to current buffer string, which is then
8
+ * split using the regexp and re-emitted to as new chunks.
9
+ *
10
+ * One of the main use cases for this transducer is to work in conjunction with
11
+ * NodeJS' stream processing.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * [...rechunk(/-/, ["abc-d", "ef-g-", "hij", "-k-lm"])]
16
+ * // [ "abc", "def", "g", "hij", "k", "lm" ]
17
+ * ```
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * import { spawn } from "child_process"
22
+ * import { fromNodeJS, trace } from "@thi.ng/rstream";
23
+ *
24
+ * const cmd = spawn("ls", ["-la"]);
25
+ *
26
+ * // (btw. also see linesFromNodeJS() for automatic rechunking)
27
+ * fromNodeJS<string>(cmd.stdout, cmd.stderr)
28
+ * .transform(rechunk())
29
+ * .subscribe(trace("output"));
30
+ *
31
+ * // output total 12760
32
+ * // output drwxr-xr-x 37 foo staff 1184 Nov 15 15:29 .
33
+ * // output drwxr-xr-x 143 foo staff 4576 Nov 11 21:08 ..
34
+ * // output drwxr-xr-x 17 foo staff 544 Nov 15 17:39 .git
35
+ * // output -rw-r--r-- 1 foo staff 149 Aug 4 15:32 .gitattributes
36
+ * // output drwxr-xr-x 5 foo staff 160 Apr 12 2021 .github
37
+ * // output -rw-r--r-- 1 foo staff 659 Sep 10 22:55 .gitignore
38
+ * // ...
39
+ * // output done
40
+ * ```
41
+ *
42
+ * @param re
43
+ */
44
+ export declare function rechunk(re?: RegExp): Transducer<string, string>;
45
+ export declare function rechunk(xs: Iterable<string>): IterableIterator<string>;
46
+ export declare function rechunk(re: RegExp, xs: Iterable<string>): IterableIterator<string>;
47
+ //# sourceMappingURL=rechunk.d.ts.map
package/rechunk.js ADDED
@@ -0,0 +1,34 @@
1
+ import { iterator, __iter } from "./iterator.js";
2
+ import { isReduced } from "./reduced.js";
3
+ export function rechunk(...args) {
4
+ const iter = __iter(rechunk, args, iterator);
5
+ if (iter)
6
+ return iter;
7
+ return ([init, complete, reduce]) => {
8
+ let buf = "";
9
+ const re = args[0] || /\r?\n/;
10
+ return [
11
+ init,
12
+ (acc) => {
13
+ if (buf)
14
+ acc = reduce(acc, buf);
15
+ return complete(acc);
16
+ },
17
+ (acc, chunk) => {
18
+ buf += chunk;
19
+ const res = buf.split(re);
20
+ if (res.length > 1) {
21
+ buf = res.pop();
22
+ for (let l of res) {
23
+ acc = reduce(acc, l);
24
+ if (isReduced(acc)) {
25
+ buf = "";
26
+ break;
27
+ }
28
+ }
29
+ }
30
+ return acc;
31
+ },
32
+ ];
33
+ };
34
+ }
package/reduce.js CHANGED
@@ -30,7 +30,7 @@ export function reduceRight(...args) {
30
30
  args = parseArgs(args);
31
31
  let acc = args[0] == null ? init() : args[0];
32
32
  const xs = args[1];
33
- for (let i = xs.length; --i >= 0;) {
33
+ for (let i = xs.length; i-- > 0;) {
34
34
  acc = reduce(acc, xs[i]);
35
35
  if (isReduced(acc)) {
36
36
  acc = acc.deref();
package/reverse.js CHANGED
@@ -14,7 +14,7 @@ import { ensureArray } from "@thi.ng/arrays/ensure-array";
14
14
  export function* reverse(input) {
15
15
  const _input = ensureArray(input);
16
16
  let n = _input.length;
17
- while (--n >= 0) {
17
+ while (n-- > 0) {
18
18
  yield _input[n];
19
19
  }
20
20
  }