-
Notifications
You must be signed in to change notification settings - Fork 1
/
fab.method.js
50 lines (39 loc) · 1.19 KB
/
fab.method.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
exports.summary = "Takes one more more method names, and returns a ternary app that passes the request to the first app when the request method matches, and to the second app otherwise.";
method = exports.app = function( names ) {
var
methods = {},
len = arguments.length;
for ( var i = 0; i < len; i++ ) {
methods[ arguments[ i ] ] = true;
}
return function( hit ) {
return function( miss ) {
return function() {
var out = this;
return function( head ) {
var app = head.method in methods ? hit : miss;
app = app.call( out );
if ( app ) app = app( head );
return app;
}
}
}
}
}
exports.tests = ( function() {
var ok = function(){ this({ body: true }) }
, notok = function(){ this({ body: false }) }
, app = method( "GET" )( ok )( notok );
return [
function
methodMatchesGetCorrectly() {
var out = this;
app.call( function( obj ){ out( obj.body ) } )({ method: "GET" });
},
function
methodMismatchesPostCorrectly() {
var out = this;
app.call( function( obj ){ out( !obj.body ) } )({ method: "POST" });
}
];
})();