-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoption.js
More file actions
51 lines (44 loc) · 1.16 KB
/
option.js
File metadata and controls
51 lines (44 loc) · 1.16 KB
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
module.exports = (function() {
function Option(val) {
this.val = val;
}
Option.prototype.get = function() {
if(this.val === null || this.val === undefined)
throw new TypeError("None.get");
return this.val
}
Option.prototype.getOrElse = function(x) {
if(this.val === null || this.val === undefined)
return x;
return this.val;
};
Option.prototype.getOrUndefined = function() {
if(this.val === null || this.val === undefined)
return undefined;
return this.val;
};
Option.prototype.map = function(func) {
if(this.val === null || this.val === undefined)
return this;
return new Option(func(this.val));
}
Option.prototype.match = function(case_some, case_none) {
if(this.val === null || this.val === undefined)
return new Option(case_none(this));
return new Option(case_some(this));
}
var option = function(val) {
if(typeof(val) === 'function') {
try {
return new Option(val());
} catch(e) {
return new Option(undefined);
}
}
if(val instanceof Option) {
return Option;
}
return new Option(val);
}
return option;
})();