@zthun/trilean 2.0.1 → 2.0.2
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/README.md +9 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/intrinsic.d.mts +1 -1
- package/dist/trilean.d.mts +1 -1
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ yarn add @zthun/trilean
|
|
|
14
14
|
## Usage
|
|
15
15
|
|
|
16
16
|
```ts
|
|
17
|
-
import { trilean, ZTrilean } from
|
|
17
|
+
import { trilean, ZTrilean } from "@zthun/trilean";
|
|
18
18
|
|
|
19
19
|
const checked: trilean = true;
|
|
20
20
|
const unchecked: trilean = false;
|
|
@@ -28,27 +28,28 @@ ZTrilean.stringify(unchecked);
|
|
|
28
28
|
ZTrilean.stringify(indeterminate);
|
|
29
29
|
|
|
30
30
|
// Returns true
|
|
31
|
-
ZTrilean.parse(
|
|
31
|
+
ZTrilean.parse("true");
|
|
32
32
|
// Returns false
|
|
33
|
-
ZTrilean.parse(
|
|
33
|
+
ZTrilean.parse("false");
|
|
34
34
|
// Returns ZTrilean.Indeterminate
|
|
35
|
-
ZTrilean.parse(
|
|
35
|
+
ZTrilean.parse("indeterminate");
|
|
36
36
|
// Returns false
|
|
37
37
|
ZTrilean.parse(null);
|
|
38
38
|
// Returns true fallback.
|
|
39
|
-
ZTrilean.parse(
|
|
39
|
+
ZTrilean.parse("not-a-trilean", true);
|
|
40
40
|
|
|
41
41
|
// Returns true
|
|
42
42
|
ZTrilean.is(true);
|
|
43
43
|
ZTrilean.is(false);
|
|
44
44
|
ZTrilean.is(ZTrilean.Indeterminate);
|
|
45
45
|
// Returns false
|
|
46
|
-
ZTrilean.is(
|
|
47
|
-
// Returns true - This is mostly for
|
|
46
|
+
ZTrilean.is("string is not a trilean");
|
|
47
|
+
// Returns true - This is mostly for
|
|
48
|
+
// typescript type guard support.
|
|
48
49
|
ZTrilean.isIndeterminate(ZTrilean.Indeterminate);
|
|
49
50
|
|
|
50
51
|
// Convert any value to a trilean.
|
|
51
|
-
const kindaTrue = ZTrilean.convert(
|
|
52
|
+
const kindaTrue = ZTrilean.convert("A truthy value");
|
|
52
53
|
const kindaFalse = ZTrilean.convert(0);
|
|
53
54
|
const alsoIndeterminate = ZTrilean.convert(ZTrilean.Indeterminate);
|
|
54
55
|
```
|
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 =
|
|
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":[],"mappings":";;AAgCO,MAAe,YAAf,MAAe,UAAS;AAAA;AAAA;AAAA;AAAA;AA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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 =
|
|
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":[],"mappings":"AAgCO,MAAe,YAAf,MAAe,UAAS;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgB7B,OAAc,UAAU,GAAoB;AAC1C,WAAO,OAAO,CAAC;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcA,OAAc,MACZ,KACA,WAAoB,OACX;AACT,SAAI,2BAAK,cAAc,cAAc,aAAY,GAAG;AAC3C,aAAA;AAAA,IACT;AAEA,SAAI,2BAAK,cAAc,cAAc,cAAa,GAAG;AAC5C,aAAA;AAAA,IACT;AAEA,SAAI,2BAAK,cAAc,cAAc,sBAAqB,GAAG;AAC3D,aAAO,UAAS;AAAA,IAClB;AAEO,WAAA;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,OAAc,GAAG,KAA0B;AACzC,WAAO,OAAO,QAAQ,aAAa,UAAS,gBAAgB,GAAG;AAAA,EACjE;AAAA;AAAA;AAAA;AAAA,EAKA,OAAc,gBAAgB,KAAoC;AAChE,WAAO,QAAQ,UAAS;AAAA,EAC1B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAsBA,OAAc,QAAQ,KAAU,WAAoB,OAAgB;AAClE,QAAI,OAAO,MAAM;AACR,aAAA;AAAA,IACT;AAEI,QAAA,UAAS,GAAG,GAAG,GAAG;AACb,aAAA;AAAA,IACT;AAEI,QAAA,OAAO,QAAQ,UAAU;AACpB,aAAA,UAAS,MAAM,KAAK,QAAQ;AAAA,IACrC;AAEA,WAAO,CAAC,CAAC;AAAA,EACX;AACF;AAtGE,UAAuB,gBAA+B;AAJjD,IAAe,WAAf;"}
|
package/dist/intrinsic.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Intrinsic types as a type.
|
|
3
3
|
*/
|
|
4
|
-
export type ZIntrinsic =
|
|
4
|
+
export type ZIntrinsic = "bigint" | "boolean" | "function" | "number" | "object" | "string" | "symbol" | "trilean";
|
package/dist/trilean.d.mts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zthun/trilean",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "A tri state boolean logic type.",
|
|
5
5
|
"author": "Anthony Bonta",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,14 +25,14 @@
|
|
|
25
25
|
"access": "public"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"typescript": "~5.5.
|
|
29
|
-
"vite": "^5.
|
|
30
|
-
"vitest": "^2.0.
|
|
31
|
-
"vitest-mock-extended": "^
|
|
28
|
+
"typescript": "~5.5.4",
|
|
29
|
+
"vite": "^5.4.2",
|
|
30
|
+
"vitest": "^2.0.5",
|
|
31
|
+
"vitest-mock-extended": "^2.0.2"
|
|
32
32
|
},
|
|
33
33
|
"files": [
|
|
34
34
|
"dist"
|
|
35
35
|
],
|
|
36
36
|
"sideEffects": false,
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "724479607a31c9f4a7703e6d7dc1f493d79cc577"
|
|
38
38
|
}
|