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 🙏

© 2026 – Pkg Stats / Ryan Hefner

mix-machine

v1.1.2

Published

A simple state machine mixin.

Readme

mixMachine.js

A simple state machine mixin implementation. To use, simple pass an object through stateMachine().

Example: Creaing a statemachine from an empty object:

var mix = require("mixMachine"); var myMachine = mix.stateMachine({});

Note: the {} are actually optional in this case because stateMachine will make it's own empty object if no object is provided. The object is accepted because you can pass anything in! stateMachine() will turn whatever object you you provide into a stateMachine by giving it three functions:

###addState(identifier, action, transition)

Adds a state to the machine. Action and transition must be functions and all three arguments are required. Whenever the state machine is run, action will be performed, followed by transition. Anything returned by action will be returned but if the return value of transition matches any state's identifier that state will be run next time runState is called.

###setState(identifier)

Manually sets the state which will be used next time runState is called. Note, this must be called once before the machine is run for the first time.

###runState()

Runs the function associated with the current state and then possibly transitions. runState() will return anything returned by the current state's action function, pass any arguments passed to it on to the action and transition functions, and binds the state machine to this for their execution.

##Examples

// Create a new State Machine
var waveMaker = require('mixMachine').stateMachine();
waveMaker.value = 0;

// Add increasing and decreasing states.
waveMaker.addState("increasing", 
                   function(){ ++this.value; },
                   function(){
                     if (this.value >= 100) {
                       return "decreasing";
                     }
                     return "no-transition";
                   });

waveMaker.addState("decreasing", 
                   function(){ --this.value; },
                   function(){
                     if (this.value <= -100) {
                       return "increasing";
                     }
                     return "no-transition";
                   });

// Start the machine in the increasing state.
waveMaker.setState("increasing");

// Run it a few times.
for (var i=0; i < 1000; ++i) {
    console.log(waveMaker.value);
    waveMaker.runState();
}

Generator

Generator inherits from stateMachine but changes the way transitions work. Instead of supplying a transition function you supply either an array or map of states to transition to. If you supply an array the state will randomly transition to one of the provided states after being run. If you supply a map the same thing happens but the keys are the states to transition to and the values are weights. For instance, if you pass a map {'foo':2, 'bar':1} for a state, the next state after it will be foo two thirds of the time and bar the other third.

Example:

var arrGen = mix.Generator();
var heads = 0, tails = 0;

arrGen.addState("heads", function(){
    ++heads;
}, ['heads','tails']);

arrGen.addState("tails", function(){
    ++tails;
}, ['heads', 'tails']);

arrGen.setState('heads');

for (var i=0; i<1000; ++i) {
    arrGen.runState();
}

console.log("Array Generator test:");
console.log("Heads: " +heads+ " Tails: " + tails);