@zthun/trilean 2.0.18 → 2.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +100 -126
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +99 -122
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -1,128 +1,102 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* The indeterminate type.
|
|
7
|
-
*
|
|
8
|
-
* Note that the indeterminate type is officially a string
|
|
9
|
-
* here instead of null. This is by design since null
|
|
10
|
-
* is considered to have no value.
|
|
11
|
-
*
|
|
12
|
-
* Instead of checking for the indeterminate string, always
|
|
13
|
-
* make sure to use ZTrilean.isIndeterminate(val) to check
|
|
14
|
-
* for this type.
|
|
15
|
-
*
|
|
16
|
-
* When setting the indeterminate value, use
|
|
17
|
-
* ZTrilean.Indeterminate as the value to set.
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
*
|
|
21
|
-
* ```ts
|
|
22
|
-
* const value: indeterminate = ZTrilean.Indeterminate;
|
|
23
|
-
* ```
|
|
24
|
-
*/ function _define_property(obj, key, value) {
|
|
25
|
-
if (key in obj) {
|
|
26
|
-
Object.defineProperty(obj, key, {
|
|
27
|
-
value: value,
|
|
28
|
-
enumerable: true,
|
|
29
|
-
configurable: true,
|
|
30
|
-
writable: true
|
|
31
|
-
});
|
|
32
|
-
} else {
|
|
33
|
-
obj[key] = value;
|
|
34
|
-
}
|
|
35
|
-
return obj;
|
|
36
|
-
}
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/trilean.mts
|
|
37
3
|
/**
|
|
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
|
-
if
|
|
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
|
-
|
|
4
|
+
* The indeterminate type.
|
|
5
|
+
*
|
|
6
|
+
* Note that the indeterminate type is officially a string
|
|
7
|
+
* here instead of null. This is by design since null
|
|
8
|
+
* is considered to have no value.
|
|
9
|
+
*
|
|
10
|
+
* Instead of checking for the indeterminate string, always
|
|
11
|
+
* make sure to use ZTrilean.isIndeterminate(val) to check
|
|
12
|
+
* for this type.
|
|
13
|
+
*
|
|
14
|
+
* When setting the indeterminate value, use
|
|
15
|
+
* ZTrilean.Indeterminate as the value to set.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* const value: indeterminate = ZTrilean.Indeterminate;
|
|
21
|
+
* ```
|
|
22
|
+
*/ /**
|
|
23
|
+
* A utility class for trilean values.
|
|
24
|
+
*/ var ZTrilean = class ZTrilean {
|
|
25
|
+
/**
|
|
26
|
+
* A constant value that represents an indeterminate value.
|
|
27
|
+
*/ static Indeterminate = "indeterminate";
|
|
28
|
+
/**
|
|
29
|
+
* Converts a trilean value to a string.
|
|
30
|
+
*
|
|
31
|
+
* @param x -
|
|
32
|
+
* The value to convert.
|
|
33
|
+
*
|
|
34
|
+
* @returns
|
|
35
|
+
* A string of true if x is true, false if x is false,
|
|
36
|
+
* and indeterminate if x is the indeterminate symbol.
|
|
37
|
+
*/ static stringify(x) {
|
|
38
|
+
return String(x);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Converts a string to a trilean value.
|
|
42
|
+
*
|
|
43
|
+
* @param str -
|
|
44
|
+
* The text to parse.
|
|
45
|
+
* @param fallback -
|
|
46
|
+
* The fallback in the case that str is not a parsable value.
|
|
47
|
+
*
|
|
48
|
+
* @returns
|
|
49
|
+
* The parsed trilean value or fallback if str is not a valid
|
|
50
|
+
* trilean value.
|
|
51
|
+
*/ static parse(str, fallback = false) {
|
|
52
|
+
if (str?.toUpperCase().localeCompare("TRUE") === 0) return true;
|
|
53
|
+
if (str?.toUpperCase().localeCompare("FALSE") === 0) return false;
|
|
54
|
+
if (str?.toUpperCase().localeCompare("INDETERMINATE") === 0) return ZTrilean.Indeterminate;
|
|
55
|
+
return fallback;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Gets a value that determines if val is a trilean supported value.
|
|
59
|
+
*
|
|
60
|
+
* @param val -
|
|
61
|
+
* The value to test.
|
|
62
|
+
*
|
|
63
|
+
* @returns
|
|
64
|
+
* True if val is a trilean value. False otherwise.
|
|
65
|
+
*/ static is(val) {
|
|
66
|
+
return typeof val === "boolean" || ZTrilean.isIndeterminate(val);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Gets whether val is the indeterminate value.
|
|
70
|
+
*/ static isIndeterminate(val) {
|
|
71
|
+
return val === ZTrilean.Indeterminate;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Converts from a value to a trilean value.
|
|
75
|
+
*
|
|
76
|
+
* This is similar to parse, but it also supports non
|
|
77
|
+
* string inputs which will be converted to a boolean.
|
|
78
|
+
*
|
|
79
|
+
* @param val -
|
|
80
|
+
* The value to convert.
|
|
81
|
+
* @param fallback -
|
|
82
|
+
* The fallback value in the case that val cannot be converted. This will
|
|
83
|
+
* only be used in the case that val is a string, null, or undefined.
|
|
84
|
+
*
|
|
85
|
+
* @returns
|
|
86
|
+
* The trilean value of val. If val is null or undefined, then
|
|
87
|
+
* fallback is returned. If val is a string, then the return value of
|
|
88
|
+
* {@link parse} will be used. If val is a boolean, then it will be
|
|
89
|
+
* directly returned. If val is equal to {@link Indeterminate}, then
|
|
90
|
+
* {@link Indeterminate} will be returned. Otherwise, the truthy
|
|
91
|
+
* nature of value will be returned.
|
|
92
|
+
*/ static convert(val, fallback = false) {
|
|
93
|
+
if (val == null) return fallback;
|
|
94
|
+
if (ZTrilean.is(val)) return val;
|
|
95
|
+
if (typeof val === "string") return ZTrilean.parse(val, fallback);
|
|
96
|
+
return !!val;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
//#endregion
|
|
127
100
|
exports.ZTrilean = ZTrilean;
|
|
128
|
-
|
|
101
|
+
|
|
102
|
+
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/trilean.mts"],"sourcesContent":["/**\n * The indeterminate type.\n *\n * Note that the indeterminate type is officially a string\n * here instead of null. This is by design since null\n * is considered to have no value.\n *\n * Instead of checking for the indeterminate string, always\n * make sure to use ZTrilean.isIndeterminate(val) to check\n * for this type.\n *\n * When setting the indeterminate value, use\n * ZTrilean.Indeterminate as the value to set.\n *\n * @example\n *\n * ```ts\n * const value: indeterminate = ZTrilean.Indeterminate;\n * ```\n */\nexport type indeterminate = \"indeterminate\";\n\n/**\n * Represents a tri logic state.\n *\n * See {@link indeterminate} for what the symbol value should be.\n */\nexport type trilean = boolean | indeterminate;\n\n/**\n * A utility class for trilean values.\n */\nexport abstract class ZTrilean {\n /**\n * A constant value that represents an indeterminate value.\n */\n public static readonly Indeterminate: indeterminate = \"indeterminate\";\n\n /**\n * Converts a trilean value to a string.\n *\n * @param x -\n * The value to convert.\n *\n * @returns\n * A string of true if x is true, false if x is false,\n * and indeterminate if x is the indeterminate symbol.\n */\n public static stringify(x: trilean): string {\n return String(x);\n }\n\n /**\n * Converts a string to a trilean value.\n *\n * @param str -\n * The text to parse.\n * @param fallback -\n * The fallback in the case that str is not a parsable value.\n *\n * @returns\n * The parsed trilean value or fallback if str is not a valid\n * trilean value.\n */\n public static parse(\n str: string | null | undefined,\n fallback: trilean = false,\n ): trilean {\n if (str?.toUpperCase().localeCompare(\"TRUE\") === 0) {\n return true;\n }\n\n if (str?.toUpperCase().localeCompare(\"FALSE\") === 0) {\n return false;\n }\n\n if (str?.toUpperCase().localeCompare(\"INDETERMINATE\") === 0) {\n return ZTrilean.Indeterminate;\n }\n\n return fallback;\n }\n\n /**\n * Gets a value that determines if val is a trilean supported value.\n *\n * @param val -\n * The value to test.\n *\n * @returns\n * True if val is a trilean value. False otherwise.\n */\n public static is(val: any): val is trilean {\n return typeof val === \"boolean\" || ZTrilean.isIndeterminate(val);\n }\n\n /**\n * Gets whether val is the indeterminate value.\n */\n public static isIndeterminate(val: trilean): val is indeterminate {\n return val === ZTrilean.Indeterminate;\n }\n\n /**\n * Converts from a value to a trilean value.\n *\n * This is similar to parse, but it also supports non\n * string inputs which will be converted to a boolean.\n *\n * @param val -\n * The value to convert.\n * @param fallback -\n * The fallback value in the case that val cannot be converted. This will\n * only be used in the case that val is a string, null, or undefined.\n *\n * @returns\n * The trilean value of val. If val is null or undefined, then\n * fallback is returned. If val is a string, then the return value of\n * {@link parse} will be used. If val is a boolean, then it will be\n * directly returned. If val is equal to {@link Indeterminate}, then\n * {@link Indeterminate} will be returned. Otherwise, the truthy\n * nature of value will be returned.\n */\n public static convert(val: any, fallback: trilean = false): trilean {\n if (val == null) {\n return fallback;\n }\n\n if (ZTrilean.is(val)) {\n return val;\n }\n\n if (typeof val === \"string\") {\n return ZTrilean.parse(val, fallback);\n }\n\n return !!val;\n }\n}\n"],"
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["ZTrilean","Indeterminate","stringify","x","String","parse","str","fallback","toUpperCase","localeCompare","is","val","isIndeterminate","convert"],"sources":["../src/trilean.mts"],"sourcesContent":["/**\n * The indeterminate type.\n *\n * Note that the indeterminate type is officially a string\n * here instead of null. This is by design since null\n * is considered to have no value.\n *\n * Instead of checking for the indeterminate string, always\n * make sure to use ZTrilean.isIndeterminate(val) to check\n * for this type.\n *\n * When setting the indeterminate value, use\n * ZTrilean.Indeterminate as the value to set.\n *\n * @example\n *\n * ```ts\n * const value: indeterminate = ZTrilean.Indeterminate;\n * ```\n */\nexport type indeterminate = \"indeterminate\";\n\n/**\n * Represents a tri logic state.\n *\n * See {@link indeterminate} for what the symbol value should be.\n */\nexport type trilean = boolean | indeterminate;\n\n/**\n * A utility class for trilean values.\n */\nexport abstract class ZTrilean {\n /**\n * A constant value that represents an indeterminate value.\n */\n public static readonly Indeterminate: indeterminate = \"indeterminate\";\n\n /**\n * Converts a trilean value to a string.\n *\n * @param x -\n * The value to convert.\n *\n * @returns\n * A string of true if x is true, false if x is false,\n * and indeterminate if x is the indeterminate symbol.\n */\n public static stringify(x: trilean): string {\n return String(x);\n }\n\n /**\n * Converts a string to a trilean value.\n *\n * @param str -\n * The text to parse.\n * @param fallback -\n * The fallback in the case that str is not a parsable value.\n *\n * @returns\n * The parsed trilean value or fallback if str is not a valid\n * trilean value.\n */\n public static parse(\n str: string | null | undefined,\n fallback: trilean = false,\n ): trilean {\n if (str?.toUpperCase().localeCompare(\"TRUE\") === 0) {\n return true;\n }\n\n if (str?.toUpperCase().localeCompare(\"FALSE\") === 0) {\n return false;\n }\n\n if (str?.toUpperCase().localeCompare(\"INDETERMINATE\") === 0) {\n return ZTrilean.Indeterminate;\n }\n\n return fallback;\n }\n\n /**\n * Gets a value that determines if val is a trilean supported value.\n *\n * @param val -\n * The value to test.\n *\n * @returns\n * True if val is a trilean value. False otherwise.\n */\n public static is(val: any): val is trilean {\n return typeof val === \"boolean\" || ZTrilean.isIndeterminate(val);\n }\n\n /**\n * Gets whether val is the indeterminate value.\n */\n public static isIndeterminate(val: trilean): val is indeterminate {\n return val === ZTrilean.Indeterminate;\n }\n\n /**\n * Converts from a value to a trilean value.\n *\n * This is similar to parse, but it also supports non\n * string inputs which will be converted to a boolean.\n *\n * @param val -\n * The value to convert.\n * @param fallback -\n * The fallback value in the case that val cannot be converted. This will\n * only be used in the case that val is a string, null, or undefined.\n *\n * @returns\n * The trilean value of val. If val is null or undefined, then\n * fallback is returned. If val is a string, then the return value of\n * {@link parse} will be used. If val is a boolean, then it will be\n * directly returned. If val is equal to {@link Indeterminate}, then\n * {@link Indeterminate} will be returned. Otherwise, the truthy\n * nature of value will be returned.\n */\n public static convert(val: any, fallback: trilean = false): trilean {\n if (val == null) {\n return fallback;\n }\n\n if (ZTrilean.is(val)) {\n return val;\n }\n\n if (typeof val === \"string\") {\n return ZTrilean.parse(val, fallback);\n }\n\n return !!val;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;GAgCA,IAAsBA,WAAtB,MAAsBA,SAAAA;;;IAIpB,OAAuBC,gBAA+B;;;;;;;;;;IAYtD,OAAcC,UAAUC,GAAoB;AAC1C,SAAOC,OAAOD,EAAAA;;;;;;;;;;;;;IAehB,OAAcE,MACZC,KACAC,WAAoB,OACX;AACT,MAAID,KAAKE,aAAAA,CAAcC,cAAc,OAAA,KAAY,EAC/C,QAAO;AAGT,MAAIH,KAAKE,aAAAA,CAAcC,cAAc,QAAA,KAAa,EAChD,QAAO;AAGT,MAAIH,KAAKE,aAAAA,CAAcC,cAAc,gBAAA,KAAqB,EACxD,QAAOT,SAASC;AAGlB,SAAOM;;;;;;;;;;IAYT,OAAcG,GAAGC,KAA0B;AACzC,SAAO,OAAOA,QAAQ,aAAaX,SAASY,gBAAgBD,IAAAA;;;;IAM9D,OAAcC,gBAAgBD,KAAoC;AAChE,SAAOA,QAAQX,SAASC;;;;;;;;;;;;;;;;;;;;;IAuB1B,OAAcY,QAAQF,KAAUJ,WAAoB,OAAgB;AAClE,MAAII,OAAO,KACT,QAAOJ;AAGT,MAAIP,SAASU,GAAGC,IAAAA,CACd,QAAOA;AAGT,MAAI,OAAOA,QAAQ,SACjB,QAAOX,SAASK,MAAMM,KAAKJ,SAAAA;AAG7B,SAAO,CAAC,CAACI"}
|
package/dist/index.js
CHANGED
|
@@ -1,124 +1,101 @@
|
|
|
1
|
+
//#region src/trilean.mts
|
|
1
2
|
/**
|
|
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
|
-
* @returns
|
|
100
|
-
* The trilean value of val. If val is null or undefined, then
|
|
101
|
-
* fallback is returned. If val is a string, then the return value of
|
|
102
|
-
* {@link parse} will be used. If val is a boolean, then it will be
|
|
103
|
-
* directly returned. If val is equal to {@link Indeterminate}, then
|
|
104
|
-
* {@link Indeterminate} will be returned. Otherwise, the truthy
|
|
105
|
-
* nature of value will be returned.
|
|
106
|
-
*/ static convert(val, fallback = false) {
|
|
107
|
-
if (val == null) {
|
|
108
|
-
return fallback;
|
|
109
|
-
}
|
|
110
|
-
if (ZTrilean.is(val)) {
|
|
111
|
-
return val;
|
|
112
|
-
}
|
|
113
|
-
if (typeof val === "string") {
|
|
114
|
-
return ZTrilean.parse(val, fallback);
|
|
115
|
-
}
|
|
116
|
-
return !!val;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* A constant value that represents an indeterminate value.
|
|
121
|
-
*/ _define_property(ZTrilean, "Indeterminate", "indeterminate");
|
|
122
|
-
|
|
3
|
+
* The indeterminate type.
|
|
4
|
+
*
|
|
5
|
+
* Note that the indeterminate type is officially a string
|
|
6
|
+
* here instead of null. This is by design since null
|
|
7
|
+
* is considered to have no value.
|
|
8
|
+
*
|
|
9
|
+
* Instead of checking for the indeterminate string, always
|
|
10
|
+
* make sure to use ZTrilean.isIndeterminate(val) to check
|
|
11
|
+
* for this type.
|
|
12
|
+
*
|
|
13
|
+
* When setting the indeterminate value, use
|
|
14
|
+
* ZTrilean.Indeterminate as the value to set.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* const value: indeterminate = ZTrilean.Indeterminate;
|
|
20
|
+
* ```
|
|
21
|
+
*/ /**
|
|
22
|
+
* A utility class for trilean values.
|
|
23
|
+
*/ var ZTrilean = class ZTrilean {
|
|
24
|
+
/**
|
|
25
|
+
* A constant value that represents an indeterminate value.
|
|
26
|
+
*/ static Indeterminate = "indeterminate";
|
|
27
|
+
/**
|
|
28
|
+
* Converts a trilean value to a string.
|
|
29
|
+
*
|
|
30
|
+
* @param x -
|
|
31
|
+
* The value to convert.
|
|
32
|
+
*
|
|
33
|
+
* @returns
|
|
34
|
+
* A string of true if x is true, false if x is false,
|
|
35
|
+
* and indeterminate if x is the indeterminate symbol.
|
|
36
|
+
*/ static stringify(x) {
|
|
37
|
+
return String(x);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Converts a string to a trilean value.
|
|
41
|
+
*
|
|
42
|
+
* @param str -
|
|
43
|
+
* The text to parse.
|
|
44
|
+
* @param fallback -
|
|
45
|
+
* The fallback in the case that str is not a parsable value.
|
|
46
|
+
*
|
|
47
|
+
* @returns
|
|
48
|
+
* The parsed trilean value or fallback if str is not a valid
|
|
49
|
+
* trilean value.
|
|
50
|
+
*/ static parse(str, fallback = false) {
|
|
51
|
+
if (str?.toUpperCase().localeCompare("TRUE") === 0) return true;
|
|
52
|
+
if (str?.toUpperCase().localeCompare("FALSE") === 0) return false;
|
|
53
|
+
if (str?.toUpperCase().localeCompare("INDETERMINATE") === 0) return ZTrilean.Indeterminate;
|
|
54
|
+
return fallback;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Gets a value that determines if val is a trilean supported value.
|
|
58
|
+
*
|
|
59
|
+
* @param val -
|
|
60
|
+
* The value to test.
|
|
61
|
+
*
|
|
62
|
+
* @returns
|
|
63
|
+
* True if val is a trilean value. False otherwise.
|
|
64
|
+
*/ static is(val) {
|
|
65
|
+
return typeof val === "boolean" || ZTrilean.isIndeterminate(val);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Gets whether val is the indeterminate value.
|
|
69
|
+
*/ static isIndeterminate(val) {
|
|
70
|
+
return val === ZTrilean.Indeterminate;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Converts from a value to a trilean value.
|
|
74
|
+
*
|
|
75
|
+
* This is similar to parse, but it also supports non
|
|
76
|
+
* string inputs which will be converted to a boolean.
|
|
77
|
+
*
|
|
78
|
+
* @param val -
|
|
79
|
+
* The value to convert.
|
|
80
|
+
* @param fallback -
|
|
81
|
+
* The fallback value in the case that val cannot be converted. This will
|
|
82
|
+
* only be used in the case that val is a string, null, or undefined.
|
|
83
|
+
*
|
|
84
|
+
* @returns
|
|
85
|
+
* The trilean value of val. If val is null or undefined, then
|
|
86
|
+
* fallback is returned. If val is a string, then the return value of
|
|
87
|
+
* {@link parse} will be used. If val is a boolean, then it will be
|
|
88
|
+
* directly returned. If val is equal to {@link Indeterminate}, then
|
|
89
|
+
* {@link Indeterminate} will be returned. Otherwise, the truthy
|
|
90
|
+
* nature of value will be returned.
|
|
91
|
+
*/ static convert(val, fallback = false) {
|
|
92
|
+
if (val == null) return fallback;
|
|
93
|
+
if (ZTrilean.is(val)) return val;
|
|
94
|
+
if (typeof val === "string") return ZTrilean.parse(val, fallback);
|
|
95
|
+
return !!val;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
//#endregion
|
|
123
99
|
export { ZTrilean };
|
|
124
|
-
|
|
100
|
+
|
|
101
|
+
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/trilean.mts"],"sourcesContent":["/**\n * The indeterminate type.\n *\n * Note that the indeterminate type is officially a string\n * here instead of null. This is by design since null\n * is considered to have no value.\n *\n * Instead of checking for the indeterminate string, always\n * make sure to use ZTrilean.isIndeterminate(val) to check\n * for this type.\n *\n * When setting the indeterminate value, use\n * ZTrilean.Indeterminate as the value to set.\n *\n * @example\n *\n * ```ts\n * const value: indeterminate = ZTrilean.Indeterminate;\n * ```\n */\nexport type indeterminate = \"indeterminate\";\n\n/**\n * Represents a tri logic state.\n *\n * See {@link indeterminate} for what the symbol value should be.\n */\nexport type trilean = boolean | indeterminate;\n\n/**\n * A utility class for trilean values.\n */\nexport abstract class ZTrilean {\n /**\n * A constant value that represents an indeterminate value.\n */\n public static readonly Indeterminate: indeterminate = \"indeterminate\";\n\n /**\n * Converts a trilean value to a string.\n *\n * @param x -\n * The value to convert.\n *\n * @returns\n * A string of true if x is true, false if x is false,\n * and indeterminate if x is the indeterminate symbol.\n */\n public static stringify(x: trilean): string {\n return String(x);\n }\n\n /**\n * Converts a string to a trilean value.\n *\n * @param str -\n * The text to parse.\n * @param fallback -\n * The fallback in the case that str is not a parsable value.\n *\n * @returns\n * The parsed trilean value or fallback if str is not a valid\n * trilean value.\n */\n public static parse(\n str: string | null | undefined,\n fallback: trilean = false,\n ): trilean {\n if (str?.toUpperCase().localeCompare(\"TRUE\") === 0) {\n return true;\n }\n\n if (str?.toUpperCase().localeCompare(\"FALSE\") === 0) {\n return false;\n }\n\n if (str?.toUpperCase().localeCompare(\"INDETERMINATE\") === 0) {\n return ZTrilean.Indeterminate;\n }\n\n return fallback;\n }\n\n /**\n * Gets a value that determines if val is a trilean supported value.\n *\n * @param val -\n * The value to test.\n *\n * @returns\n * True if val is a trilean value. False otherwise.\n */\n public static is(val: any): val is trilean {\n return typeof val === \"boolean\" || ZTrilean.isIndeterminate(val);\n }\n\n /**\n * Gets whether val is the indeterminate value.\n */\n public static isIndeterminate(val: trilean): val is indeterminate {\n return val === ZTrilean.Indeterminate;\n }\n\n /**\n * Converts from a value to a trilean value.\n *\n * This is similar to parse, but it also supports non\n * string inputs which will be converted to a boolean.\n *\n * @param val -\n * The value to convert.\n * @param fallback -\n * The fallback value in the case that val cannot be converted. This will\n * only be used in the case that val is a string, null, or undefined.\n *\n * @returns\n * The trilean value of val. If val is null or undefined, then\n * fallback is returned. If val is a string, then the return value of\n * {@link parse} will be used. If val is a boolean, then it will be\n * directly returned. If val is equal to {@link Indeterminate}, then\n * {@link Indeterminate} will be returned. Otherwise, the truthy\n * nature of value will be returned.\n */\n public static convert(val: any, fallback: trilean = false): trilean {\n if (val == null) {\n return fallback;\n }\n\n if (ZTrilean.is(val)) {\n return val;\n }\n\n if (typeof val === \"string\") {\n return ZTrilean.parse(val, fallback);\n }\n\n return !!val;\n }\n}\n"],"
|
|
1
|
+
{"version":3,"file":"index.js","names":["ZTrilean","Indeterminate","stringify","x","String","parse","str","fallback","toUpperCase","localeCompare","is","val","isIndeterminate","convert"],"sources":["../src/trilean.mts"],"sourcesContent":["/**\n * The indeterminate type.\n *\n * Note that the indeterminate type is officially a string\n * here instead of null. This is by design since null\n * is considered to have no value.\n *\n * Instead of checking for the indeterminate string, always\n * make sure to use ZTrilean.isIndeterminate(val) to check\n * for this type.\n *\n * When setting the indeterminate value, use\n * ZTrilean.Indeterminate as the value to set.\n *\n * @example\n *\n * ```ts\n * const value: indeterminate = ZTrilean.Indeterminate;\n * ```\n */\nexport type indeterminate = \"indeterminate\";\n\n/**\n * Represents a tri logic state.\n *\n * See {@link indeterminate} for what the symbol value should be.\n */\nexport type trilean = boolean | indeterminate;\n\n/**\n * A utility class for trilean values.\n */\nexport abstract class ZTrilean {\n /**\n * A constant value that represents an indeterminate value.\n */\n public static readonly Indeterminate: indeterminate = \"indeterminate\";\n\n /**\n * Converts a trilean value to a string.\n *\n * @param x -\n * The value to convert.\n *\n * @returns\n * A string of true if x is true, false if x is false,\n * and indeterminate if x is the indeterminate symbol.\n */\n public static stringify(x: trilean): string {\n return String(x);\n }\n\n /**\n * Converts a string to a trilean value.\n *\n * @param str -\n * The text to parse.\n * @param fallback -\n * The fallback in the case that str is not a parsable value.\n *\n * @returns\n * The parsed trilean value or fallback if str is not a valid\n * trilean value.\n */\n public static parse(\n str: string | null | undefined,\n fallback: trilean = false,\n ): trilean {\n if (str?.toUpperCase().localeCompare(\"TRUE\") === 0) {\n return true;\n }\n\n if (str?.toUpperCase().localeCompare(\"FALSE\") === 0) {\n return false;\n }\n\n if (str?.toUpperCase().localeCompare(\"INDETERMINATE\") === 0) {\n return ZTrilean.Indeterminate;\n }\n\n return fallback;\n }\n\n /**\n * Gets a value that determines if val is a trilean supported value.\n *\n * @param val -\n * The value to test.\n *\n * @returns\n * True if val is a trilean value. False otherwise.\n */\n public static is(val: any): val is trilean {\n return typeof val === \"boolean\" || ZTrilean.isIndeterminate(val);\n }\n\n /**\n * Gets whether val is the indeterminate value.\n */\n public static isIndeterminate(val: trilean): val is indeterminate {\n return val === ZTrilean.Indeterminate;\n }\n\n /**\n * Converts from a value to a trilean value.\n *\n * This is similar to parse, but it also supports non\n * string inputs which will be converted to a boolean.\n *\n * @param val -\n * The value to convert.\n * @param fallback -\n * The fallback value in the case that val cannot be converted. This will\n * only be used in the case that val is a string, null, or undefined.\n *\n * @returns\n * The trilean value of val. If val is null or undefined, then\n * fallback is returned. If val is a string, then the return value of\n * {@link parse} will be used. If val is a boolean, then it will be\n * directly returned. If val is equal to {@link Indeterminate}, then\n * {@link Indeterminate} will be returned. Otherwise, the truthy\n * nature of value will be returned.\n */\n public static convert(val: any, fallback: trilean = false): trilean {\n if (val == null) {\n return fallback;\n }\n\n if (ZTrilean.is(val)) {\n return val;\n }\n\n if (typeof val === \"string\") {\n return ZTrilean.parse(val, fallback);\n }\n\n return !!val;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;GAgCA,IAAsBA,WAAtB,MAAsBA,SAAAA;;;IAIpB,OAAuBC,gBAA+B;;;;;;;;;;IAYtD,OAAcC,UAAUC,GAAoB;AAC1C,SAAOC,OAAOD,EAAAA;;;;;;;;;;;;;IAehB,OAAcE,MACZC,KACAC,WAAoB,OACX;AACT,MAAID,KAAKE,aAAAA,CAAcC,cAAc,OAAA,KAAY,EAC/C,QAAO;AAGT,MAAIH,KAAKE,aAAAA,CAAcC,cAAc,QAAA,KAAa,EAChD,QAAO;AAGT,MAAIH,KAAKE,aAAAA,CAAcC,cAAc,gBAAA,KAAqB,EACxD,QAAOT,SAASC;AAGlB,SAAOM;;;;;;;;;;IAYT,OAAcG,GAAGC,KAA0B;AACzC,SAAO,OAAOA,QAAQ,aAAaX,SAASY,gBAAgBD,IAAAA;;;;IAM9D,OAAcC,gBAAgBD,KAAoC;AAChE,SAAOA,QAAQX,SAASC;;;;;;;;;;;;;;;;;;;;;IAuB1B,OAAcY,QAAQF,KAAUJ,WAAoB,OAAgB;AAClE,MAAII,OAAO,KACT,QAAOJ;AAGT,MAAIP,SAASU,GAAGC,IAAAA,CACd,QAAOA;AAGT,MAAI,OAAOA,QAAQ,SACjB,QAAOX,SAASK,MAAMM,KAAKJ,SAAAA;AAG7B,SAAO,CAAC,CAACI"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zthun/trilean",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.20",
|
|
4
4
|
"description": "A tri state boolean logic type.",
|
|
5
5
|
"author": "Anthony Bonta",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,15 +25,15 @@
|
|
|
25
25
|
"access": "public"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@zthun/janitor-build-config": "^
|
|
28
|
+
"@zthun/janitor-build-config": "^20.0.2",
|
|
29
29
|
"typescript": "~5.9.3",
|
|
30
|
-
"vite": "^
|
|
31
|
-
"vitest": "^4.
|
|
30
|
+
"vite": "^8.0.3",
|
|
31
|
+
"vitest": "^4.1.2",
|
|
32
32
|
"vitest-mock-extended": "^3.1.0"
|
|
33
33
|
},
|
|
34
34
|
"files": [
|
|
35
35
|
"dist"
|
|
36
36
|
],
|
|
37
37
|
"sideEffects": false,
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "fb21f2279a05eba87d706c959e6ab56e41f2b364"
|
|
39
39
|
}
|