Skip to content

Commit 3d71da4

Browse files
committed
Fix issue jakesgordon#60 - can() with unexpected event (crashes)
1 parent c17eca7 commit 3d71da4

File tree

3 files changed

+5
-2
lines changed

3 files changed

+5
-2
lines changed

state-machine.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676

7777
fsm.current = 'none';
7878
fsm.is = function(state) { return Array.isArray(state) ? (state.indexOf(this.current) >= 0) : (this.current === state); };
79-
fsm.can = function(event) { return !this.transition && (map[event].hasOwnProperty(this.current) || map[event].hasOwnProperty(StateMachine.WILDCARD)); }
79+
fsm.can = function(event) { return !this.transition && (map[event] !== undefined) && (map[event].hasOwnProperty(this.current) || map[event].hasOwnProperty(StateMachine.WILDCARD)); }
8080
fsm.cannot = function(event) { return !this.can(event); };
8181
fsm.transitions = function() { return (transitions[this.current] || []).concat(transitions[StateMachine.WILDCARD] || []); };
8282
fsm.isFinished = function() { return this.is(terminal); };

state-machine.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test_basics.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ test("can & cannot", function() {
7676
ok(fsm.cannot('panic'), "should NOT be able to panic from red state")
7777
ok(fsm.can('calm'), "should be able to calm from red state")
7878

79+
equal(fsm.can('jibber'), false, "unknown event should not crash")
80+
equal(fsm.cannot('jabber'), true, "unknown event should not crash")
81+
7982
});
8083

8184
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)