@thi.ng/transducers-fsm 2.2.49 → 2.2.51

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.
Files changed (3) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/index.js +21 -92
  3. package/package.json +8 -6
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/index.js CHANGED
@@ -1,96 +1,25 @@
1
1
  import { compR } from "@thi.ng/transducers/compr";
2
2
  import { ensureReduced, isReduced } from "@thi.ng/transducers/reduced";
3
- /**
4
- * Finite State Machine transducer. Takes an FSM configuration object and
5
- * returns a transducer, which processes inputs using the provided state handler
6
- * functions, which in turn can return any number of outputs per consumed input.
7
- *
8
- * Before processing the first input, the FSM state is initialized by calling
9
- * the user provided `init()` function, which MUST return a state object with at
10
- * least a `state` key, whose value is used for dynamic (i.e. stateful) dispatch
11
- * during input processing. This state object is passed with each input value to
12
- * the current state handler, which is expected to mutate this object, e.g. to
13
- * cause state changes based on given inputs.
14
- *
15
- * If a state handler needs to "emit" results for downstream processing, it can
16
- * return an array of values. Any such values are passed on (individually, not
17
- * as array) to the next reducer in the chain. If a state handler returns `null`
18
- * or `undefined`, further downstream processing of the current input is
19
- * skipped.
20
- *
21
- * Regardless of return value, if a state handler has caused a state change to
22
- * the configured `terminal` state, processing is terminated (by calling
23
- * [`ensureReduced()`](https://docs.thi.ng/umbrella/transducers/functions/ensureReduced.html))
24
- * and no further inputs will be consumed.
25
- *
26
- * @example
27
- * ```ts
28
- * testFSM = {
29
- * states: {
30
- * skip: (state, x) => {
31
- * if (x < 20) {
32
- * if (++state.count > 5) {
33
- * state.state = "take";
34
- * state.count = 1;
35
- * return [x];
36
- * }
37
- * } else {
38
- * state.state = "done";
39
- * }
40
- * },
41
- * take: (state, x) => {
42
- * if (x < 20) {
43
- * if (++state.count > 5) {
44
- * state.state = "skip";
45
- * state.count = 1;
46
- * } else {
47
- * return [x];
48
- * }
49
- * } else {
50
- * state.state = "done";
51
- * }
52
- * },
53
- * done: () => { },
54
- * },
55
- * terminate: "done",
56
- * init: () => ({ state: "skip", count: 0 })
57
- * }
58
- *
59
- * [...tx.iterator(fsm.fsm(testFSM), tx.range(100))]
60
- * // [ 5, 6, 7, 8, 9, 15, 16, 17, 18, 19 ]
61
- *
62
- * // as part of composed transducers...
63
- *
64
- * [...tx.iterator(
65
- * tx.comp(tx.takeNth(2), fsm.fsm(testFSM)),
66
- * tx.range(100))]
67
- * // [ 10, 12, 14, 16, 18 ]
68
- *
69
- * [...tx.iterator(
70
- * tx.comp(fsm.fsm(testFSM), tx.map((x) => x * 10)),
71
- * tx.range(100))]
72
- * // [ 50, 60, 70, 80, 90, 150, 160, 170, 180, 190 ]
73
- * ```
74
- *
75
- * @param opts -
76
- */
77
- export const fsm = (opts) => (rfn) => {
78
- const states = opts.states;
79
- const state = opts.init();
80
- const r = rfn[2];
81
- return compR(rfn, (acc, x) => {
82
- const res = states[state.state](state, x);
83
- if (res != null) {
84
- for (let i = 0, n = res.length; i < n; i++) {
85
- acc = r(acc, res[i]);
86
- if (isReduced(acc)) {
87
- break;
88
- }
89
- }
3
+ const fsm = (opts) => (rfn) => {
4
+ const states = opts.states;
5
+ const state = opts.init();
6
+ const r = rfn[2];
7
+ return compR(rfn, (acc, x) => {
8
+ const res = states[state.state](state, x);
9
+ if (res != null) {
10
+ for (let i = 0, n = res.length; i < n; i++) {
11
+ acc = r(acc, res[i]);
12
+ if (isReduced(acc)) {
13
+ break;
90
14
  }
91
- if (state.state === opts.terminate) {
92
- return ensureReduced(acc);
93
- }
94
- return acc;
95
- });
15
+ }
16
+ }
17
+ if (state.state === opts.terminate) {
18
+ return ensureReduced(acc);
19
+ }
20
+ return acc;
21
+ });
22
+ };
23
+ export {
24
+ fsm
96
25
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/transducers-fsm",
3
- "version": "2.2.49",
3
+ "version": "2.2.51",
4
4
  "description": "Transducer-based Finite State Machine transformer",
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,12 +35,12 @@
33
35
  "test": "bun test"
34
36
  },
35
37
  "dependencies": {
36
- "@thi.ng/api": "^8.9.10",
37
- "@thi.ng/transducers": "^8.8.13"
38
+ "@thi.ng/api": "^8.9.12",
39
+ "@thi.ng/transducers": "^8.8.15"
38
40
  },
39
41
  "devDependencies": {
40
42
  "@microsoft/api-extractor": "^7.38.3",
41
- "@thi.ng/testament": "^0.4.3",
43
+ "esbuild": "^0.19.8",
42
44
  "rimraf": "^5.0.5",
43
45
  "tools": "^0.0.1",
44
46
  "typedoc": "^0.25.4",
@@ -73,5 +75,5 @@
73
75
  "status": "completed",
74
76
  "year": 2018
75
77
  },
76
- "gitHead": "04d1de79f256d7a53c6b5fd157b37f49bc88e11d\n"
78
+ "gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
77
79
  }