aboutsummaryrefslogtreecommitdiff
path: root/lib/results.js
blob: d5dd7af9ea1e5e28c52a128dd5a2c8b4876d68c8 (plain)
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
exports.Result = Result;
exports.warning = warning;


function Result(value, messages) {
    this.value = value;
    this.messages = messages || [];
}

Result.prototype.map = function(func) {
    return new Result(func(this.value), this.messages);
};

Result.prototype.flatMapThen = function(func) {
    var messages = this.messages;
    return func(this.value).then(function(otherResult) {
        return new Result(otherResult.value, messages.concat(otherResult.messages));
    });
};

function warning(message) {
    return {
        type: "warning",
        message: message
    };
}