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

js-fsm-parser

v0.6.2

Published

Parser for js-fsm module

Downloads

14

Readme

js-fsm

Moore Finite State Machine (FSM)

Build Status

js-fsm is a simple javascript Moore Finite State Machine. A Moore machine is a finite-state machine whose output values are determined solely by its current state.

To use js-fsm, define:

  • the starting state,
  • transitions (from state, to state, and inputs)
  • outputs at each state.

Next, set your inputs and call fsm.clock().

Installation

Install with npm

npm install js-fsm

Example

Simple Vending Machine

A vending machine dispenses pieces of candy that cost 20 cents each. The machine accepts nickels and dimes only and does not give change. As soon as the amount deposited equals or exceeds 20 cents, the machine releases a piece of candy. The next coin deposited starts the process over again.

Our state machine has states for the each possible deposited amount: 0 for zero cents deposited, 5 cents, 10 cents, 15 cents, 20 cents and 25 cents.

Defining the FSM

fsm = require( 'js-fsm' )
  initial : '0' # starting balance is zero
  transitions : [
    { from : [ '0', '20' ], to : '5', inputs : 'nickle' }
    { from : [ '0', '20' ], to : '10', inputs : 'dime' }
    { from : [ '5', '25' ], to : '10', inputs : 'nickle' }
    { from : [ '5', '25' ], to : '15', inputs : 'dime' }
    { from : '10', to : '15', inputs : 'nickle' }
    { from : '10', to : '20', inputs : 'dime' }
    { from : '15', to : '20', inputs : 'nickle' }
    { from : '15', to : '25', inputs : 'dime' }
  ]
  outputs :
    '^5,25' : [ 'light5' ]
    '^10' : [ 'light10' ]
    '^15' : [ 'light15' ]
    '^20, 25' : [ 'lightCandy' ]

Operating the vending machine

# from the example

insert 'nickle'
insert 'dime'
insert 'dime'
insert 'dime'
insert 'dime'
insert 'dime'
insert 'nickle'

Output

  coin   |  from   |   to    |    5    |   10    |   15    |  candy
 ------- + ------- + ------- + ------- + ------- + ------- + -------
 #nickle |    0    |    5    |    x    |         |         |
 ------- + ------- + ------- + ------- + ------- + ------- + -------
  #dime  |    5    |   15    |         |         |    x    |
 ------- + ------- + ------- + ------- + ------- + ------- + -------
  #dime  |   15    |   25    |    x    |         |         |    x
 ------- + ------- + ------- + ------- + ------- + ------- + -------
  #dime  |   25    |   15    |         |         |    x    |
 ------- + ------- + ------- + ------- + ------- + ------- + -------
  #dime  |   15    |   25    |    x    |         |         |    x
 ------- + ------- + ------- + ------- + ------- + ------- + -------
  #dime  |   25    |   15    |         |         |    x    |
 ------- + ------- + ------- + ------- + ------- + ------- + -------
 #nickle |   15    |   20    |         |         |         |    x
 ------- + ------- + ------- + ------- + ------- + ------- + -------

API

Create FSM

fsm(options)

options is an {Object}

  • initial is a {String} with the initial state name.

  • transitions is an {Array} of {Object}s that specify the destination state and necessary inputs for each transition

    • from {String} is the current state
    • to {String} is the destination of the transition
    • inputs is an {Array} of signal names and their required state. A transition is possible only if all inputs are true. A signal prefixed with a ! is negated and so is true if it is low (false) and vice-versa.
    • description is an optional {String} which is returned in leave and enter callbacks.
# This transition will occur only when start is true and cancelled is false
{ from: 'initial', to: 'type', inputs: ['start', '!cancelled']
  • outputs {Object} specifies which signals are to be set (and their value) depending on the current state.
# signal 'candy' will go high only in states 20 and 25 and is low everywhere else
# 'FIVE' will go high only in state 25 and is low everywhere else
# Prefixing with an exclamation will set the outputs for all states other than the
# specified ones

outputs :
  '20, 25' : [ 'candy' ]
  '!20, 25' : [ '!candy' ]
  '0, 5, 10, 15, 20' : [ '!FIVE' ]
  '25' : [ 'FIVE' ]

Inverting States

Prefixing with an exclamation ! will set the outputs for all states other than the specified ones.

   # Output 'candy' is set reset in states other than 20 and 25
   '!20, 25': ['!candy']

If-and-only-if Output

Prefixing with a caret ^ sets the state/output combination as if-and-only-if

   # Output 'candy' is set in states 20 and 25, and reset everywhere else
   '^20, 25': ['candy']

Methods

fsm.signal([value])

Gets or sets the value of the named signal (signal must be replaced with the appropriate name above).

#set signal nickle to true and dime to false
fsm
.nickle true
.dime false

fsm.clock()

Instructs the FSM to attempt a transition based on the current input values. If no transition is possible, the fsm will emit a noop event.

# sets input values and and transitions (if possible)
fsm
.dime false
.nickle true
.clock()

fsm.current()

The current state's name

Events

on('noop', cb())

fsm.clock() resulted in no state change.

on('state', cb(state, from, desc))

Fired when the FSM transitions to a state. The callback cb receives the state names state, from and a string description of why the transition occured.

on('error', cb(Error))

Fired if an error occurs. e.g. to many transitions from a state.

on('changed:signal', cb(new, old))

Fired when a signal value changes. Signal values can change either by the user by calling fsm.signal(value), or as a result of entering a new state.

Order of Events

On a state change, events are emitted in the following order:

  • changed:signal for any changed output/signals
  • state