summaryrefslogtreecommitdiff
path: root/node_modules/@jet/environment/json/reader/coercion.js
blob: 6f84fcf333437d220e1bc1766dfe3fdda582b156 (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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.valueAsNumber = exports.valueAsString = exports.valueAsBoolean = void 0;
const optional_1 = require("../../types/optional");
const validation = require("../validation");
/**
 * Attempt to coerce the given value to a boolean.
 *
 * @see asBoolean
 * @param value - the value to coerce
 * @param policy - determines when validation errors are added to the current validation context
 * @param path - an optional string appended to validation errors to identify where this value originated
 * @returns a boolean if the value was a boolean or coercible to a boolean, otherwise null
 */
function valueAsBoolean(value, policy = "coercible", path) {
    if (!(0, optional_1.isSome)(value)) {
        return value;
    }
    if (typeof value === "boolean") {
        return value;
    }
    // Handle string coercion
    if (typeof value === "string") {
        if (value === "true") {
            return true;
        }
        else if (value === "false") {
            return false;
        }
    }
    // Else coerce.
    const coercedValue = Boolean(value);
    switch (policy) {
        case "strict": {
            validation.context("asBoolean", () => {
                validation.unexpectedType("coercedValue", "boolean", value, path);
            });
            break;
        }
        case "coercible": {
            if ((0, optional_1.isNothing)(coercedValue)) {
                validation.context("asBoolean", () => {
                    validation.unexpectedType("coercedValue", "boolean", value, path);
                });
                return null;
            }
            break;
        }
        case "none":
        default: {
            break;
        }
    }
    return coercedValue;
}
exports.valueAsBoolean = valueAsBoolean;
/**
 * Attempt to coerce the given value to a string.
 *
 * @see asString
 * @param value - the value to coerce
 * @param policy - determines when validation errors are added to the current validation context
 * @param path - an optional string appended to validation errors to identify where this value originated
 * @returns a string if the value was a string or coercible to a string, otherwise null
 */
function valueAsString(value, policy = "coercible", path) {
    if (!(0, optional_1.isSome)(value)) {
        return value;
    }
    if (typeof value === "string") {
        return value;
    }
    // We don't consider arbitrary objects as convertable to strings even through they will result in some value
    const coercedValue = typeof value === "object" ? null : String(value);
    switch (policy) {
        case "strict": {
            validation.context("asString", () => {
                validation.unexpectedType("coercedValue", "string", value, path);
            });
            break;
        }
        case "coercible": {
            if ((0, optional_1.isNothing)(coercedValue)) {
                validation.context("asString", () => {
                    validation.unexpectedType("coercedValue", "string", value, path);
                });
            }
            break;
        }
        case "none":
        default: {
            break;
        }
    }
    return coercedValue;
}
exports.valueAsString = valueAsString;
/**
 * Attempt to coerce the given value to a number.
 *
 * @see asNumber
 * @param value - the value to coerce
 * @param policy - determines when validation errors are added to the current validation context
 * @param path - an optional string appended to validation errors to identify where this value originated
 * @returns a number if the value was a number or coercible to a number, otherwise null
 */
function valueAsNumber(value, policy = "coercible", path) {
    if (!(0, optional_1.isSome)(value)) {
        return value;
    }
    if (typeof value === "number") {
        return value;
    }
    const coercedValue = Number(value);
    switch (policy) {
        case "strict": {
            validation.context("asNumber", () => {
                validation.unexpectedType("coercedValue", "number", value, path);
            });
            break;
        }
        case "coercible": {
            if (isNaN(coercedValue)) {
                validation.context("asNumber", () => {
                    validation.unexpectedType("coercedValue", "number", value, path);
                });
                return null;
            }
            break;
        }
        case "none":
        default: {
            break;
        }
    }
    return coercedValue;
}
exports.valueAsNumber = valueAsNumber;
//# sourceMappingURL=coercion.js.map