@thi.ng/transducers-async 0.2.22 → 0.3.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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2024-10-05T12:12:32Z
3
+ - **Last updated**: 2024-10-14T19:58:28Z
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.
@@ -9,6 +9,14 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [0.3.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/transducers-async@0.3.0) (2024-10-14)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add intercept() transducer ([8d87215](https://github.com/thi-ng/umbrella/commit/8d87215))
17
+ - add tests & docs
18
+ - update pkg exports
19
+
12
20
  ### [0.2.6](https://github.com/thi-ng/umbrella/tree/@thi.ng/transducers-async@0.2.6) (2024-06-21)
13
21
 
14
22
  #### ♻️ Refactoring
package/README.md CHANGED
@@ -116,7 +116,7 @@ For Node.js REPL:
116
116
  const txa = await import("@thi.ng/transducers-async");
117
117
  ```
118
118
 
119
- Package sizes (brotli'd, pre-treeshake): ESM: 3.11 KB
119
+ Package sizes (brotli'd, pre-treeshake): ESM: 3.20 KB
120
120
 
121
121
  ## Dependencies
122
122
 
package/index.d.ts CHANGED
@@ -8,6 +8,7 @@ export * from "./delayed.js";
8
8
  export * from "./ensure.js";
9
9
  export * from "./events.js";
10
10
  export * from "./filter.js";
11
+ export * from "./intercept.js";
11
12
  export * from "./iterator.js";
12
13
  export * from "./map.js";
13
14
  export * from "./mapcat.js";
package/index.js CHANGED
@@ -8,6 +8,7 @@ export * from "./delayed.js";
8
8
  export * from "./ensure.js";
9
9
  export * from "./events.js";
10
10
  export * from "./filter.js";
11
+ export * from "./intercept.js";
11
12
  export * from "./iterator.js";
12
13
  export * from "./map.js";
13
14
  export * from "./mapcat.js";
package/intercept.d.ts ADDED
@@ -0,0 +1,68 @@
1
+ import type { Fn, MaybeAsyncIterable, MaybePromise, Nullable } from "@thi.ng/api";
2
+ import type { AsyncTransducer } from "./api.js";
3
+ export type Interceptor<T> = Fn<T, MaybePromise<Nullable<T>>>;
4
+ export interface AsyncInterceptor<T> extends AsyncTransducer<T, T> {
5
+ /**
6
+ * Adds given interceptor function to the beginning of the list of interceptors.
7
+ *
8
+ * @param pred
9
+ */
10
+ prepend(pred: Interceptor<T>): void;
11
+ /**
12
+ * Adds given interceptor function to the end of the list of interceptors.
13
+ *
14
+ * @param pred
15
+ */
16
+ append(pred: Interceptor<T>): void;
17
+ /**
18
+ * Removes interceptor from list of interceptors and returns true if
19
+ * successful.
20
+ *
21
+ * @param pred
22
+ */
23
+ remove(pred: Interceptor<T>): boolean;
24
+ }
25
+ /**
26
+ * Async transducer. Applies a (dynamically changeable) list of interceptor
27
+ * functions to augment/transform incoming values, or drop them entirely.
28
+ *
29
+ * @remarks
30
+ * Interceptor functions are always applied in series to each received value.
31
+ * Each interceptor can stop processing of a value by returning `null` or
32
+ * `undefined`, otherwise the function is free to augment/transform the value
33
+ * (but keeping its type) and the result is used as input for the next
34
+ * interceptor, and eventually as result of the entire transducer function.
35
+ *
36
+ * If `intercept()` is called without source iterable and returns an transducer,
37
+ * interceptor functions can be dynamically added or removed via the exposed
38
+ * functions in {@link AsyncInterceptor}.
39
+ *
40
+ * @example
41
+ * ```ts tangle:../export/intercept.ts
42
+ * import { intercept, iterator } from "@thi.ng/transducers-async";
43
+ *
44
+ * // tag-based inference
45
+ * const xform = intercept<string[]>([
46
+ * // add an "untitled" tag, if needed
47
+ * (tags) => !tags.find(x => /^title:/.test(x)) ? [...tags, "untitled"] : tags,
48
+ * ]);
49
+ *
50
+ * // dynamically add a second interceptor to skip items if they're tagged with "temp"
51
+ * // using .prepend() here to avoid extraneous processing
52
+ * xform.prepend((tags) => tags.includes("temp") ? null : tags);
53
+ *
54
+ * const items = [
55
+ * ["photo1", "title:test"],
56
+ * ["photo2"],
57
+ * ["photo3", "temp"],
58
+ * ];
59
+ *
60
+ * // process inputs and display results
61
+ * for await(let tags of iterator(xform, items)) console.log(tags);
62
+ * ```
63
+ *
64
+ * @param interceptors
65
+ */
66
+ export declare function intercept<T>(interceptors?: Interceptor<T>[]): AsyncInterceptor<T>;
67
+ export declare function intercept<T>(interceptors: Interceptor<T>[], src: MaybeAsyncIterable<T>): AsyncIterableIterator<T>;
68
+ //# sourceMappingURL=intercept.d.ts.map
package/intercept.js ADDED
@@ -0,0 +1,31 @@
1
+ import { compR } from "./compr.js";
2
+ import { iterator1 } from "./iterator.js";
3
+ function intercept(interceptors = [], src) {
4
+ if (src) return iterator1(intercept(interceptors), src);
5
+ const xform = (rfn) => compR(rfn, async (acc, x) => {
6
+ for (let fn of interceptors) {
7
+ const res = await fn(x);
8
+ if (res == null) return acc;
9
+ x = res;
10
+ }
11
+ return rfn[2](acc, x);
12
+ });
13
+ xform.prepend = (fn) => {
14
+ interceptors.unshift(fn);
15
+ };
16
+ xform.append = (fn) => {
17
+ interceptors.push(fn);
18
+ };
19
+ xform.remove = (fn) => {
20
+ const idx = interceptors.indexOf(fn);
21
+ if (idx >= 0) {
22
+ interceptors.splice(idx, 1);
23
+ return true;
24
+ }
25
+ return false;
26
+ };
27
+ return xform;
28
+ }
29
+ export {
30
+ intercept
31
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/transducers-async",
3
- "version": "0.2.22",
3
+ "version": "0.3.1",
4
4
  "description": "Async versions of various highly composable transducers, reducers and iterators",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -41,7 +41,7 @@
41
41
  "@thi.ng/checks": "^3.6.13",
42
42
  "@thi.ng/compose": "^3.0.14",
43
43
  "@thi.ng/errors": "^2.5.17",
44
- "@thi.ng/transducers": "^9.2.6"
44
+ "@thi.ng/transducers": "^9.2.7"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@microsoft/api-extractor": "^7.47.9",
@@ -118,6 +118,9 @@
118
118
  "./filter": {
119
119
  "default": "./filter.js"
120
120
  },
121
+ "./intercept": {
122
+ "default": "./intercept.js"
123
+ },
121
124
  "./iterator": {
122
125
  "default": "./iterator.js"
123
126
  },
@@ -205,5 +208,5 @@
205
208
  "status": "alpha",
206
209
  "year": 2018
207
210
  },
208
- "gitHead": "241df2019d93359c34da1a11a94e416b6ea73f32\n"
211
+ "gitHead": "8301254c4436505a1fb0e3cc28eca3adb920c3e0\n"
209
212
  }