@zthun/trilean 2.0.19 → 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 CHANGED
@@ -1,128 +1,102 @@
1
- 'use strict';
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
- * A utility class for trilean values.
39
- */ class ZTrilean {
40
- /**
41
- * Converts a trilean value to a string.
42
- *
43
- * @param x -
44
- * The value to convert.
45
- *
46
- * @returns
47
- * A string of true if x is true, false if x is false,
48
- * and indeterminate if x is the indeterminate symbol.
49
- */ static stringify(x) {
50
- return String(x);
51
- }
52
- /**
53
- * Converts a string to a trilean value.
54
- *
55
- * @param str -
56
- * The text to parse.
57
- * @param fallback -
58
- * The fallback in the case that str is not a parsable value.
59
- *
60
- * @returns
61
- * The parsed trilean value or fallback if str is not a valid
62
- * trilean 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
- /**
76
- * Gets a value that determines if val is a trilean supported value.
77
- *
78
- * @param val -
79
- * The value to test.
80
- *
81
- * @returns
82
- * True if val is a trilean value. False otherwise.
83
- */ static is(val) {
84
- return typeof val === "boolean" || ZTrilean.isIndeterminate(val);
85
- }
86
- /**
87
- * Gets whether val is the indeterminate value.
88
- */ static isIndeterminate(val) {
89
- return val === ZTrilean.Indeterminate;
90
- }
91
- /**
92
- * Converts from a value to a trilean value.
93
- *
94
- * This is similar to parse, but it also supports non
95
- * string inputs which will be converted to a boolean.
96
- *
97
- * @param val -
98
- * The value to convert.
99
- * @param fallback -
100
- * The fallback value in the case that val cannot be converted. This will
101
- * only be used in the case that val is a string, null, or undefined.
102
- *
103
- * @returns
104
- * The trilean value of val. If val is null or undefined, then
105
- * fallback is returned. If val is a string, then the return value of
106
- * {@link parse} will be used. If val is a boolean, then it will be
107
- * directly returned. If val is equal to {@link Indeterminate}, then
108
- * {@link Indeterminate} will be returned. Otherwise, the truthy
109
- * nature of value will be returned.
110
- */ static convert(val, fallback = false) {
111
- if (val == null) {
112
- return fallback;
113
- }
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
- }
123
- /**
124
- * A constant value that represents an indeterminate value.
125
- */ _define_property(ZTrilean, "Indeterminate", "indeterminate");
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
- //# sourceMappingURL=index.cjs.map
101
+
102
+ //# sourceMappingURL=index.cjs.map
@@ -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,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;;;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JoBX,UAIGS,eAAAA,EAA+B,eAAA,CAAA;;;;"}
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
- * The indeterminate type.
3
- *
4
- * Note that the indeterminate type is officially a string
5
- * here instead of null. This is by design since null
6
- * is considered to have no value.
7
- *
8
- * Instead of checking for the indeterminate string, always
9
- * make sure to use ZTrilean.isIndeterminate(val) to check
10
- * for this type.
11
- *
12
- * When setting the indeterminate value, use
13
- * ZTrilean.Indeterminate as the value to set.
14
- *
15
- * @example
16
- *
17
- * ```ts
18
- * const value: indeterminate = ZTrilean.Indeterminate;
19
- * ```
20
- */ function _define_property(obj, key, value) {
21
- if (key in obj) {
22
- Object.defineProperty(obj, key, {
23
- value: value,
24
- enumerable: true,
25
- configurable: true,
26
- writable: true
27
- });
28
- } else {
29
- obj[key] = value;
30
- }
31
- return obj;
32
- }
33
- /**
34
- * A utility class for trilean values.
35
- */ class ZTrilean {
36
- /**
37
- * Converts a trilean value to a string.
38
- *
39
- * @param x -
40
- * The value to convert.
41
- *
42
- * @returns
43
- * A string of true if x is true, false if x is false,
44
- * and indeterminate if x is the indeterminate symbol.
45
- */ static stringify(x) {
46
- return String(x);
47
- }
48
- /**
49
- * Converts a string to a trilean value.
50
- *
51
- * @param str -
52
- * The text to parse.
53
- * @param fallback -
54
- * The fallback in the case that str is not a parsable value.
55
- *
56
- * @returns
57
- * The parsed trilean value or fallback if str is not a valid
58
- * trilean 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
- /**
72
- * Gets a value that determines if val is a trilean supported value.
73
- *
74
- * @param val -
75
- * The value to test.
76
- *
77
- * @returns
78
- * True if val is a trilean value. False otherwise.
79
- */ static is(val) {
80
- return typeof val === "boolean" || ZTrilean.isIndeterminate(val);
81
- }
82
- /**
83
- * Gets whether val is the indeterminate value.
84
- */ static isIndeterminate(val) {
85
- return val === ZTrilean.Indeterminate;
86
- }
87
- /**
88
- * Converts from a value to a trilean value.
89
- *
90
- * This is similar to parse, but it also supports non
91
- * string inputs which will be converted to a boolean.
92
- *
93
- * @param val -
94
- * The value to convert.
95
- * @param fallback -
96
- * The fallback value in the case that val cannot be converted. This will
97
- * only be used in the case that val is a string, null, or undefined.
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
- //# sourceMappingURL=index.js.map
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"],"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,C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AAA;AACF;AAzGE;;MAGA,gBAAA,CAJoBX,UAIGS,eAAAA,EAA+B,eAAA,CAAA;;;;"}
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.19",
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": "^19.5.6",
28
+ "@zthun/janitor-build-config": "^20.0.2",
29
29
  "typescript": "~5.9.3",
30
- "vite": "^7.3.1",
31
- "vitest": "^4.0.18",
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": "ce16b028e2f7c30ae6da3400cb6fe7484bbd00ba"
38
+ "gitHead": "fb21f2279a05eba87d706c959e6ab56e41f2b364"
39
39
  }