@desicochrane/machine

[![Coverage Status](https://coveralls.io/repos/github/desicochrane/machine/badge.svg?branch=master&q=1)](https://coveralls.io/github/desicochrane/machine?branch=master)

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
215May 24, 2021Aug 21, 2018Minified + gzip package size for @desicochrane/machine in KB

Readme

Machine

Coverage Status

Tiny dependency-free state-machine implementation in javascript.

Get Started

Basic:

  1. npm install @desicochrane/machine

  2. Define a new state machine specification

    import Machine, { Transition } from '@desicochrane/machine'
    
    const spec = {
        __start__: 'off',
        off: {
            click: Transition('on'),
        },
        on: {
            click: Transition('off'),
        }
    }
    
  3. Instantiate your machine with data:

    const m = Machine(spec)
    
  4. Use the machine

    console.log(m.state) // "off"
    
    m.click()
    
    console.log(m.state) // "on"
    
  5. Export to Dot file:

    import { Dot } from  '@desicochrane/machine'
    import fs from 'fs'
    
    const dot = Dot(spec)
    
    fs.writeFileSync('machine.dot', dot);
    
  6. Optionally pass in data

    import Machine, { Transition } from '@desicochrane/machine'
    
    const spec = {
        __start__: 'off',
        off: {
            click: Transition('on', (m, args) => {
                m.data.count += args
            }),
        },
        on: {
            click: Transition('off', (m, args) => {
                m.data.count -= args
            }),
        }
    }
    
    
    const m = Machine(spec, { count: 0 })
    
    m.click(3)
    console.log(m.data.count) // 3
    
    m.click(2)
    console.log(m.data.count) // 1
    

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.