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

maschine

v0.0.1

Published

A finite state machine

Downloads

4

Readme

var maschine = require('maschine');
var dfa = new maschine.Maschine();

Define a state by proving a name only. The state will be neither an initial nor a final state.

dfa.addState('A');
dfa.setInitial('A');
dfa.setFinal('A');

Define a state and make it inital and final. This also defines a transition to the state A on receiving the message 'x'

dfa.addState('B', 'initial', 'final', { 'x': 'A' } );

Define a state with multiple transitions.

dfa.addState('C', 'initial', 'final', { 'x': 'A' },  { 'y' : 'C' });

Set multiple final states

dfa.setFinal('A', 'B');

This state has one transition is a function that computes the target state. This is also useful if the messages that this machine is supposed to handle are not strings.

dfa.addState('D', 'initial', 'final', function (message) {
    if (message === 'x') {
	    return 'A';
    }
} );

You can mix regular and computing transitions.

dfa.addState('E', 
    { 'x': 'A' },
    function (message) { return 'B'; },
    { 'y': 'E' }
);

Define a state in object literal notation. Note that you need to give the transitions in an array.

dfa.addState({
    name:    'F',
    initial: true,
    final:   false,
    transitions: [
	    { 'x': 'A' },
	    function (message) { return 'B'; },
	    { 'y': 'E' }
    ]
});

Adding states can be chained:

dfa.addState('A', 'initial', { 'm': 'B' })
   .addState('B', 'final')
   .addState('C', 'final', { 'z' : 'B' });

There is also a full object literal notation for transitions:

dfa.addState('A', {
	name:    'alpha',
	from:    'A',
	message: 'x',
	to:      'B'
});

The properties name and from are optional. If omitted, the name will be defined as A>x>B.

The object literal notation for a computing transition is:

dfa.addState('A', {
	name: 'alpha',
	from: 'A',
	compute: function (message) { }
});

Again, name and from are optional.

Define a state with a control function that governs the entry to the state. If the function returns true, the transition is allowed, otherwise the machine will remain in its current state.

dfa.addState({
    name: 'G',
    transitions: [ { 'x': 'D' } ],
    enterGuards: [ function (message, dfa) {
	    return true;
    } ],
    exitGuards: [ function (message, dfa) {
	    return false
    } ]
});

Define multiple entry guards.

dfa.addState({
    name: 'H',
    enterGuards: [
	    function (message, dfa) {

	    } ,
	    function (message, dfa) {

	    }
    ]
});

Add listeners: After a new state has been entered.

dfa.on('enter', function (message, from, to) { });

After a state has been exited.

dfa.on('exit', function (message, from, to) { });

After exisiting from a state has been denied by a guard.

dfa.on('exitDenied', function (message, from, to) { });

After entering a new state has been denied.

dfa.on('enterDenied', function (message, from, to) { });

After entering a final state.

dfa.on('final', function (message, from, to) { });

Send the machine a message to trigger a state transition.

dfa.message('hello');

Get the current state name:

console.log( dfa.currentState.name );