@zthun/trilean 1.1.0
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/LICENSE +21 -0
- package/README.md +54 -0
- package/dist/index.cjs +95 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +95 -0
- package/dist/index.js.map +1 -0
- package/dist/intrinsic.d.mts +4 -0
- package/dist/trilean.d.mts +74 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Anthony Bonta
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# Trilean
|
|
2
|
+
|
|
3
|
+
Adds a three state logic type. The values can be true, false, and indeterminate.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
# NPM
|
|
9
|
+
npm install @zthun/trilean
|
|
10
|
+
# Yarn
|
|
11
|
+
yarn add @zthun/trilean
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import { trilean, ZTrilean } from '@zthun/trilean';
|
|
18
|
+
|
|
19
|
+
const checked: trilean = true;
|
|
20
|
+
const unchecked: trilean = false;
|
|
21
|
+
const indeterminate: trilean = ZTrilean.Indeterminate;
|
|
22
|
+
|
|
23
|
+
// Returns 'true'
|
|
24
|
+
ZTrilean.stringify(checked);
|
|
25
|
+
// Returns 'false'
|
|
26
|
+
ZTrilean.stringify(unchecked);
|
|
27
|
+
// Returns 'indeterminate'
|
|
28
|
+
ZTrilean.stringify(indeterminate);
|
|
29
|
+
|
|
30
|
+
// Returns true
|
|
31
|
+
ZTrilean.parse('true');
|
|
32
|
+
// Returns false
|
|
33
|
+
ZTrilean.parse('false');
|
|
34
|
+
// Returns ZTrilean.Indeterminate
|
|
35
|
+
ZTrilean.parse('indeterminate');
|
|
36
|
+
// Returns false
|
|
37
|
+
ZTrilean.parse(null);
|
|
38
|
+
// Returns true fallback.
|
|
39
|
+
ZTrilean.parse('not-a-trilean', true);
|
|
40
|
+
|
|
41
|
+
// Returns true
|
|
42
|
+
ZTrilean.is(true);
|
|
43
|
+
ZTrilean.is(false);
|
|
44
|
+
ZTrilean.is(ZTrilean.Indeterminate);
|
|
45
|
+
// Returns false
|
|
46
|
+
ZTrilean.is('string is not a trilean');
|
|
47
|
+
// Returns true - This is mostly for typescript type guard support.
|
|
48
|
+
ZTrilean.isIndeterminate(ZTrilean.Indeterminate);
|
|
49
|
+
|
|
50
|
+
// Convert any value to a trilean.
|
|
51
|
+
const kindaTrue = ZTrilean.convert('A truthy value');
|
|
52
|
+
const kindaFalse = ZTrilean.convert(0);
|
|
53
|
+
const alsoIndeterminate = ZTrilean.convert(ZTrilean.Indeterminate);
|
|
54
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const _ZTrilean = class _ZTrilean {
|
|
4
|
+
/**
|
|
5
|
+
* Converts a trilean value to a string.
|
|
6
|
+
*
|
|
7
|
+
* @param x -
|
|
8
|
+
* The value to convert.
|
|
9
|
+
*
|
|
10
|
+
* @returns
|
|
11
|
+
* A string of true if x is true, false if x is false,
|
|
12
|
+
* and indeterminate if x is the indeterminate symbol.
|
|
13
|
+
*/
|
|
14
|
+
static stringify(x) {
|
|
15
|
+
return _ZTrilean.isIndeterminate(x) ? x.description : String(x);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Converts a string to a trilean value.
|
|
19
|
+
*
|
|
20
|
+
* @param str -
|
|
21
|
+
* The text to parse.
|
|
22
|
+
* @param fallback -
|
|
23
|
+
* The fallback in the case that str is not a parsable value.
|
|
24
|
+
*
|
|
25
|
+
* @returns
|
|
26
|
+
* The parsed trilean value or fallback if str is not a valid
|
|
27
|
+
* trilean value.
|
|
28
|
+
*/
|
|
29
|
+
static parse(str, fallback = false) {
|
|
30
|
+
if ((str == null ? void 0 : str.toUpperCase().localeCompare("TRUE")) === 0) {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
if ((str == null ? void 0 : str.toUpperCase().localeCompare("FALSE")) === 0) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
if ((str == null ? void 0 : str.toUpperCase().localeCompare("INDETERMINATE")) === 0) {
|
|
37
|
+
return _ZTrilean.Indeterminate;
|
|
38
|
+
}
|
|
39
|
+
return fallback;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Gets a value that determines if val is a trilean supported value.
|
|
43
|
+
*
|
|
44
|
+
* @param val -
|
|
45
|
+
* The value to test.
|
|
46
|
+
*
|
|
47
|
+
* @returns
|
|
48
|
+
* True if val is a trilean value. False otherwise.
|
|
49
|
+
*/
|
|
50
|
+
static is(val) {
|
|
51
|
+
return typeof val === "boolean" || _ZTrilean.isIndeterminate(val);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Gets whether val is the indeterminate value.
|
|
55
|
+
*/
|
|
56
|
+
static isIndeterminate(val) {
|
|
57
|
+
return val === _ZTrilean.Indeterminate;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Converts from a value to a trilean value.
|
|
61
|
+
*
|
|
62
|
+
* This is similar to parse, but it also supports non
|
|
63
|
+
* string inputs which will be converted to a boolean.
|
|
64
|
+
*
|
|
65
|
+
* @param val -
|
|
66
|
+
* The value to convert.
|
|
67
|
+
* @param fallback -
|
|
68
|
+
* The fallback value in the case that val cannot be converted. This will
|
|
69
|
+
* only be used in the case that val is a string, null, or undefined.
|
|
70
|
+
*
|
|
71
|
+
* @returns
|
|
72
|
+
* The trilean value of val. If val is null or undefined, then
|
|
73
|
+
* fallback is returned. If val is a string, then the return value of
|
|
74
|
+
* {@link parse} will be used. If val is a boolean, then it will be
|
|
75
|
+
* directly returned. If val is equal to {@link Indeterminate}, then
|
|
76
|
+
* {@link Indeterminate} will be returned. Otherwise, the truthy
|
|
77
|
+
* nature of value will be returned.
|
|
78
|
+
*/
|
|
79
|
+
static convert(val, fallback = false) {
|
|
80
|
+
if (val == null) {
|
|
81
|
+
return fallback;
|
|
82
|
+
}
|
|
83
|
+
if (_ZTrilean.is(val)) {
|
|
84
|
+
return val;
|
|
85
|
+
}
|
|
86
|
+
if (typeof val === "string") {
|
|
87
|
+
return _ZTrilean.parse(val, fallback);
|
|
88
|
+
}
|
|
89
|
+
return !!val;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
_ZTrilean.Indeterminate = Symbol("indeterminate");
|
|
93
|
+
let ZTrilean = _ZTrilean;
|
|
94
|
+
exports.ZTrilean = ZTrilean;
|
|
95
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/trilean.mts"],"sourcesContent":["/**\n * Represents a tri logic state.\n *\n * See {@link ZTrilean.Indeterminate} for what the symbol value should be.\n */\nexport type trilean = boolean | symbol;\n\n/**\n * A utility class for trilean values.\n */\nexport abstract class ZTrilean {\n /**\n * A unique value for the third, indeterminate, state.\n */\n public static readonly Indeterminate = Symbol('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 ZTrilean.isIndeterminate(x) ? x.description! : 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(str: string | null | undefined, fallback: trilean = false): 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 symbol {\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":";;AAUO,MAAe,YAAf,MAAe,UAAS;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgB7B,OAAc,UAAU,GAAoB;AAC1C,WAAO,UAAS,gBAAgB,CAAC,IAAI,EAAE,cAAe,OAAO,CAAC;AAAA,EAChE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcA,OAAc,MAAM,KAAgC,WAAoB,OAAgB;AACtF,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,KAA6B;AACzD,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;AAnGyB,UAAA,gBAAgB,OAAO,eAAe;AAJxD,IAAe,WAAf;;"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
const _ZTrilean = class _ZTrilean {
|
|
2
|
+
/**
|
|
3
|
+
* Converts a trilean value to a string.
|
|
4
|
+
*
|
|
5
|
+
* @param x -
|
|
6
|
+
* The value to convert.
|
|
7
|
+
*
|
|
8
|
+
* @returns
|
|
9
|
+
* A string of true if x is true, false if x is false,
|
|
10
|
+
* and indeterminate if x is the indeterminate symbol.
|
|
11
|
+
*/
|
|
12
|
+
static stringify(x) {
|
|
13
|
+
return _ZTrilean.isIndeterminate(x) ? x.description : String(x);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Converts a string to a trilean value.
|
|
17
|
+
*
|
|
18
|
+
* @param str -
|
|
19
|
+
* The text to parse.
|
|
20
|
+
* @param fallback -
|
|
21
|
+
* The fallback in the case that str is not a parsable value.
|
|
22
|
+
*
|
|
23
|
+
* @returns
|
|
24
|
+
* The parsed trilean value or fallback if str is not a valid
|
|
25
|
+
* trilean value.
|
|
26
|
+
*/
|
|
27
|
+
static parse(str, fallback = false) {
|
|
28
|
+
if ((str == null ? void 0 : str.toUpperCase().localeCompare("TRUE")) === 0) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
if ((str == null ? void 0 : str.toUpperCase().localeCompare("FALSE")) === 0) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
if ((str == null ? void 0 : str.toUpperCase().localeCompare("INDETERMINATE")) === 0) {
|
|
35
|
+
return _ZTrilean.Indeterminate;
|
|
36
|
+
}
|
|
37
|
+
return fallback;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Gets a value that determines if val is a trilean supported value.
|
|
41
|
+
*
|
|
42
|
+
* @param val -
|
|
43
|
+
* The value to test.
|
|
44
|
+
*
|
|
45
|
+
* @returns
|
|
46
|
+
* True if val is a trilean value. False otherwise.
|
|
47
|
+
*/
|
|
48
|
+
static is(val) {
|
|
49
|
+
return typeof val === "boolean" || _ZTrilean.isIndeterminate(val);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Gets whether val is the indeterminate value.
|
|
53
|
+
*/
|
|
54
|
+
static isIndeterminate(val) {
|
|
55
|
+
return val === _ZTrilean.Indeterminate;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Converts from a value to a trilean value.
|
|
59
|
+
*
|
|
60
|
+
* This is similar to parse, but it also supports non
|
|
61
|
+
* string inputs which will be converted to a boolean.
|
|
62
|
+
*
|
|
63
|
+
* @param val -
|
|
64
|
+
* The value to convert.
|
|
65
|
+
* @param fallback -
|
|
66
|
+
* The fallback value in the case that val cannot be converted. This will
|
|
67
|
+
* only be used in the case that val is a string, null, or undefined.
|
|
68
|
+
*
|
|
69
|
+
* @returns
|
|
70
|
+
* The trilean value of val. If val is null or undefined, then
|
|
71
|
+
* fallback is returned. If val is a string, then the return value of
|
|
72
|
+
* {@link parse} will be used. If val is a boolean, then it will be
|
|
73
|
+
* directly returned. If val is equal to {@link Indeterminate}, then
|
|
74
|
+
* {@link Indeterminate} will be returned. Otherwise, the truthy
|
|
75
|
+
* nature of value will be returned.
|
|
76
|
+
*/
|
|
77
|
+
static convert(val, fallback = false) {
|
|
78
|
+
if (val == null) {
|
|
79
|
+
return fallback;
|
|
80
|
+
}
|
|
81
|
+
if (_ZTrilean.is(val)) {
|
|
82
|
+
return val;
|
|
83
|
+
}
|
|
84
|
+
if (typeof val === "string") {
|
|
85
|
+
return _ZTrilean.parse(val, fallback);
|
|
86
|
+
}
|
|
87
|
+
return !!val;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
_ZTrilean.Indeterminate = Symbol("indeterminate");
|
|
91
|
+
let ZTrilean = _ZTrilean;
|
|
92
|
+
export {
|
|
93
|
+
ZTrilean
|
|
94
|
+
};
|
|
95
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/trilean.mts"],"sourcesContent":["/**\n * Represents a tri logic state.\n *\n * See {@link ZTrilean.Indeterminate} for what the symbol value should be.\n */\nexport type trilean = boolean | symbol;\n\n/**\n * A utility class for trilean values.\n */\nexport abstract class ZTrilean {\n /**\n * A unique value for the third, indeterminate, state.\n */\n public static readonly Indeterminate = Symbol('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 ZTrilean.isIndeterminate(x) ? x.description! : 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(str: string | null | undefined, fallback: trilean = false): 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 symbol {\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":"AAUO,MAAe,YAAf,MAAe,UAAS;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgB7B,OAAc,UAAU,GAAoB;AAC1C,WAAO,UAAS,gBAAgB,CAAC,IAAI,EAAE,cAAe,OAAO,CAAC;AAAA,EAChE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcA,OAAc,MAAM,KAAgC,WAAoB,OAAgB;AACtF,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,KAA6B;AACzD,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;AAnGyB,UAAA,gBAAgB,OAAO,eAAe;AAJxD,IAAe,WAAf;"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a tri logic state.
|
|
3
|
+
*
|
|
4
|
+
* See {@link ZTrilean.Indeterminate} for what the symbol value should be.
|
|
5
|
+
*/
|
|
6
|
+
export type trilean = boolean | symbol;
|
|
7
|
+
/**
|
|
8
|
+
* A utility class for trilean values.
|
|
9
|
+
*/
|
|
10
|
+
export declare abstract class ZTrilean {
|
|
11
|
+
/**
|
|
12
|
+
* A unique value for the third, indeterminate, state.
|
|
13
|
+
*/
|
|
14
|
+
static readonly Indeterminate: unique symbol;
|
|
15
|
+
/**
|
|
16
|
+
* Converts a trilean value to a string.
|
|
17
|
+
*
|
|
18
|
+
* @param x -
|
|
19
|
+
* The value to convert.
|
|
20
|
+
*
|
|
21
|
+
* @returns
|
|
22
|
+
* A string of true if x is true, false if x is false,
|
|
23
|
+
* and indeterminate if x is the indeterminate symbol.
|
|
24
|
+
*/
|
|
25
|
+
static stringify(x: trilean): string;
|
|
26
|
+
/**
|
|
27
|
+
* Converts a string to a trilean value.
|
|
28
|
+
*
|
|
29
|
+
* @param str -
|
|
30
|
+
* The text to parse.
|
|
31
|
+
* @param fallback -
|
|
32
|
+
* The fallback in the case that str is not a parsable value.
|
|
33
|
+
*
|
|
34
|
+
* @returns
|
|
35
|
+
* The parsed trilean value or fallback if str is not a valid
|
|
36
|
+
* trilean value.
|
|
37
|
+
*/
|
|
38
|
+
static parse(str: string | null | undefined, fallback?: trilean): trilean;
|
|
39
|
+
/**
|
|
40
|
+
* Gets a value that determines if val is a trilean supported value.
|
|
41
|
+
*
|
|
42
|
+
* @param val -
|
|
43
|
+
* The value to test.
|
|
44
|
+
*
|
|
45
|
+
* @returns
|
|
46
|
+
* True if val is a trilean value. False otherwise.
|
|
47
|
+
*/
|
|
48
|
+
static is(val: any): val is trilean;
|
|
49
|
+
/**
|
|
50
|
+
* Gets whether val is the indeterminate value.
|
|
51
|
+
*/
|
|
52
|
+
static isIndeterminate(val: trilean): val is symbol;
|
|
53
|
+
/**
|
|
54
|
+
* Converts from a value to a trilean value.
|
|
55
|
+
*
|
|
56
|
+
* This is similar to parse, but it also supports non
|
|
57
|
+
* string inputs which will be converted to a boolean.
|
|
58
|
+
*
|
|
59
|
+
* @param val -
|
|
60
|
+
* The value to convert.
|
|
61
|
+
* @param fallback -
|
|
62
|
+
* The fallback value in the case that val cannot be converted. This will
|
|
63
|
+
* only be used in the case that val is a string, null, or undefined.
|
|
64
|
+
*
|
|
65
|
+
* @returns
|
|
66
|
+
* The trilean value of val. If val is null or undefined, then
|
|
67
|
+
* fallback is returned. If val is a string, then the return value of
|
|
68
|
+
* {@link parse} will be used. If val is a boolean, then it will be
|
|
69
|
+
* directly returned. If val is equal to {@link Indeterminate}, then
|
|
70
|
+
* {@link Indeterminate} will be returned. Otherwise, the truthy
|
|
71
|
+
* nature of value will be returned.
|
|
72
|
+
*/
|
|
73
|
+
static convert(val: any, fallback?: trilean): trilean;
|
|
74
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zthun/trilean",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "A tri state boolean logic type.",
|
|
5
|
+
"author": "Anthony Bonta",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/zthun/trilean",
|
|
10
|
+
"directory": "packages/trilean"
|
|
11
|
+
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"main": "./dist/index.cjs",
|
|
14
|
+
"module": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
"require": "./dist/index.cjs",
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "vite build"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"typescript": "~5.4.3",
|
|
29
|
+
"vite": "^5.2.7",
|
|
30
|
+
"vitest": "^1.4.0",
|
|
31
|
+
"vitest-mock-extended": "^1.3.1"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
36
|
+
"sideEffects": false,
|
|
37
|
+
"gitHead": "c616fb580f4334703d6a5bdf4c1936d31a75aaff"
|
|
38
|
+
}
|