npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fsm-generator

v1.0.1

Published

A Finite State Machine generator

Downloads

2

Readme

Finite State Machine (FSM) Implementation

This package provides a flexible and type-safe implementation of a Finite State Machine in TypeScript.

Features

  • Type-safe implementation
  • Support for custom output types
  • Easy configuration through a simple object structure
  • Methods for state transitions, output retrieval, and machine reset

Installation

npm install fsm-generator


## Usage

To use this FSM, you need to define your states, alphabet (input symbols), transitions, and outputs.

```typescript
import FiniteStateMachine from 'fsm-generator';

const trafficLightFSM = new FiniteStateMachine({
  states: ['Red', 'Yellow', 'Green'],
  alphabet: ['Next'],
  initialState: 'Red',
  acceptingStates: ['Red', 'Green'],
  transitions: [
    { from: 'Red', input: 'Next', to: 'Green' },
    { from: 'Green', input: 'Next', to: 'Yellow' },
    { from: 'Yellow', input: 'Next', to: 'Red' },
  ],
  outputs: [
    ['Red', 'Stop'],
    ['Yellow', 'Prepare to stop'],
    ['Green', 'Go'],
  ],
});


### Methods

- `transition(currentState: StateName, input: InputSymbol): StateName | null` - Attempts to transition from the given state based on the input. Returns the next state if a valid transition exists, or null if no transition is defined.
- `getOutput(): O | undefined` - Get the output associated with the current state
- `getCurrentState(): StateName` - Get the current state
- `reset(): void` - Reset the machine to its initial state

## How It Works

This FSM implements a Moore machine, where the output depends only on the current state, not on the input.


Key characteristics:
- The FSM maintains its current state internally.
- Transitions are triggered by input symbols, updating the internal state.
- Each state has an associated output.
- The `transition` method updates the internal state and returns the new state.

## Limitations

- The FSM does not support epsilon transitions (transition from one state to another without consuming any input symbol.)
- All states and transitions must be defined upfront
- The machine does not support dynamic addition or removal of states or transitions

## Example

Here's an example of using the FSM for a simple traffic light system:

```typescript
import FiniteStateMachine from "fsm-generator";
// Create and configure the FSM
const trafficLightFSM = new FiniteStateMachine({
    states: ["Red", "Yellow", "Green"],
    alphabet: ["Next"],
    initialState: "Red",
    acceptingStates: ["Red", "Green"],
    transitions: [
        ["Red", "Next", "Green"],
        ["Green", "Next", "Yellow"],
        ["Yellow", "Next", "Red"],
    ],
    outputs: [
        ["Red", "Stop"],
        ["Yellow", "Prepare to stop"],
        ["Green", "Go"],
    ],
});

// Use the FSM
trafficLightFSM.getCurrentState(); // 'Red'
trafficLightFSM.getOutput(); // 'Stop'
trafficLightFSM.transition("Next");
trafficLightFSM.getCurrentState(); // 'Green'
trafficLightFSM.getOutput(); // 'Go'
trafficLightFSM.transition("Next");
trafficLightFSM.getCurrentState(); // 'Yellow'
trafficLightFSM.getOutput(); // 'Prepare to stop'
trafficLightFSM.reset();
trafficLightFSM.getCurrentState(); // 'Red'
// Process a sequence of inputs
const result = trafficLightFSM.process("NextNextNext");
console.log(result);
// {
// accepted: true,
// output: 'Stop',
// finalState: 'Red'
// }


## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

This project is licensed under the MIT License - see the LICENSE file for details.