fsm-vir
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

tests

fsm-vir

The heroic finite state machine package. Has zero dependencies! Can be used to quickly create Mealy or Moore finite state machines.

Usage

Install

This package is available via npm:

npm i fsm-vir

Example

import {createStateMachine} from 'fsm-vir';

/** Finite states must be defined. */
enum MyState {
    Start = 'start',
    Middle = 'middle',
    End = 'end',
}

/** Define a state machine like so: */
const myStateMachine = createStateMachine<MyState, string, string>({
    performStateAction: (currentState, input, lastOutput) => {
        if (currentState === MyState.Middle) {
            return `This person likes ${input}.`;
        } else {
            return lastOutput;
        }
    },
    calculateNextState: (currentState, input) => {
        if (currentState === MyState.Start && input.endsWith('likes')) {
            return MyState.Middle;
        } else if (currentState === MyState.Middle) {
            return MyState.End;
        } else {
            return currentState;
        }
    },
    initialState: MyState.Start,
    endState: MyState.End,
});

/** Run the state machine with a set of inputs */
const result = myStateMachine.runMachine([
    'person name',
    'Rando Winston',
    'person hair color',
    'brown',
    'person likes',
    'birthday cake',
    'person eye color',
    'brown',
    'person ear size',
    'small',
]);

console.log(result.output);
// This person likes birthday cake.

Details

For more details, see the type definitions which are well documented.

Package Sidebar

Install

npm i fsm-vir

Weekly Downloads

3

Version

1.0.1

License

MIT

Unpacked Size

22.2 kB

Total Files

19

Last publish

Collaborators

  • electrovir