Lightweight finite state machine (FSM) library for TypeScript.
bun add @j0u1/finity
# or
npm install @j0u1/finitySingle transition per state:
import { createMachine } from "@j0u1/finity"
const traffic = createMachine({
initial: "red",
transitions: {
red: "yellow",
yellow: "green",
green: "yellow",
},
})
traffic.current // "red"
traffic.moveTo("yellow") // "yellow"
traffic.canChangeTo("green") // true
traffic.canChangeTo("red") // falseMultiple transitions per state:
const order = createMachine({
initial: "pending",
transitions: {
pending: ["success", "fail"],
success: [],
fail: [],
},
})
order.canChangeTo("success") // true
order.canChangeTo("fail") // true
order.moveTo("success") // "success"Creates a new state machine.
| Parameter | Type | Description |
|---|---|---|
config.initial |
string |
Initial state |
config.transitions |
Record<string, string | string[]> |
Map of state transitions |
Returns an object with:
current— current statemoveTo(state)— transitions to the given state. Throws if transition is not allowed.canChangeTo(state)— returnstrueif transition to given state is possible from current state
MIT