New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

state-js

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

state-js

A JavaScript finite state machine with a more natural fashion.

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

StateJS

A JavaScript finite state machine with a more natural fashion.

Installation

Browser
<script src="path/to/state.js"></script>
var myState = StateJS.create(/* ... */);
Node.JS
var State = require('state-js');
var myState = State.create(/* ... */);

.create(config)

Creates and returns a state machine object according to configuration defined in config.

  • config: A JavaScript object literal defining the state machine.

State Machine Config Object

A config object should be in the following form:

{
  name: [{
    entry: 'Hello world', // `entry` specifies state entry conditions
    onEnter: function (newVal, oldVal) {
      // `onEnter` specifies the callback function to be invoked when entering the state.
      // `newVal`: current value.
      // `oldVal`: previous value.
    },
    onLeave: function (newVal, oldVal) {
      // `onLeave` specifies the callback function to be invoked when leaving the state.
      // `newVal`: current value.
      // `oldVal`: previous value.
    }
  }, {
    entry: 'StateJS', // Here is another state.
    onEnter: function () {/* ... */},
    onLeave: function () {/* ... */}
  }],
  version: {
    value: 1, // By wrapping state in an object literal, you can use `value` to define its default/initial value.
    states: [{ // Then use `states` to define states.
      entry: function (val) { return Math.floor(val) === 100 }, // `entry` can also be a function returning true or false. 
      onEnter: function () {/* ... */},
      onLeave: function () {/* ... */}
    }]
  }
}

Change/get states

After create a state machine, you can change/get by:

var state = StateJS(/* ... */);
console.log(state.name); // To get.
state.name = 'StateJS'; // To set/change. May trigger onEnter and onLeave callbacks.

Add/remove states.

You can add states by calling .add(config). To remove states, call .remove([stateName1, stateName2, ...]).

Current Limitation

In current version, entry cannot be either object or array. However, you can compare/manipulate such types by defining entry as a function and then access object/array through the passed argument.

License

MIT

Keywords

state

FAQs

Package last updated on 12 Jan 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts