-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
43 lines (35 loc) · 887 Bytes
/
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
var Union = (function () {
function Union(kinds) {
var self = this;
kinds.forEach(function (kind) {
self[kind] = function () {
return new Instance(kind, Array.from(arguments));
};
});
}
function Instance(kind, data) {
this.kind = kind;
this.data = data;
}
Union.fromJSON = function (jsonObject) {
return new Instance(jsonObject.kind, jsonObject.data);
};
Instance.prototype.match = function (clauses) {
if (this.kind in clauses) {
return clauses[this.kind].apply(undefined, this.data);
} else if (clauses.hasOwnProperty('_')) {
return clauses._();
} else {
throw new Error('Case not matched: ' + this.kind);
}
};
Instance.prototype.toString = function () {
return this.kind + '(' + this.data.join(', ') + ')';
};
return Union;
})();
if (typeof module !== 'undefined') {
module.exports = Union;
} else {
window.Union = Union;
}