@zthun/trilean 2.0.15 → 2.0.18
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 +40 -81
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +40 -81
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -21,25 +21,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
21
21
|
* ```ts
|
|
22
22
|
* const value: indeterminate = ZTrilean.Indeterminate;
|
|
23
23
|
* ```
|
|
24
|
-
*/ function
|
|
25
|
-
if (!(instance instanceof Constructor)) {
|
|
26
|
-
throw new TypeError("Cannot call a class as a function");
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
function _defineProperties(target, props) {
|
|
30
|
-
for(var i = 0; i < props.length; i++){
|
|
31
|
-
var descriptor = props[i];
|
|
32
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
|
33
|
-
descriptor.configurable = true;
|
|
34
|
-
if ("value" in descriptor) descriptor.writable = true;
|
|
35
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
function _create_class(Constructor, protoProps, staticProps) {
|
|
39
|
-
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
40
|
-
return Constructor;
|
|
41
|
-
}
|
|
42
|
-
function _define_property(obj, key, value) {
|
|
24
|
+
*/ function _define_property(obj, key, value) {
|
|
43
25
|
if (key in obj) {
|
|
44
26
|
Object.defineProperty(obj, key, {
|
|
45
27
|
value: value,
|
|
@@ -54,14 +36,8 @@ function _define_property(obj, key, value) {
|
|
|
54
36
|
}
|
|
55
37
|
/**
|
|
56
38
|
* A utility class for trilean values.
|
|
57
|
-
*/
|
|
58
|
-
|
|
59
|
-
_class_call_check(this, ZTrilean);
|
|
60
|
-
}
|
|
61
|
-
_create_class(ZTrilean, null, [
|
|
62
|
-
{
|
|
63
|
-
key: "stringify",
|
|
64
|
-
value: /**
|
|
39
|
+
*/ class ZTrilean {
|
|
40
|
+
/**
|
|
65
41
|
* Converts a trilean value to a string.
|
|
66
42
|
*
|
|
67
43
|
* @param x -
|
|
@@ -70,13 +46,10 @@ function _define_property(obj, key, value) {
|
|
|
70
46
|
* @returns
|
|
71
47
|
* A string of true if x is true, false if x is false,
|
|
72
48
|
* and indeterminate if x is the indeterminate symbol.
|
|
73
|
-
*/
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
{
|
|
78
|
-
key: "parse",
|
|
79
|
-
value: /**
|
|
49
|
+
*/ static stringify(x) {
|
|
50
|
+
return String(x);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
80
53
|
* Converts a string to a trilean value.
|
|
81
54
|
*
|
|
82
55
|
* @param str -
|
|
@@ -87,23 +60,19 @@ function _define_property(obj, key, value) {
|
|
|
87
60
|
* @returns
|
|
88
61
|
* The parsed trilean value or fallback if str is not a valid
|
|
89
62
|
* trilean value.
|
|
90
|
-
*/
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
},
|
|
104
|
-
{
|
|
105
|
-
key: "is",
|
|
106
|
-
value: /**
|
|
63
|
+
*/ static parse(str, fallback = false) {
|
|
64
|
+
if (str?.toUpperCase().localeCompare("TRUE") === 0) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
if (str?.toUpperCase().localeCompare("FALSE") === 0) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
if (str?.toUpperCase().localeCompare("INDETERMINATE") === 0) {
|
|
71
|
+
return ZTrilean.Indeterminate;
|
|
72
|
+
}
|
|
73
|
+
return fallback;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
107
76
|
* Gets a value that determines if val is a trilean supported value.
|
|
108
77
|
*
|
|
109
78
|
* @param val -
|
|
@@ -111,21 +80,15 @@ function _define_property(obj, key, value) {
|
|
|
111
80
|
*
|
|
112
81
|
* @returns
|
|
113
82
|
* True if val is a trilean value. False otherwise.
|
|
114
|
-
*/
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
{
|
|
119
|
-
key: "isIndeterminate",
|
|
120
|
-
value: /**
|
|
83
|
+
*/ static is(val) {
|
|
84
|
+
return typeof val === "boolean" || ZTrilean.isIndeterminate(val);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
121
87
|
* Gets whether val is the indeterminate value.
|
|
122
|
-
*/
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
{
|
|
127
|
-
key: "convert",
|
|
128
|
-
value: /**
|
|
88
|
+
*/ static isIndeterminate(val) {
|
|
89
|
+
return val === ZTrilean.Indeterminate;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
129
92
|
* Converts from a value to a trilean value.
|
|
130
93
|
*
|
|
131
94
|
* This is similar to parse, but it also supports non
|
|
@@ -144,23 +107,19 @@ function _define_property(obj, key, value) {
|
|
|
144
107
|
* directly returned. If val is equal to {@link Indeterminate}, then
|
|
145
108
|
* {@link Indeterminate} will be returned. Otherwise, the truthy
|
|
146
109
|
* nature of value will be returned.
|
|
147
|
-
*/
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
return fallback;
|
|
151
|
-
}
|
|
152
|
-
if (ZTrilean.is(val)) {
|
|
153
|
-
return val;
|
|
154
|
-
}
|
|
155
|
-
if (typeof val === "string") {
|
|
156
|
-
return ZTrilean.parse(val, fallback);
|
|
157
|
-
}
|
|
158
|
-
return !!val;
|
|
159
|
-
}
|
|
110
|
+
*/ static convert(val, fallback = false) {
|
|
111
|
+
if (val == null) {
|
|
112
|
+
return fallback;
|
|
160
113
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
114
|
+
if (ZTrilean.is(val)) {
|
|
115
|
+
return val;
|
|
116
|
+
}
|
|
117
|
+
if (typeof val === "string") {
|
|
118
|
+
return ZTrilean.parse(val, fallback);
|
|
119
|
+
}
|
|
120
|
+
return !!val;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
164
123
|
/**
|
|
165
124
|
* A constant value that represents an indeterminate value.
|
|
166
125
|
*/ _define_property(ZTrilean, "Indeterminate", "indeterminate");
|
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"],"names":["ZTrilean","stringify","x","String","parse","str","fallback","toUpperCase","localeCompare","Indeterminate","is","val","isIndeterminate","convert"],"mappings":";;;;AAAA;;;;;;;;;;;;;;;;;;;AAmBC,IAAA,SAAA,
|
|
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"],"names":["ZTrilean","stringify","x","String","parse","str","fallback","toUpperCase","localeCompare","Indeterminate","is","val","isIndeterminate","convert"],"mappings":";;;;AAAA;;;;;;;;;;;;;;;;;;;AAmBC,IAAA,SAAA,gBAAA,CAAA,GAAA,EAAA,GAAA,EAAA,KAAA,EAAA;;;;;;;;;;;;;AAUD;;AAEC,IACM,MAAeA,QAAAA,CAAAA;AAMpB;;;;;;;;;MAUA,OAAcC,SAAAA,CAAUC,CAAU,EAAU;AAC1C,QAAA,OAAOC,MAAAA,CAAOD,CAAAA,CAAAA;AAChB,IAAA;AAEA;;;;;;;;;;;AAWC,MACD,OAAcE,KAAAA,CACZC,GAA8B,EAC9BC,QAAAA,GAAoB,KAAK,EAChB;AACT,QAAA,IAAID,GAAAA,EAAKE,WAAAA,EAAAA,CAAcC,aAAAA,CAAc,MAAA,CAAA,KAAY,CAAA,EAAG;YAClD,OAAO,IAAA;AACT,QAAA;AAEA,QAAA,IAAIH,GAAAA,EAAKE,WAAAA,EAAAA,CAAcC,aAAAA,CAAc,OAAA,CAAA,KAAa,CAAA,EAAG;YACnD,OAAO,KAAA;AACT,QAAA;AAEA,QAAA,IAAIH,GAAAA,EAAKE,WAAAA,EAAAA,CAAcC,aAAAA,CAAc,eAAA,CAAA,KAAqB,CAAA,EAAG;AAC3D,YAAA,OAAOR,SAASS,aAAa;AAC/B,QAAA;QAEA,OAAOH,QAAAA;AACT,IAAA;AAEA;;;;;;;;MASA,OAAcI,EAAAA,CAAGC,GAAQ,EAAkB;AACzC,QAAA,OAAO,OAAOA,GAAAA,KAAQ,SAAA,IAAaX,QAAAA,CAASY,eAAe,CAACD,GAAAA,CAAAA;AAC9D,IAAA;AAEA;;MAGA,OAAcC,eAAAA,CAAgBD,GAAY,EAAwB;QAChE,OAAOA,GAAAA,KAAQX,SAASS,aAAa;AACvC,IAAA;AAEA;;;;;;;;;;;;;;;;;;;AAmBC,MACD,OAAcI,OAAAA,CAAQF,GAAQ,EAAEL,QAAAA,GAAoB,KAAK,EAAW;AAClE,QAAA,IAAIK,OAAO,IAAA,EAAM;YACf,OAAOL,QAAAA;AACT,QAAA;QAEA,IAAIN,QAAAA,CAASU,EAAE,CAACC,GAAAA,CAAAA,EAAM;YACpB,OAAOA,GAAAA;AACT,QAAA;QAEA,IAAI,OAAOA,QAAQ,QAAA,EAAU;YAC3B,OAAOX,QAAAA,CAASI,KAAK,CAACO,GAAAA,EAAKL,QAAAA,CAAAA;AAC7B,QAAA;AAEA,QAAA,OAAO,CAAC,CAACK,GAAAA;AACX,IAAA;AACF;AAzGE;;MAGA,gBAAA,CAJoBX,UAIGS,eAAAA,EAA+B,eAAA,CAAA;;;;"}
|
package/dist/index.js
CHANGED
|
@@ -17,25 +17,7 @@
|
|
|
17
17
|
* ```ts
|
|
18
18
|
* const value: indeterminate = ZTrilean.Indeterminate;
|
|
19
19
|
* ```
|
|
20
|
-
*/ function
|
|
21
|
-
if (!(instance instanceof Constructor)) {
|
|
22
|
-
throw new TypeError("Cannot call a class as a function");
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
function _defineProperties(target, props) {
|
|
26
|
-
for(var i = 0; i < props.length; i++){
|
|
27
|
-
var descriptor = props[i];
|
|
28
|
-
descriptor.enumerable = descriptor.enumerable || false;
|
|
29
|
-
descriptor.configurable = true;
|
|
30
|
-
if ("value" in descriptor) descriptor.writable = true;
|
|
31
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
function _create_class(Constructor, protoProps, staticProps) {
|
|
35
|
-
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
36
|
-
return Constructor;
|
|
37
|
-
}
|
|
38
|
-
function _define_property(obj, key, value) {
|
|
20
|
+
*/ function _define_property(obj, key, value) {
|
|
39
21
|
if (key in obj) {
|
|
40
22
|
Object.defineProperty(obj, key, {
|
|
41
23
|
value: value,
|
|
@@ -50,14 +32,8 @@ function _define_property(obj, key, value) {
|
|
|
50
32
|
}
|
|
51
33
|
/**
|
|
52
34
|
* A utility class for trilean values.
|
|
53
|
-
*/
|
|
54
|
-
|
|
55
|
-
_class_call_check(this, ZTrilean);
|
|
56
|
-
}
|
|
57
|
-
_create_class(ZTrilean, null, [
|
|
58
|
-
{
|
|
59
|
-
key: "stringify",
|
|
60
|
-
value: /**
|
|
35
|
+
*/ class ZTrilean {
|
|
36
|
+
/**
|
|
61
37
|
* Converts a trilean value to a string.
|
|
62
38
|
*
|
|
63
39
|
* @param x -
|
|
@@ -66,13 +42,10 @@ function _define_property(obj, key, value) {
|
|
|
66
42
|
* @returns
|
|
67
43
|
* A string of true if x is true, false if x is false,
|
|
68
44
|
* and indeterminate if x is the indeterminate symbol.
|
|
69
|
-
*/
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
{
|
|
74
|
-
key: "parse",
|
|
75
|
-
value: /**
|
|
45
|
+
*/ static stringify(x) {
|
|
46
|
+
return String(x);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
76
49
|
* Converts a string to a trilean value.
|
|
77
50
|
*
|
|
78
51
|
* @param str -
|
|
@@ -83,23 +56,19 @@ function _define_property(obj, key, value) {
|
|
|
83
56
|
* @returns
|
|
84
57
|
* The parsed trilean value or fallback if str is not a valid
|
|
85
58
|
* trilean value.
|
|
86
|
-
*/
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
},
|
|
100
|
-
{
|
|
101
|
-
key: "is",
|
|
102
|
-
value: /**
|
|
59
|
+
*/ static parse(str, fallback = false) {
|
|
60
|
+
if (str?.toUpperCase().localeCompare("TRUE") === 0) {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
if (str?.toUpperCase().localeCompare("FALSE") === 0) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
if (str?.toUpperCase().localeCompare("INDETERMINATE") === 0) {
|
|
67
|
+
return ZTrilean.Indeterminate;
|
|
68
|
+
}
|
|
69
|
+
return fallback;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
103
72
|
* Gets a value that determines if val is a trilean supported value.
|
|
104
73
|
*
|
|
105
74
|
* @param val -
|
|
@@ -107,21 +76,15 @@ function _define_property(obj, key, value) {
|
|
|
107
76
|
*
|
|
108
77
|
* @returns
|
|
109
78
|
* True if val is a trilean value. False otherwise.
|
|
110
|
-
*/
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
{
|
|
115
|
-
key: "isIndeterminate",
|
|
116
|
-
value: /**
|
|
79
|
+
*/ static is(val) {
|
|
80
|
+
return typeof val === "boolean" || ZTrilean.isIndeterminate(val);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
117
83
|
* Gets whether val is the indeterminate value.
|
|
118
|
-
*/
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
{
|
|
123
|
-
key: "convert",
|
|
124
|
-
value: /**
|
|
84
|
+
*/ static isIndeterminate(val) {
|
|
85
|
+
return val === ZTrilean.Indeterminate;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
125
88
|
* Converts from a value to a trilean value.
|
|
126
89
|
*
|
|
127
90
|
* This is similar to parse, but it also supports non
|
|
@@ -140,23 +103,19 @@ function _define_property(obj, key, value) {
|
|
|
140
103
|
* directly returned. If val is equal to {@link Indeterminate}, then
|
|
141
104
|
* {@link Indeterminate} will be returned. Otherwise, the truthy
|
|
142
105
|
* nature of value will be returned.
|
|
143
|
-
*/
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
return fallback;
|
|
147
|
-
}
|
|
148
|
-
if (ZTrilean.is(val)) {
|
|
149
|
-
return val;
|
|
150
|
-
}
|
|
151
|
-
if (typeof val === "string") {
|
|
152
|
-
return ZTrilean.parse(val, fallback);
|
|
153
|
-
}
|
|
154
|
-
return !!val;
|
|
155
|
-
}
|
|
106
|
+
*/ static convert(val, fallback = false) {
|
|
107
|
+
if (val == null) {
|
|
108
|
+
return fallback;
|
|
156
109
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
}
|
|
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
|
+
}
|
|
160
119
|
/**
|
|
161
120
|
* A constant value that represents an indeterminate value.
|
|
162
121
|
*/ _define_property(ZTrilean, "Indeterminate", "indeterminate");
|
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"],"names":["ZTrilean","stringify","x","String","parse","str","fallback","toUpperCase","localeCompare","Indeterminate","is","val","isIndeterminate","convert"],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;AAmBC,IAAA,SAAA,
|
|
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"],"names":["ZTrilean","stringify","x","String","parse","str","fallback","toUpperCase","localeCompare","Indeterminate","is","val","isIndeterminate","convert"],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;AAmBC,IAAA,SAAA,gBAAA,CAAA,GAAA,EAAA,GAAA,EAAA,KAAA,EAAA;;;;;;;;;;;;;AAUD;;AAEC,IACM,MAAeA,QAAAA,CAAAA;AAMpB;;;;;;;;;MAUA,OAAcC,SAAAA,CAAUC,CAAU,EAAU;AAC1C,QAAA,OAAOC,MAAAA,CAAOD,CAAAA,CAAAA;AAChB,IAAA;AAEA;;;;;;;;;;;AAWC,MACD,OAAcE,KAAAA,CACZC,GAA8B,EAC9BC,QAAAA,GAAoB,KAAK,EAChB;AACT,QAAA,IAAID,GAAAA,EAAKE,WAAAA,EAAAA,CAAcC,aAAAA,CAAc,MAAA,CAAA,KAAY,CAAA,EAAG;YAClD,OAAO,IAAA;AACT,QAAA;AAEA,QAAA,IAAIH,GAAAA,EAAKE,WAAAA,EAAAA,CAAcC,aAAAA,CAAc,OAAA,CAAA,KAAa,CAAA,EAAG;YACnD,OAAO,KAAA;AACT,QAAA;AAEA,QAAA,IAAIH,GAAAA,EAAKE,WAAAA,EAAAA,CAAcC,aAAAA,CAAc,eAAA,CAAA,KAAqB,CAAA,EAAG;AAC3D,YAAA,OAAOR,SAASS,aAAa;AAC/B,QAAA;QAEA,OAAOH,QAAAA;AACT,IAAA;AAEA;;;;;;;;MASA,OAAcI,EAAAA,CAAGC,GAAQ,EAAkB;AACzC,QAAA,OAAO,OAAOA,GAAAA,KAAQ,SAAA,IAAaX,QAAAA,CAASY,eAAe,CAACD,GAAAA,CAAAA;AAC9D,IAAA;AAEA;;MAGA,OAAcC,eAAAA,CAAgBD,GAAY,EAAwB;QAChE,OAAOA,GAAAA,KAAQX,SAASS,aAAa;AACvC,IAAA;AAEA;;;;;;;;;;;;;;;;;;;AAmBC,MACD,OAAcI,OAAAA,CAAQF,GAAQ,EAAEL,QAAAA,GAAoB,KAAK,EAAW;AAClE,QAAA,IAAIK,OAAO,IAAA,EAAM;YACf,OAAOL,QAAAA;AACT,QAAA;QAEA,IAAIN,QAAAA,CAASU,EAAE,CAACC,GAAAA,CAAAA,EAAM;YACpB,OAAOA,GAAAA;AACT,QAAA;QAEA,IAAI,OAAOA,QAAQ,QAAA,EAAU;YAC3B,OAAOX,QAAAA,CAASI,KAAK,CAACO,GAAAA,EAAKL,QAAAA,CAAAA;AAC7B,QAAA;AAEA,QAAA,OAAO,CAAC,CAACK,GAAAA;AACX,IAAA;AACF;AAzGE;;MAGA,gBAAA,CAJoBX,UAIGS,eAAAA,EAA+B,eAAA,CAAA;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zthun/trilean",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.18",
|
|
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": "^19.4
|
|
28
|
+
"@zthun/janitor-build-config": "^19.5.4",
|
|
29
29
|
"typescript": "~5.9.3",
|
|
30
|
-
"vite": "^7.
|
|
31
|
-
"vitest": "^4.0.
|
|
30
|
+
"vite": "^7.3.0",
|
|
31
|
+
"vitest": "^4.0.16",
|
|
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": "8bc0bc00013db1d029c800e3d3cd9680d4cabedd"
|
|
39
39
|
}
|