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.

Revisions

  1. @podefr podefr revised this gist Mar 29, 2012. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -15,4 +15,6 @@ v2: 128 bytes and the actions can be executed in a given context

    v3: 126 bytes with careful reuse of an already declared var -c- in the event function

    v4: 125 bytes by using the assignment c=b[a][c] directly to determine whether or not the event exists
    v4: 125 bytes by using the assignment c=b[a][c] directly to determine whether or not the event exists

    v5: 106 bytes by removing unecessary code! What feature should I add now?
  2. @podefr podefr revised this gist Mar 29, 2012. 2 changed files with 12 additions and 12 deletions.
    22 changes: 11 additions & 11 deletions annotated.js
    Original file line number Diff line number Diff line change
    @@ -13,20 +13,20 @@
    * @returns {Object}
    */
    SM = function(
    a // stores the current state
    ,b // an object to store all states and their transitions
    a // stores the current state
    ,b // an object to store all states and their transitions
    ){
    return{
    event:function( // The function to send an event to the state machine
    c // The name of the event
    ,d // The arguments to pass to the action
    ,e
    ,f
    ){
    return (c=b[a][c]) // Save the array [action, nextState] in c which is carefuly reused,
    &&(e=c[0],f=e[0], // And if c is defined.
    f&&f.call?f.call(e[1],d):e(d), // call the function with the context or call it directly
    a=c[1]||a) // The next state is the new state and the new state is returned
    c // The name of the event
    ,d // The arguments to pass to the action
    ){
    return (c=b[a][c]) // Save the array [action, nextState] in c which is carefuly reused,
    && ( // If c is defined.
    (c[0][0]||c[0]) // Either c[0] is the function, or c[0][0] if a scope is given
    .call(c[0][1],d), // call the function in the context or call it directly
    a=c[1]||a // The next state is the new state and the new state is returned
    )
    }
    }
    }
    2 changes: 1 addition & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    function(a,b){return{event:function(c,d,e,f){return(c=b[a][c])&&(e=c[0],f=e[0],f&&f.call?f.call(e[1],d):e(d),a=c[1]||a)}}}
    function(a,b){return{event:function(c,d){return(c=b[a][c])&&((c[0][0]||c[0]).call(c[0][1],d),a=c[1]||a)}}}
  3. @podefr podefr revised this gist Mar 29, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion annotated.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    * @param {String} a the state on which the state machine will be initialized
    * @param {Object} b the state machine's diagram :
    * { "state1": {
    * "event1": [action1, state2],
    * "event1": [action1, "state2"],
    * "event2": [action2]
    * },
    * "state2": {
  4. @podefr podefr revised this gist Oct 31, 2011. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,12 @@
    State Machine Generator
    Finite State Machine
    =========

    Generates a state machine with the value you pass in parameter
    You can define states, transitions to these states, parameters to the actions, and execute the actions in a given context.
    This code was test driven developed with JsTestDriver, the TestCase is attached
    Generates a state machine with the value you pass in parameter.
    You can define states, transitions to these states, pass parameters to the actions, and execute the actions in a given context.
    This code was test driven developed with JsTestDriver, the TestCase is attached.

    Downsized with the help of UglifyJS on marijn's website : http://marijnhaverbeke.nl/uglifyjs

    The example of the state machine comes from http://www.objectmentor.com/resources/articles/umlfsm.pdf

    v1: only 85 bytes but the actions were ran in the global scope
  5. @podefr podefr revised this gist Oct 28, 2011. 3 changed files with 14 additions and 12 deletions.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -12,4 +12,6 @@ v1: only 85 bytes but the actions were ran in the global scope

    v2: 128 bytes and the actions can be executed in a given context

    v3: 126 bytes with careful reuse of an already declared var -c- in the event function
    v3: 126 bytes with careful reuse of an already declared var -c- in the event function

    v4: 125 bytes by using the assignment c=b[a][c] directly to determine whether or not the event exists
    20 changes: 10 additions & 10 deletions annotated.js
    Original file line number Diff line number Diff line change
    @@ -13,20 +13,20 @@
    * @returns {Object}
    */
    SM = function(
    a // stores the current state
    ,b // an object to store all states and their transitions
    a // stores the current state
    ,b // an object to store all states and their transitions
    ){
    return{
    event:function( // The function to send an event to the state machine
    c // The name of the event
    ,d // The arguments to pass to the action
    event:function( // The function to send an event to the state machine
    c // The name of the event
    ,d // The arguments to pass to the action
    ,e
    ,f
    ){
    c=b[a][c] // Save the array [action, nextState] in c which is carefuly reused
    return c&&(e=c[0],f=e[0], // If c is defined.
    f&&f.call?f.call(e[1],d):e(d), // call the function with the context or call it directly
    a=c[1]||a) // The next state is the new state and the new state is returned
    ){
    return (c=b[a][c]) // Save the array [action, nextState] in c which is carefuly reused,
    &&(e=c[0],f=e[0], // And if c is defined.
    f&&f.call?f.call(e[1],d):e(d), // call the function with the context or call it directly
    a=c[1]||a) // The next state is the new state and the new state is returned
    }
    }
    }
    2 changes: 1 addition & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    SM=function(a,b){return{event:function(c,d,e,f){return c=b[a][c],c&&(e=c[0],f=e[0],f&&f.call?f.call(e[1],d):e(d),a=c[1]||a)}}}
    function(a,b){return{event:function(c,d,e,f){return(c=b[a][c])&&(e=c[0],f=e[0],f&&f.call?f.call(e[1],d):e(d),a=c[1]||a)}}}
  6. @podefr podefr revised this gist Oct 28, 2011. 2 changed files with 7 additions and 7 deletions.
    12 changes: 6 additions & 6 deletions annotated.js
    Original file line number Diff line number Diff line change
    @@ -19,14 +19,14 @@ SM = function(
    return{
    event:function( // The function to send an event to the state machine
    c // The name of the event
    ,d // The arguments to pass to the action
    ,e
    ,d // The arguments to pass to the action
    ,e
    ,f
    ){
    e=b[a][c] // Save the array [action, nextState] in e
    return e&&(f=e[0],c=f[0], // If e is defined, careful reuse of c to save 2 bytes
    c&&c.call?c.call(f[1],d):f(d), // if call the function with the context or call it directly
    a=e[1]||a) // The next state is the new state and the new state is returned
    c=b[a][c] // Save the array [action, nextState] in c which is carefuly reused
    return c&&(e=c[0],f=e[0], // If c is defined.
    f&&f.call?f.call(e[1],d):e(d), // call the function with the context or call it directly
    a=c[1]||a) // The next state is the new state and the new state is returned
    }
    }
    }
    2 changes: 1 addition & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    function(a,b){return{event:function(c,d,e,f){return e=b[a][c],e&&(f=e[0],c=f[0],c&&c.call?c.call(f[1],d):f(d),a=e[1]||a)}}}
    SM=function(a,b){return{event:function(c,d,e,f){return c=b[a][c],c&&(e=c[0],f=e[0],f&&f.call?f.call(e[1],d):e(d),a=c[1]||a)}}}
  7. @podefr podefr revised this gist Oct 28, 2011. 3 changed files with 7 additions and 5 deletions.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -10,4 +10,6 @@ The example of the state machine comes from http://www.objectmentor.com/resource

    v1: only 85 bytes but the actions were ran in the global scope

    v2: 128 bytes and the actions can be executed in a given context
    v2: 128 bytes and the actions can be executed in a given context

    v3: 126 bytes with careful reuse of an already declared var -c- in the event function
    6 changes: 3 additions & 3 deletions annotated.js
    Original file line number Diff line number Diff line change
    @@ -21,11 +21,11 @@ SM = function(
    c // The name of the event
    ,d // The arguments to pass to the action
    ,e
    ,f
    ,f
    ){
    e=b[a][c] // Save the array [action, nextState] in e
    return e&&(f=e[0], // If e is defined
    f[0]&&f[0].call?f[0].call(f[1],d):f(d), // if call the function with the context or call it directly
    return e&&(f=e[0],c=f[0], // If e is defined, careful reuse of c to save 2 bytes
    c&&c.call?c.call(f[1],d):f(d), // if call the function with the context or call it directly
    a=e[1]||a) // The next state is the new state and the new state is returned
    }
    }
    2 changes: 1 addition & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    function(a,b){return{event:function(c,d,e,f){return e=b[a][c],e&&(f=e[0],f[0]&&f[0].call?f[0].call(f[1],d):f(d),a=e[1]||a)}}}
    function(a,b){return{event:function(c,d,e,f){return e=b[a][c],e&&(f=e[0],c=f[0],c&&c.call?c.call(f[1],d):f(d),a=e[1]||a)}}}
  8. @podefr podefr revised this gist Oct 28, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -6,8 +6,8 @@ You can define states, transitions to these states, parameters to the actions, a
    This code was test driven developed with JsTestDriver, the TestCase is attached

    Downsized with the help of UglifyJS on marijn's website : http://marijnhaverbeke.nl/uglifyjs
    The example of the state machine comes from ... (need to find the url)
    The example of the state machine comes from http://www.objectmentor.com/resources/articles/umlfsm.pdf

    v1: only 85 bytes but the actions were ran in the global scope

    v2: 128 bytes but the actions can be executed in a given context
    v2: 128 bytes and the actions can be executed in a given context
  9. @podefr podefr revised this gist Oct 27, 2011. 2 changed files with 4 additions and 4 deletions.
    6 changes: 3 additions & 3 deletions annotated.js
    Original file line number Diff line number Diff line change
    @@ -23,10 +23,10 @@ SM = function(
    ,e
    ,f
    ){
    e=b[a][c]; // Save the array [action, nextState] in e
    if(e)return f=e[0], // If e is defined
    e=b[a][c] // Save the array [action, nextState] in e
    return e&&(f=e[0], // If e is defined
    f[0]&&f[0].call?f[0].call(f[1],d):f(d), // if call the function with the context or call it directly
    a=e[1]||a // The next state is the new state and the new state is returned
    a=e[1]||a) // The next state is the new state and the new state is returned
    }
    }
    }
    2 changes: 1 addition & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    function(a,b){return{event:function(c,d,e,f){e=b[a][c];if(e)return f=e[0],f[0]&&f[0].call?f[0].call(f[1],d):f(d),a=e[1]||a}}}
    function(a,b){return{event:function(c,d,e,f){return e=b[a][c],e&&(f=e[0],f[0]&&f[0].call?f[0].call(f[1],d):f(d),a=e[1]||a)}}}
  10. @podefr podefr revised this gist Oct 27, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion annotated.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    /**
    * Create a finite state machine
    * @param {String} a the state on which the state machine will be initialized
    * @param {Object} the state machine's diagram :
    * @param {Object} b the state machine's diagram :
    * { "state1": {
    * "event1": [action1, state2],
    * "event2": [action2]
  11. @podefr podefr revised this gist Oct 27, 2011. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -9,4 +9,5 @@ Downsized with the help of UglifyJS on marijn's website : http://marijnhaverbeke
    The example of the state machine comes from ... (need to find the url)

    v1: only 85 bytes but the actions were ran in the global scope

    v2: 128 bytes but the actions can be executed in a given context
  12. @podefr podefr revised this gist Oct 27, 2011. 5 changed files with 47 additions and 18 deletions.
    2 changes: 1 addition & 1 deletion LICENSE.txt
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
    Version 2, December 2004

    Copyright (C) 2011 Olivier Scherrer <YOUR_URL_HERE>
    Copyright (C) 2011 Olivier Scherrer

    Everyone is permitted to copy and distribute verbatim or modified
    copies of this license document, and changing it is allowed as long
    5 changes: 3 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -2,10 +2,11 @@ State Machine Generator
    =========

    Generates a state machine with the value you pass in parameter
    You can define states, transitions to these states, and parameters to the actions
    You can define states, transitions to these states, parameters to the actions, and execute the actions in a given context.
    This code was test driven developed with JsTestDriver, the TestCase is attached

    Downsized with the help of UglifyJS on marijn's website : http://marijnhaverbeke.nl/uglifyjs
    The example of the state machine comes from ... (need to find the url)

    As it's only 85 bytes, I could try to pass a context to execute the action in.
    v1: only 85 bytes but the actions were ran in the global scope
    v2: 128 bytes but the actions can be executed in a given context
    25 changes: 21 additions & 4 deletions annotated.js
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,32 @@
    /**
    * Create a finite state machine
    * @param {String} a the state on which the state machine will be initialized
    * @param {Object} the state machine's diagram :
    * { "state1": {
    * "event1": [action1, state2],
    * "event2": [action2]
    * },
    * "state2": {
    * "event3": [[action3, context], "state1"]
    * }
    * }
    * @returns {Object}
    */
    SM = function(
    a // stores the current state
    ,b // an object to store all states and their transitions
    ){
    return{
    ev:function( // The function to send an event to the state machine
    event:function( // The function to send an event to the state machine
    c // The name of the event
    ,d // The arguments to pass to the action
    ,e
    ,e
    ,f
    ){
    return e=b[a][c], // Save the array [action, nextState] in e
    e&&(e[0](d),a=e[1]||a) // If e is defined, execute the action, then assign the nextState to current and return it
    e=b[a][c]; // Save the array [action, nextState] in e
    if(e)return f=e[0], // If e is defined
    f[0]&&f[0].call?f[0].call(f[1],d):f(d), // if call the function with the context or call it directly
    a=e[1]||a // The next state is the new state and the new state is returned
    }
    }
    }
    2 changes: 1 addition & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    function(a,b){return{ev:function(c,d,e){return e=b[a][c],e&&(e[0](d),a=e[1]||a)}}}
    function(a,b){return{event:function(c,d,e,f){e=b[a][c];if(e)return f=e[0],f[0]&&f[0].call?f[0].call(f[1],d):f(d),a=e[1]||a}}}
    31 changes: 21 additions & 10 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    TestCase("SM", {
    "test SM API": function() {
    var sm = SM();
    assertFunction(sm.ev);
    assertFunction(sm.event);
    },

    "test SM transits between 2 states": function() {
    @@ -17,11 +17,11 @@ TestCase("SM", {
    });

    assertUndefined(open.called);
    assertEquals("opened", sm.ev("open!"));
    assertEquals("opened", sm.event("open!"));
    assertTrue(open.called);

    assertUndefined(close.called);
    assertEquals("closed", sm.ev("close!"));
    assertEquals("closed", sm.event("close!"));
    assertTrue(close.called);

    },
    @@ -30,7 +30,7 @@ TestCase("SM", {
    var sm = SM("state", {
    "state": {"event": [function(){}, "nextState"]}
    });
    assertUndefined(sm.ev("crash!"));
    assertUndefined(sm.event("crash!"));
    },

    "test SM can pass arguments to the action": function () {
    @@ -42,11 +42,22 @@ TestCase("SM", {
    arg = {},
    arg2 = {};

    sm.ev("event", arg);
    sm.event("event", arg);
    assertSame(arg, action.arg);
    sm.ev("event", arg2);
    sm.event("event", arg2);
    assertSame(arg2, action.arg);
    },

    "test SM can run actions in given context": function () {
    var action = function() { action.ctx = this; },
    ctx = {},
    sm = SM("state", {
    "state": { "event": [[action, ctx], "nextState"]}});

    sm.event("event");
    assertSame(ctx, action.ctx);

    },

    "test the whole diagram": function() {

    @@ -66,19 +77,19 @@ TestCase("SM", {
    }
    });

    assertEquals("closed", sm.ev("pass", "picture"));
    assertEquals("closed", sm.event("pass", "picture"));
    assertTrue(alarm.called);
    assertEquals("picture", alarm.args[0]);

    assertEquals("opened", sm.ev("coin", "$2"));
    assertEquals("opened", sm.event("coin", "$2"));
    assertTrue(open.called);
    assertEquals("$2", open.args[0]);

    assertEquals("opened", sm.ev("coin", "c50"));
    assertEquals("opened", sm.event("coin", "c50"));
    assertTrue(thank.called);
    assertEquals("c50", thank.args[0]);

    assertEquals("closed", sm.ev("pass"));
    assertEquals("closed", sm.event("pass"));
    assertTrue(close.called);

    }
  13. @podefr podefr revised this gist Oct 25, 2011. 5 changed files with 72 additions and 56 deletions.
    2 changes: 1 addition & 1 deletion LICENSE.txt
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
    Version 2, December 2004

    Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
    Copyright (C) 2011 Olivier Scherrer <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
    9 changes: 6 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,11 @@
    State Machine Generator
    =========

    Generates a state machine.
    You can add transitions and pass events so the machine changes its state and executes actions.
    Documentation is coming.
    Generates a state machine with the value you pass in parameter
    You can define states, transitions to these states, and parameters to the actions
    This code was test driven developed with JsTestDriver, the TestCase is attached

    Downsized with the help of UglifyJS on marijn's website : http://marijnhaverbeke.nl/uglifyjs
    The example of the state machine comes from ... (need to find the url)

    As it's only 85 bytes, I could try to pass a context to execute the action in.
    23 changes: 7 additions & 16 deletions annotated.js
    Original file line number Diff line number Diff line change
    @@ -2,23 +2,14 @@ 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
    a // The name of the state to add
    ,c // The event that will trigger the transition
    ,d // The action to perform
    ,e // The next state is optional
    ){
    (b[a]=b[a]||{})[c]=[d,e] // Add a state c to object b (or assign default {})
    // and use the result to store an array with [ action, nextState ]
    },
    return{
    ev:function( // The function to send an event to the state machine
    c // The name of the event
    ,d
    ){
    return d=b[a][c], // execute the action for the event corresponding in the current state
    d&&(d[0](),a=d[1]||a) // and return the next state that is assigned to the current state (for testing purpose)
    }
    ,d // The arguments to pass to the action
    ,e
    ){
    return e=b[a][c], // Save the array [action, nextState] in e
    e&&(e[0](d),a=e[1]||a) // If e is defined, execute the action, then assign the nextState to current and return it
    }
    }
    }
    2 changes: 1 addition & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    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)}}}
    function(a,b){return{ev:function(c,d,e){return e=b[a][c],e&&(e[0](d),a=e[1]||a)}}}
    92 changes: 57 additions & 35 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -1,61 +1,83 @@
    // 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");

    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.
    "closed":{"open!": [open, "opened"]},
    // The opposite
    "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");
    var sm = SM("state", {
    "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");
    "test SM can pass arguments to the action": function () {
    var action = function(arg) {action.arg = arg; },
    sm = SM("state", {
    "state": {"event": [action, "state2"]},
    "state2": {"event": [action]}
    }),
    arg = {},
    arg2 = {};

    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"));
    sm.ev("event", arg);
    assertSame(arg, action.arg);
    sm.ev("event", arg2);
    assertSame(arg2, action.arg);
    },

    "test the whole diagram": function() {

    var alarm = function () { alarm.called = true; alarm.args = arguments; },
    open = function () { open.called = true; open.args = arguments; },
    thank = function () { thank.called = true; thank.args = arguments; },
    close = function () { close.called = true; close.args = arguments; },

    sm = SM("closed", {
    "closed": {
    "pass": [alarm],
    "coin": [open, "opened"]
    },
    "opened": {
    "coin": [thank],
    "pass": [close, "closed"]
    }
    });

    assertEquals("closed", sm.ev("pass", "picture"));
    assertTrue(alarm.called);

    assertEquals("opened", sm.ev("coin"));
    assertEquals("picture", alarm.args[0]);

    assertEquals("opened", sm.ev("coin", "$2"));
    assertTrue(open.called);

    assertEquals("opened", sm.ev("coin"));
    assertEquals("$2", open.args[0]);

    assertEquals("opened", sm.ev("coin", "c50"));
    assertTrue(thank.called);

    assertEquals("c50", thank.args[0]);

    assertEquals("closed", sm.ev("pass"));
    assertTrue(close.called);

  14. @podefr podefr revised this gist Oct 25, 2011. 2 changed files with 17 additions and 41 deletions.
    34 changes: 5 additions & 29 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,32 +1,8 @@
    140byt.es
    State Machine Generator
    =========

    A tweet-sized, fork-to-play, community-curated collection of JavaScript.
    Generates a state machine.
    You can add transitions and pass events so the machine changes its state and executes actions.
    Documentation is coming.

    How to play
    -----------

    1. Click the ![Fork](https://d3nwyuy0nl342s.cloudfront.net/images/gist/buttons/fork_button.png) 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](http://sam.zoy.org/wtfpl/) or equally permissive license.

    For more information
    --------------------

    See the [140byt.es](http://140byt.es) site for a showcase of entries (built itself using 140-byte entries!), and follow [@140bytes](http://twitter.com/140bytes) on Twitter.

    To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to [the wiki](https://github.com/jed/140bytes/wiki/Byte-saving-techniques).

    140byt.es is brought to you by [Jed Schmidt](http://jed.is), with help from Alex Kloss. It was inspired by work from [Thomas Fuchs](http://mir.aculo.us) and [Dustin Diaz](http://www.dustindiaz.com/).
    Downsized with the help of UglifyJS on marijn's website : http://marijnhaverbeke.nl/uglifyjs
    24 changes: 12 additions & 12 deletions annotated.js
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,24 @@
    SM = function(
    a // stores the current state
    ,b // an object to store all states and their transitions
    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
    a // The name of the state to add
    ,c // The event that will trigger the transition
    ,d // The action to perform
    ,e // 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 ]
    (b[a]=b[a]||{})[c]=[d,e] // 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
    c // The name of the event
    ,d
    ){
    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)
    return d=b[a][c], // execute the action for the event corresponding in the current state
    d&&(d[0](),a=d[1]||a) // and return the next state that is assigned to the current state (for testing purpose)
    }
    }
    }
  15. @podefr podefr revised this gist Oct 25, 2011. 5 changed files with 95 additions and 29 deletions.
    32 changes: 23 additions & 9 deletions annotated.js
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,24 @@
    function(){
    // make sure
    // to annotate
    // your code
    // so everyone
    // can learn
    // from it!
    // see jed's entries
    // for examples.
    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)
    }
    }
    }
    2 changes: 1 addition & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    function(){/* Your entry, a useful, unique, and valid JavaScript expression that packs as much functionality into 140 bytes as possible. */}
    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)}}}
    14 changes: 7 additions & 7 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,13 @@
    {
    "name": "theNameOfYourLibWhichMustBeAValidCamelCasedJavaScriptIdentifier",
    "name": "StateMachineGenerator",

    "description": "This should be a short description of your entry.",
    "description": "A state machine generator",

    "keywords": [
    "five",
    "descriptive",
    "keywords",
    "or",
    "fewer"
    "state",
    "machine",
    "generator",
    "fsm",
    "design pattern"
    ]
    }
    12 changes: 0 additions & 12 deletions test.html
    Original file line number Diff line number Diff line change
    @@ -1,12 +0,0 @@
    <!DOCTYPE html>
    <title>Foo</title>
    <div>Expected value: <b>undefined</b></div>
    <div>Actual value: <b id="ret"></b></div>
    <script>
    // write a small example that shows off the API for your example
    // and tests it in one fell swoop.

    var myFunction = function(){ /* the code here should be identical to the entry. */ }

    document.getElementById( "ret" ).innerHTML = myFunction()
    </script>
    64 changes: 64 additions & 0 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    // 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);

    }

    });
  16. @140bytes 140bytes revised this gist Jul 28, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ All entries must also be licensed under the [WTFPL](http://sam.zoy.org/wtfpl/) o
    For more information
    --------------------

    See the [140byt.es](http://140byt.es) for a showcase of entries built itself using 140-byte entries, and follow [@140bytes](http://twitter.com/140bytes) on Twitter.
    See the [140byt.es](http://140byt.es) site for a showcase of entries (built itself using 140-byte entries!), and follow [@140bytes](http://twitter.com/140bytes) on Twitter.

    To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to [the wiki](https://github.com/jed/140bytes/wiki/Byte-saving-techniques).

  17. @140bytes 140bytes revised this gist Jul 28, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -25,8 +25,8 @@ All entries must also be licensed under the [WTFPL](http://sam.zoy.org/wtfpl/) o
    For more information
    --------------------

    The [140byt.es](http://140byt.es) site hasn't launched yet, but for now follow [@140bytes](http://twitter.com/140bytes) on Twitter.
    See the [140byt.es](http://140byt.es) for a showcase of entries built itself using 140-byte entries, and follow [@140bytes](http://twitter.com/140bytes) on Twitter.

    To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to [the wiki](https://github.com/jed/140bytes/wiki/Byte-saving-techniques).

    140byt.es is brought to you by [Jed Schmidt](http://jed.is). It was inspired by work from [Thomas Fuchs](http://mir.aculo.us) and [Dustin Diaz](http://www.dustindiaz.com/).
    140byt.es is brought to you by [Jed Schmidt](http://jed.is), with help from Alex Kloss. It was inspired by work from [Thomas Fuchs](http://mir.aculo.us) and [Dustin Diaz](http://www.dustindiaz.com/).
  18. @140bytes 140bytes revised this gist Jun 1, 2011. 1 changed file with 5 additions and 7 deletions.
    12 changes: 5 additions & 7 deletions test.html
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,12 @@
    <!DOCTYPE html>
    <body>
    <div>Expected value: <b>undefined</b></div>
    <div>Actual value: <b id="ret"></b></div>
    </body>
    <!DOCTYPE html>
    <title>Foo</title>
    <div>Expected value: <b>undefined</b></div>
    <div>Actual value: <b id="ret"></b></div>
    <script>
    // write a small example that shows off the API for your example
    // and tests it in one fell swoop.

    var myFunction = function(){ /* the code here should be identical to the entry. */ }
    var ret = myFunction()

    document.getElementById( "ret" ).innerHTML = ret
    document.getElementById( "ret" ).innerHTML = myFunction()
    </script>
  19. @140bytes 140bytes revised this gist Jun 1, 2011. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions test.html
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <!DOCTYPE html>
    <body>
    <div>Expected value: <b>undefined</b></div>
    <div>Actual value: <b id="ret"></b></div>
  20. @140bytes 140bytes revised this gist May 31, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion test.html
    Original file line number Diff line number Diff line change
    @@ -9,5 +9,5 @@
    var myFunction = function(){ /* the code here should be identical to the entry. */ }
    var ret = myFunction()

    document.getElementById( "ret" ) = ret
    document.getElementById( "ret" ).innerHTML = ret
    </script>
  21. @140bytes 140bytes revised this gist May 31, 2011. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions test.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    <body>
    <div>Expected value: <b>undefined</b></div>
    <div>Actual value: <b id="ret"></b></div>
    </body>
    <script>
    // write a small example that shows off the API for your example
    // and tests it in one fell swoop.

    var myFunction = function(){ /* the code here should be identical to the entry. */ }
    var ret = myFunction()

    document.getElementById( "ret" ) = ret
    </script>
  22. @140bytes 140bytes revised this gist May 31, 2011. 3 changed files with 17 additions and 19 deletions.
    17 changes: 9 additions & 8 deletions annotated.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,10 @@
    function(){
    /* Rules:
    (1) anonymous function or IIFE // make sure
    (2) may execute immediately // to annotate
    (3) <=140 bytes // your code
    (4) no globals // so everyone
    (5) permissive license // can learn
    (6) have a good time! // from it!
    */}
    // make sure
    // to annotate
    // your code
    // so everyone
    // can learn
    // from it!
    // see jed's entries
    // for examples.
    }
    2 changes: 1 addition & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    function(){/******************************************************************************************************************************/}
    function(){/* Your entry, a useful, unique, and valid JavaScript expression that packs as much functionality into 140 bytes as possible. */}
    17 changes: 7 additions & 10 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,13 @@
    {
    // [REQUIRED] A name for your library.
    // This must match /^[a-z_]\w*$/i
    "name": "140bytes",
    "name": "theNameOfYourLibWhichMustBeAValidCamelCasedJavaScriptIdentifier",

    // [OPTIONAL] A description of your library, phrased as a verb predicate.
    // The gist description is used by default.
    "description": "Explain the 140byt.es rules.",
    "description": "This should be a short description of your entry.",

    // [OPTIONAL] Up to 5 keywords used for indexing.
    "keywords": [
    "140bytes",
    "master",
    "rules"
    "five",
    "descriptive",
    "keywords",
    "or",
    "fewer"
    ]
    }
  23. @140bytes 140bytes revised this gist May 26, 2011. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions annotated.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    function(){
    /* Rules:
    (1) anonymous function // make sure
    (2) may execute immediately // to annotate
    (3) <=140 bytes // your code
    (4) no globals // so everyone
    (5) permissive license // can learn
    (6) have a good time! // from it!
    (1) anonymous function or IIFE // make sure
    (2) may execute immediately // to annotate
    (3) <=140 bytes // your code
    (4) no globals // so everyone
    (5) permissive license // can learn
    (6) have a good time! // from it!
    */}
  24. @140bytes 140bytes revised this gist May 25, 2011. 3 changed files with 8 additions and 8 deletions.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ Rules
    -----
    All entries must exist in an `index.js` file, whose contents are

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

    12 changes: 6 additions & 6 deletions annotated.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    function(){
    /* Rules:
    (1) anonymous function // make sure
    (2) may be self-executing // to annotate
    (3) <=140 bytes // your code
    (4) no globals // so everyone
    (5) MIT license // can learn
    (6) have a good time! // from it!
    (1) anonymous function // make sure
    (2) may execute immediately // to annotate
    (3) <=140 bytes // your code
    (4) no globals // so everyone
    (5) permissive license // can learn
    (6) have a good time! // from it!
    */}
    2 changes: 1 addition & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    function(){/*Rules: (1) anonymous function (2) may be self-executing (3) <=140 bytes (4) no globals (5) MIT license (6) have a good time!*/}
    function(){/******************************************************************************************************************************/}
  25. @140bytes 140bytes revised this gist May 23, 2011. 2 changed files with 14 additions and 21 deletions.
    33 changes: 13 additions & 20 deletions LICENSE.txt
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,13 @@
    Copyright (c) 2011 YOUR_NAME_HERE, YOUR_URL_HERE

    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    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.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ All entries must exist in an `index.js` file, whose contents are
    2. contains no more than 140 bytes, and
    3. does not leak to the global scope.

    All entries must also be licensed under a license as or more permitting than the MIT license.
    All entries must also be licensed under the [WTFPL](http://sam.zoy.org/wtfpl/) or equally permissive license.

    For more information
    --------------------
  26. @140bytes 140bytes revised this gist May 23, 2011. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -16,12 +16,11 @@ Rules
    -----
    All entries must exist in an `index.js` file, whose contents are

    1. a valid Javascript function expression, that
    2. optionally self-executes,
    1. an assignable, valid Javascript expression, that
    2. contains no more than 140 bytes, and
    3. does not pollute global scope.
    3. does not leak to the global scope.

    All entries must also be licensed under the MIT license.
    All entries must also be licensed under a license as or more permitting than the MIT license.

    For more information
    --------------------
  27. @140bytes 140bytes revised this gist May 22, 2011. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,8 @@ How to play
    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
  28. @140bytes 140bytes revised this gist May 21, 2011. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -26,4 +26,6 @@ For more information

    The [140byt.es](http://140byt.es) site hasn't launched yet, but for now follow [@140bytes](http://twitter.com/140bytes) on Twitter.

    To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to [the wiki](https://github.com/jed/140bytes/wiki/Byte-saving-techniques).

    140byt.es is brought to you by [Jed Schmidt](http://jed.is). It was inspired by work from [Thomas Fuchs](http://mir.aculo.us) and [Dustin Diaz](http://www.dustindiaz.com/).
  29. @140bytes 140bytes revised this gist May 18, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion package.json
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@

    // [OPTIONAL] A description of your library, phrased as a verb predicate.
    // The gist description is used by default.
    "description": "Explain the 140byt.es rules."
    "description": "Explain the 140byt.es rules.",

    // [OPTIONAL] Up to 5 keywords used for indexing.
    "keywords": [
  30. @140bytes 140bytes revised this gist May 18, 2011. 1 changed file with 13 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion package.json
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,16 @@
    {
    // [REQUIRED] A name for your library.
    // This must match /^[a-z_]\w*$/i
    "name": "140bytes",
    "keywords": [ "140bytes", "master", "rules" ]

    // [OPTIONAL] A description of your library, phrased as a verb predicate.
    // The gist description is used by default.
    "description": "Explain the 140byt.es rules."

    // [OPTIONAL] Up to 5 keywords used for indexing.
    "keywords": [
    "140bytes",
    "master",
    "rules"
    ]
    }