summaryrefslogtreecommitdiff
path: root/node_modules/@jet/environment/types/optional.js
blob: ea3aaeb1a3c9f0a2b859c0906066081b9c7f8e37 (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
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.flatMapOptional = exports.mapOptional = exports.unsafeUnwrapOptional = exports.unwrapOptional = exports.isSome = exports.isNothing = exports.unsafeUninitialized = void 0;
/**
 * Bypass the protection provided by the `Optional` type
 * and pretend to produce a value of `Some<T>` while
 * actually returning `Nothing`.
 */
function unsafeUninitialized() {
    return undefined;
}
exports.unsafeUninitialized = unsafeUninitialized;
/**
 * Test whether an optional does not contain a value.
 *
 * @param value - An optional value to test.
 */
function isNothing(value) {
    return value === undefined || value === null;
}
exports.isNothing = isNothing;
/**
 * Test whether an optional contains a value.
 * @param value - An optional value to test.
 */
function isSome(value) {
    return value !== undefined && value !== null;
}
exports.isSome = isSome;
/**
 * Unwrap the value contained in a given optional,
 * throwing an error if there is no value.
 *
 * @param value - A value to unwrap.
 */
function unwrapOptional(value) {
    if (isNothing(value)) {
        throw new ReferenceError();
    }
    return value;
}
exports.unwrapOptional = unwrapOptional;
/**
 * Unwrap the value contained in a given optional
 * without checking if the value exists.
 *
 * @param value - A value to unwrap.
 */
function unsafeUnwrapOptional(value) {
    return value;
}
exports.unsafeUnwrapOptional = unsafeUnwrapOptional;
function mapOptional(value, body) {
    if (isSome(value)) {
        return body(value);
    }
    else {
        return value;
    }
}
exports.mapOptional = mapOptional;
function flatMapOptional(value, body) {
    if (isSome(value)) {
        return body(value);
    }
    else {
        return value;
    }
}
exports.flatMapOptional = flatMapOptional;
//# sourceMappingURL=optional.js.map