forked from gcanti/tcomb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.js
34 lines (30 loc) · 934 Bytes
/
match.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
var assert = require('./assert');
var isFunction = require('./isFunction');
var isType = require('./isType');
var Any = require('./Any');
module.exports = function match(x) {
var type, guard, f, count;
for (var i = 1, len = arguments.length; i < len; ) {
type = arguments[i];
guard = arguments[i + 1];
f = arguments[i + 2];
if (isFunction(f) && !isType(f)) {
i = i + 3;
}
else {
f = guard;
guard = Any.is;
i = i + 2;
}
if (process.env.NODE_ENV !== 'production') {
count = (count || 0) + 1;
assert(isType(type), function () { return 'Invalid type in clause #' + count; });
assert(isFunction(guard), function () { return 'Invalid guard in clause #' + count; });
assert(isFunction(f), function () { return 'Invalid block in clause #' + count; });
}
if (type.is(x) && guard(x)) {
return f(x);
}
}
assert.fail('Match error');
};