-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (95 loc) · 2.77 KB
/
index.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var _ = require('lodash');
var E = require('./_exceptions');
_.true = true;
_.false = !_.true;
_.undefined = undefined;
_.null = null;
_.if = function(boolean, cb){
cb = cb == null ? function(){} : cb;
if(typeof cb === 'function')
return (typeof boolean === 'function' ? boolean() : boolean) ? cb() : (function(){throw new E.FalseIf})();
};
_.else = function(exec, cb){
try {
var data = exec();
return data === undefined ? false : data;
} catch(e){
try {
_.if(e instanceof E.FalseIf, function(){})
return undefined;
} catch(_e){
throw e;
}
}
};
_.switch = function(option, cb){
var cases = [];
var switchedValue = cb == null ? option : null;
var _switch = {
case: addCase,
eval: function(result, _default){
if(switchedValue != null)
result = switchedValue;
var triggered = false;
try {
for(var i = 0; i < cases.length; i++)
if(cases[i][0] === result){
for(var j = i; i < cases.length; ++j)
if(cases[j][1]() === null){
triggered = true;
break;
} else
i = j;
}
if(!triggered || cases.length === 0)
if(_default && typeof _default === 'function')
_default();
else
throw new E.SwitchDefault;
} catch(e) {
if(e instanceof E.SwitchDefault)
throw e;
}
}
};
function addCase(o, c){
if(typeof o === 'function'){
cases.push([undefined, o]);
return _switch;
}
cases.push([o, c]);
return _switch;
}
if(switchedValue == null)
addCase(option, cb);
return _switch;
};
var _get = _.get;
_.get = function(obj, key, _default){
if(key.split('.').length > 1)
throw new E.TooManyDots();
return _get(obj, key, _default);
};
_.try = function(tryFunc){
var types = [];
var exec = function(){
try {
return tryFunc();
} catch(e){
for(var type in types)
if(e instanceof types[type].err)
return types[type].handler(e);
throw e;
}
}
var _catch = {
catch: function(err, handler){
types.push({ err: err, handler: handler});
return _catch;
},
exec: exec
}
return _catch;
}
module.exports = _;
module.exports.E = E;