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

green-fsm

v0.0.4

Published

Finite state machines for JavaScript

Downloads

18

Readme

green-fsm

This is a small finite state machine library which I built mainly for my own use.

Installation

npm install green-fsm

Example

import { fsm } from 'green-fsm'

const a = fsm(
  ['a', 'b'],   // alphabet
  ['0', '1'],   // states
  '0',          // initial state
  ['1'],        // final states
  {0: {a: '1'}} // transition map
)

console.log(a.accepts([]))         // false
console.log(a.accepts(['a']))      // true
console.log(a.accepts(['b']))      // false
console.log(a.accepts(['a', 'a'])) // false
console.log(a.accepts(['c']))      // throws exception

API

green-fsm exposes the following properties and functions. These are a little up in the air right now.

fsm(alphabet, states, finals, map)

Build a finite state machine according to the supplied parameters. Symbols in the alphabet and states are used as keys in Objects, so they should be either Strings or Symbols. states[0] is the initial state.

map may be sparse. If a transition is missing from map, then it is assumed that this transition leads to an undocumented "oblivion state" which is not final. This oblivion state does not appear when the FSM is printed out.

The resulting object has some properties and methods on it.

alphabet

states

finals

map

These are just the properties which were originally passed in.

follow(state, symbol)

Use this for preference over just looking up transitions in the map - it handles sparse transitions and suchlike correctly.

hasFinalState(state)

Use this to determine whether a given state is final in this FSM.

accepts(input)

input should be an array of symbols chosen from this FSM's alphabet. Returns a Boolean indicating whether the input is accepted.

toString()

Pretty-prints this FSM's structure.

strings()

Returns an object conforming to the iterator protocol. This means it has a single property, next, which is a function which can be called repeatedly. At first, calling next will return results of the form {value, done: false} where value is an accepted input of the FSM i.e. an array of symbols. If the FSM is finite, eventally results will take the form {done: true}.

ANYTHING_ELSE

Ordinarily, you may only feed known alphabet symbols into the FSM. Any other symbol will result in an exception being thrown. However, if you add the special Symbol ANYTHING_ELSE to your alphabet, then any unrecognised symbol will be automatically converted into ANYTHING_ELSE before following whatever transition you have specified for this symbol.

crawl(alphabet, initial, final, follow)

Crawl what is assumed to be an FSM and return a new finite state machine object representing it. Starts at state initial. At any given state, crawl calls final(state) to determine whether it is final. Then, for each symbol in alphabet, it calls follow(state, symbol) to try to discover new states. Obviously this procedure could go on for ever if your implementation of follow is faulty.

OBLIVION_STATE

Your implementation of follow (above) may also return the special Symbol OBLIVION_STATE to indicate that you have reached an inescapable, non-final "oblivion state". This state and transitions to it will be omitted from the resulting FSM.

nothing(alphabet)

Returns an FSM over the supplied alphabet which accepts no inputs at all.

epsilon(alphabet)

Returns an FSM over the supplied alphabet which accepts only the empty input, [].

union(fsms)

Returns an FSM accepting all inputs accepted by any of the supplied input FSMs.

intersection(fsms)

Returns an FSM accepting all inputs accepted by all of the supplied input FSMs.

concatenate(fsms)

Returns an FSM accepting any input a·b·... where a is an input accepted by the first FSM, b is an input accepted by the second FSM, and so on.

multiply(fsm, multiplier)

Returns an FSM equivalent to the concatenation of multiplier instances of the original FSM.

star(fsm)

Kleene star closure. Turns an FSM accepting only ['a'] into one accepting any of [], ['a'], ['a', 'a'], ...