Skip to content

Latest commit

 

History

History

fsm

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

@thi.ng/fsm

npm version npm downloads Mastodon Follow

Important

This package has been deprecated and superseded by @thi.ng/parse.

About

Composable primitives for building declarative, transducer based Finite-State Machines & matchers for arbitrary data streams.

Status

DEPRECATED - superseded by other package(s)

Search or submit any issues for this package

Related packages

Installation

yarn add @thi.ng/fsm

ESM import:

import * as fsm from "@thi.ng/fsm";

Browser ESM import:

<script type="module" src="https://esm.run/@thi.ng/fsm"></script>

JSDelivr documentation

For Node.js REPL:

const fsm = await import("@thi.ng/fsm");

Package sizes (brotli'd, pre-treeshake): ESM: 1.31 KB

Dependencies

API

Generated API docs

There're two key concepts provided by this package:

Matchers

Matchers are composable functions which receive a single input value and attempt to match it to their configured criteria / patterns. Matchers also support optional user callbacks, which are executed when a match was made and are responsible for state transitions, state update and production of any result values.

See docs strings in /src folder for now.

FSM transducer

The fsm() function is a Finite-state machine transducer / iterator with support for single lookahead values. Takes an object of states and their matchers, an arbitrary context object and an initial state ID.

The returned transducer consumes inputs of type T and produces results of type R. The results are produced by callbacks of the given state matchers. Each can produce any number of values. If a callback returns a result wrapped w/ reduced(), the FSM causes early termination of the overall transducer pipeline. Failed state callbacks too can produce outputs, but will afterwards terminate the FSM.

An IllegalStateError will be thrown if a transition to an undefined state ID occurs.

The optional update function will be invoked for each input prior to executing the currently active state matcher. It is intended to update the context object (e.g. to update input location info for generating error messages).

If the optional src iterable is given, the function returns a transforming iterator of the FSM results.

Authors

If this project contributes to an academic publication, please cite it as:

@misc{thing-fsm,
  title = "@thi.ng/fsm",
  author = "Karsten Schmidt",
  note = "https://thi.ng/fsm",
  year = 2018
}

License

© 2018 - 2024 Karsten Schmidt // Apache License 2.0