@j0u1/finity 1.0.4 → 1.0.5
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/README.md +30 -9
- package/dist/index.d.ts +3 -3
- package/dist/index.js +9 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,7 +6,6 @@ Lightweight finite state machine (FSM) library for TypeScript.
|
|
|
6
6
|
[](https://wakatime.com/badge/user/42e0f82b-2688-4a48-85af-5eedd1812f70/project/7cf65f9a-042d-4ed8-ac25-717b8b4ff28b)
|
|
7
7
|
[](https://badge.socket.dev/npm/package/@j0u1/finity)
|
|
8
8
|
|
|
9
|
-
|
|
10
9
|
## Install
|
|
11
10
|
|
|
12
11
|
```bash
|
|
@@ -17,6 +16,8 @@ npm install @j0u1/finity
|
|
|
17
16
|
|
|
18
17
|
## Usage
|
|
19
18
|
|
|
19
|
+
Single transition per state:
|
|
20
|
+
|
|
20
21
|
```ts
|
|
21
22
|
import { createMachine } from "@j0u1/finity"
|
|
22
23
|
|
|
@@ -29,11 +30,27 @@ const traffic = createMachine({
|
|
|
29
30
|
},
|
|
30
31
|
})
|
|
31
32
|
|
|
32
|
-
traffic.current
|
|
33
|
-
traffic.
|
|
34
|
-
traffic.
|
|
35
|
-
traffic.canChangeTo("
|
|
36
|
-
|
|
33
|
+
traffic.current // "red"
|
|
34
|
+
traffic.moveTo("yellow") // "yellow"
|
|
35
|
+
traffic.canChangeTo("green") // true
|
|
36
|
+
traffic.canChangeTo("red") // false
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Multiple transitions per state:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
const order = createMachine({
|
|
43
|
+
initial: "pending",
|
|
44
|
+
transitions: {
|
|
45
|
+
pending: ["success", "fail"],
|
|
46
|
+
success: [],
|
|
47
|
+
fail: [],
|
|
48
|
+
},
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
order.canChangeTo("success") // true
|
|
52
|
+
order.canChangeTo("fail") // true
|
|
53
|
+
order.moveTo("success") // "success"
|
|
37
54
|
```
|
|
38
55
|
|
|
39
56
|
## API
|
|
@@ -45,10 +62,14 @@ Creates a new state machine.
|
|
|
45
62
|
| Parameter | Type | Description |
|
|
46
63
|
|---|---|---|
|
|
47
64
|
| `config.initial` | `string` | Initial state |
|
|
48
|
-
| `config.transitions` | `Record<string, string>` | Map of state transitions |
|
|
65
|
+
| `config.transitions` | `Record<string, string \| string[]>` | Map of state transitions |
|
|
49
66
|
|
|
50
67
|
Returns an object with:
|
|
51
68
|
|
|
52
69
|
- **`current`** — current state
|
|
53
|
-
- **`
|
|
54
|
-
- **`canChangeTo(state)`** — returns `true` if transition to given state is possible from current state
|
|
70
|
+
- **`moveTo(state)`** — transitions to the given state. Throws if transition is not allowed.
|
|
71
|
+
- **`canChangeTo(state)`** — returns `true` if transition to given state is possible from current state
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
|
|
75
|
+
MIT
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
interface ConfigType {
|
|
2
2
|
initial: string;
|
|
3
|
-
transitions: Record<string, string>;
|
|
3
|
+
transitions: Record<string, string | string[]>;
|
|
4
4
|
}
|
|
5
5
|
export declare function createMachine(config: ConfigType): {
|
|
6
6
|
current: string;
|
|
7
|
-
transitions: Record<string, string>;
|
|
8
|
-
|
|
7
|
+
transitions: Record<string, string | string[]>;
|
|
8
|
+
moveTo(state: string): string;
|
|
9
9
|
canChangeTo(state: string): boolean;
|
|
10
10
|
};
|
|
11
11
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -9,14 +9,17 @@ function createMachine(config) {
|
|
|
9
9
|
return {
|
|
10
10
|
current: config.initial,
|
|
11
11
|
transitions: config.transitions,
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
moveTo(state) {
|
|
13
|
+
const available = this.transitions[this.current];
|
|
14
|
+
const states = Array.isArray(available) ? available : [available];
|
|
15
|
+
if (!states.includes(state))
|
|
16
|
+
throw new FinityError(`No transition from "${this.current}" to "${state}"`);
|
|
17
|
+
return this.current = state;
|
|
17
18
|
},
|
|
18
19
|
canChangeTo(state) {
|
|
19
|
-
|
|
20
|
+
const available = this.transitions[this.current];
|
|
21
|
+
const states = Array.isArray(available) ? available : [available];
|
|
22
|
+
return states.includes(state);
|
|
20
23
|
}
|
|
21
24
|
};
|
|
22
25
|
}
|