Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
Function.prototype.__actionName = null; | ||
Function.prototype.actionName = function() { return this.__actionName; }; | ||
Function.prototype.isAction = function() { return this.__actionName !== null; }; | ||
|
||
Function.prototype.actionize = function(name, options) { | ||
if (this.isAction()) { | ||
return this; | ||
} else { | ||
return action(name, options || {}, this); | ||
} | ||
}; | ||
|
||
function action(name, options, fun) { | ||
|
||
if (typeof options == 'function') { | ||
fun = options; | ||
options = {}; | ||
} | ||
|
||
var action = function() { | ||
if (arguments.callee.isEnabled()) { | ||
return fun.apply(this, arguments); | ||
} | ||
} | ||
|
||
action.__actionName = name; | ||
action.__actionEnabled = ('enabled' in options) ? options.enabled : true; | ||
|
||
action.isEnabled = function() { return this.__actionEnabled; }; | ||
action.disable = function() { this.__actionEnabled = false; }; | ||
action.enable = function() { this.__actionEnabled = true; }; | ||
action.toggleEnabled = function() { this.__actionEnabled = !this.__actionEnabled; }; | ||
|
||
return action; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
load("../test-unit.js"); | ||
load("../../src/actions.js"); | ||
|
||
test({ | ||
|
||
"action should be function": function() { | ||
assertEqual('function', typeof action('foo', function() {})); | ||
}, | ||
|
||
"action should be invoked as function": function() { | ||
assertEqual(10, action('bar', function(a, b) { return a + b; })(6, 4)); | ||
}, | ||
|
||
"vanilla function should not be action": function() { | ||
assert(!(function() {}).isAction()); | ||
}, | ||
|
||
"action should know it is action": function() { | ||
assert(action('bleem', function() {}).isAction()); | ||
}, | ||
|
||
"action should be enabled by default": function() { | ||
assert(action('foo', function() {}).isEnabled()); | ||
}, | ||
|
||
"action should respect enabled setting": function() { | ||
var a = action('a', {enabled: false}, function() {}); | ||
assert(!a.isEnabled()); | ||
var b = action('b', {enabled: true}, function() {}); | ||
assert(b.isEnabled()); | ||
}, | ||
|
||
"action's enabled setting should be toggleable": function() { | ||
var f = action('f', function() {}); | ||
f.toggleEnabled(); | ||
assert(!f.isEnabled()); | ||
f.toggleEnabled(); | ||
assert(f.isEnabled()); | ||
}, | ||
|
||
"action should do nothing when disabled": function() { | ||
|
||
var foo = 0; | ||
var inc = action('inc', function() { foo++; }); | ||
|
||
inc(); | ||
assertEqual(1, foo); | ||
|
||
inc.disable(); | ||
inc(); | ||
assertEqual(1, foo); | ||
|
||
inc.toggleEnabled(); | ||
inc(); | ||
assertEqual(2, foo); | ||
|
||
}, | ||
|
||
"actionize should change functions into actions": function() { | ||
|
||
var fun = function() {}; | ||
var act = fun.actionize('foof'); | ||
|
||
assert(act.isAction()); | ||
assertEqual('foof', act.actionName()); | ||
|
||
}, | ||
|
||
"actionize should leave existing actions untouched": function() { | ||
var act = action('boof', function() {}); | ||
assertEqual(act, act.actionize()); | ||
} | ||
|
||
}); |