Skip to content

Instantly share code, notes, and snippets.

@DmitryMyadzelets
Forked from podefr/LICENSE.txt
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save DmitryMyadzelets/c4619f96fb29e5b4a22f to your computer and use it in GitHub Desktop.

Select an option

Save DmitryMyadzelets/c4619f96fb29e5b4a22f to your computer and use it in GitHub Desktop.

140byt.es

A tweet-sized, fork-to-play, community-curated collection of JavaScript.

How to play

  1. Click the Fork button above to fork this gist.
  2. Modify all the files to according to the rules below.
  3. Save your entry and tweet it up!

Keep in mind that thanks to the awesome sensibilities of the GitHub team, gists are just repos. So feel free to clone yours and work locally for a more comfortable environment, and to allow commit messages.

Rules

All entries must exist in an index.js file, whose contents are

  1. an assignable, valid Javascript expression that
  2. contains no more than 140 bytes, and
  3. does not leak to the global scope.

All entries must also be licensed under the WTFPL or equally permissive license.

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

SM = function(
a // stores the current state
,b // an object to store all states and their transitions
){
return b={} // Initializes b to an empty object
,{
add:function( // The function to add a state/transition
c // The name of the state to add
,d // The event that will trigger the transition
,e // The action to perform
,f // The next state is optional
){
(b[c]=b[c]||{})[d]=[e,f] // Add a state c to object b (or assign default {})
// and use the result to store an array with [ action, nextState ]
},
ev:function( // The function to send an event to the state machine
c // The name of the event
,g
){
return g=b[a][c], // execute the action for the event corresponding in the current state
g&&(g[0](),a=g[1]||a) // and return the next state that is assigned to the current state (for testing purpose)
}
}
}
function(a,b){return b={},{add:function(a,c,d,e){(b[a]=b[a]||{})[c]=[d,e]},ev:function(c,d){return d=b[a][c],d&&(d[0](),a=d[1]||a)}}}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "StateMachineGenerator",
"description": "A state machine generator",
"keywords": [
"state",
"machine",
"generator",
"fsm",
"design pattern"
]
}
// JsTestDriver TestCase
TestCase("SM", {
"test SM API": function() {
var sm = SM();
assertFunction(sm.add);
assertFunction(sm.ev);
},
"test SM transits between 2 states": function() {
var open=function(){open.called=true},
close=function(){close.called=true},
sm = SM("closed");
// When in closed state, if the event is "open!" then execute the open action and go to opened state.
sm.add("closed", "open!", open, "opened");
// the opposite
sm.add("opened", "close!", close, "closed");
assertUndefined(open.called);
assertEquals("opened", sm.ev("open!"));
assertTrue(open.called);
assertUndefined(close.called);
assertEquals("closed", sm.ev("close!"));
assertTrue(close.called);
},
"test SM doesn't crash if wrong event in given state": function() {
var sm = SM("state");
sm.add("state", "event", function(){}, "nextState");
assertUndefined(sm.ev("crash!"));
},
"test the whole diagram": function() {
var alarm = function () { alarm.called = true },
open = function () { open.called = true },
thank = function () { thank.called = true},
close = function () { close.called = true},
sm = SM("closed");
sm.add("closed", "pass", alarm);
sm.add("closed", "coin", open, "opened");
sm.add("opened", "coin", thank);
sm.add("opened", "pass", close, "closed");
assertEquals("closed", sm.ev("pass"));
assertTrue(alarm.called);
assertEquals("opened", sm.ev("coin"));
assertTrue(open.called);
assertEquals("opened", sm.ev("coin"));
assertTrue(thank.called);
assertEquals("closed", sm.ev("pass"));
assertTrue(close.called);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment