@j0u1/finity 1.0.3 → 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 CHANGED
@@ -1,9 +1,10 @@
1
1
  # @j0u1/finity
2
2
 
3
+ Lightweight finite state machine (FSM) library for TypeScript.
4
+
3
5
  [![npm](https://img.shields.io/npm/v/@j0u1/finity)](https://www.npmjs.com/package/@j0u1/finity)
4
6
  [![wakatime](https://wakatime.com/badge/user/42e0f82b-2688-4a48-85af-5eedd1812f70/project/7cf65f9a-042d-4ed8-ac25-717b8b4ff28b.svg)](https://wakatime.com/badge/user/42e0f82b-2688-4a48-85af-5eedd1812f70/project/7cf65f9a-042d-4ed8-ac25-717b8b4ff28b)
5
-
6
- Lightweight finite state machine (FSM) library for TypeScript.
7
+ [![Socket Badge](https://badge.socket.dev/npm/package/@j0u1/finity)](https://badge.socket.dev/npm/package/@j0u1/finity)
7
8
 
8
9
  ## Install
9
10
 
@@ -15,6 +16,8 @@ npm install @j0u1/finity
15
16
 
16
17
  ## Usage
17
18
 
19
+ Single transition per state:
20
+
18
21
  ```ts
19
22
  import { createMachine } from "@j0u1/finity"
20
23
 
@@ -27,11 +30,27 @@ const traffic = createMachine({
27
30
  },
28
31
  })
29
32
 
30
- traffic.current // "red"
31
- traffic.next() // "yellow"
32
- traffic.current // "yellow"
33
- traffic.canChangeTo("green") // true
34
- traffic.canChangeTo("red") // false
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"
35
54
  ```
36
55
 
37
56
  ## API
@@ -43,10 +62,14 @@ Creates a new state machine.
43
62
  | Parameter | Type | Description |
44
63
  |---|---|---|
45
64
  | `config.initial` | `string` | Initial state |
46
- | `config.transitions` | `Record<string, string>` | Map of state transitions |
65
+ | `config.transitions` | `Record<string, string \| string[]>` | Map of state transitions |
47
66
 
48
67
  Returns an object with:
49
68
 
50
69
  - **`current`** — current state
51
- - **`next()`** — transitions to the next state, returns new state. Throws if no transition exists.
52
- - **`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
- next(): string;
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
- next() {
13
- const nextState = this.transitions[this.current];
14
- if (!nextState)
15
- throw new FinityError(`No transition from "${this.current}"`);
16
- return this.current = nextState;
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
- return this.transitions[this.current] === state;
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
  }
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "@j0u1/finity",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": " Lightweight finite state machine library for TypeScript",
5
5
  "author": "j0u1",
6
6
  "keywords": [
7
7
  "fsm",
8
- "state-machine"
8
+ "state-machine",
9
+ "finite-state-machine",
10
+ "fsm-library"
9
11
  ],
10
12
  "files": [
11
13
  "dist"