ts-data-forge 5.1.0 → 5.1.1
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 +46 -44
- package/dist/array/impl/array-utils-modification.d.mts.map +1 -1
- package/dist/array/impl/array-utils-modification.mjs.map +1 -1
- package/dist/array/impl/array-utils-reducing-value.d.mts +8 -8
- package/dist/array/impl/array-utils-search.d.mts +4 -4
- package/dist/array/impl/array-utils-set-op.d.mts +7 -7
- package/dist/array/impl/array-utils-set-op.mjs +7 -7
- package/dist/array/impl/array-utils-size.d.mts +2 -2
- package/dist/array/impl/array-utils-size.mjs +2 -2
- package/dist/array/impl/array-utils-slicing.d.mts +2 -2
- package/dist/array/impl/array-utils-slicing.mjs +2 -2
- package/dist/array/impl/array-utils-validation.d.mts +18 -18
- package/dist/array/impl/array-utils-validation.mjs +14 -14
- package/dist/collections/imap.d.mts +15 -15
- package/dist/collections/imap.mjs +3 -3
- package/dist/collections/iset-mapped.d.mts +19 -19
- package/dist/collections/iset-mapped.mjs +2 -2
- package/dist/collections/iset.d.mts +19 -19
- package/dist/collections/iset.mjs +2 -2
- package/dist/collections/queue.d.mts +25 -25
- package/dist/collections/queue.mjs +5 -5
- package/dist/collections/stack.d.mts +25 -25
- package/dist/collections/stack.mjs +5 -5
- package/dist/functional/match.d.mts +1 -1
- package/dist/functional/optional/impl/optional-expect-to-be.d.mts +2 -2
- package/dist/functional/optional/impl/optional-is-none.d.mts +1 -1
- package/dist/functional/optional/impl/optional-is-none.mjs +1 -1
- package/dist/functional/optional/impl/optional-is-optional.d.mts +2 -2
- package/dist/functional/optional/impl/optional-is-optional.mjs +2 -2
- package/dist/functional/optional/impl/optional-is-some.d.mts +1 -1
- package/dist/functional/optional/impl/optional-is-some.mjs +1 -1
- package/dist/functional/optional/impl/optional-none.d.mts +2 -2
- package/dist/functional/optional/impl/optional-none.mjs +2 -2
- package/dist/functional/optional/impl/optional-some.d.mts +2 -2
- package/dist/functional/optional/impl/optional-some.mjs +2 -2
- package/dist/functional/optional/impl/optional-to-nullable.d.mts +2 -2
- package/dist/functional/optional/impl/optional-to-nullable.mjs +2 -2
- package/dist/functional/optional/impl/optional-unwrap-or.d.mts +4 -4
- package/dist/functional/optional/impl/optional-unwrap-throw.d.mts +1 -1
- package/dist/functional/optional/impl/optional-unwrap-throw.mjs +1 -1
- package/dist/functional/optional/impl/optional-unwrap.d.mts +2 -2
- package/dist/functional/optional/impl/optional-zip.d.mts +1 -1
- package/dist/functional/optional/impl/optional-zip.mjs +1 -1
- package/dist/functional/pipe.d.mts +2 -2
- package/dist/functional/result/impl/result-err.d.mts +1 -1
- package/dist/functional/result/impl/result-err.mjs +1 -1
- package/dist/functional/result/impl/result-expect-to-be.d.mts +2 -2
- package/dist/functional/result/impl/result-from-promise.d.mts +1 -1
- package/dist/functional/result/impl/result-from-promise.mjs +1 -1
- package/dist/functional/result/impl/result-from-throwable.d.mts +1 -1
- package/dist/functional/result/impl/result-from-throwable.mjs +1 -1
- package/dist/functional/result/impl/result-is-err.d.mts +2 -2
- package/dist/functional/result/impl/result-is-err.mjs +2 -2
- package/dist/functional/result/impl/result-is-ok.d.mts +2 -2
- package/dist/functional/result/impl/result-is-ok.mjs +2 -2
- package/dist/functional/result/impl/result-is-result.d.mts +3 -3
- package/dist/functional/result/impl/result-is-result.mjs +3 -3
- package/dist/functional/result/impl/result-ok.d.mts +1 -1
- package/dist/functional/result/impl/result-ok.mjs +1 -1
- package/dist/functional/result/impl/result-unwrap-err-or.d.mts +4 -4
- package/dist/functional/result/impl/result-unwrap-err-throw.d.mts +1 -1
- package/dist/functional/result/impl/result-unwrap-err-throw.mjs +1 -1
- package/dist/functional/result/impl/result-unwrap-err.d.mts +2 -2
- package/dist/functional/result/impl/result-unwrap-err.mjs +2 -2
- package/dist/functional/result/impl/result-unwrap-ok-or.d.mts +4 -4
- package/dist/functional/result/impl/result-unwrap-ok.d.mts +2 -2
- package/dist/functional/result/impl/result-unwrap-throw.d.mts +1 -1
- package/dist/functional/result/impl/result-unwrap-throw.mjs +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-err.d.mts +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-err.mjs +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-promise.d.mts +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-promise.mjs +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-throwable.d.mts +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-from-throwable.mjs +1 -1
- package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.d.mts +3 -3
- package/dist/functional/ternary-result/impl/ternary-result-is-ternary-result.mjs +3 -3
- package/dist/globals.d.mts +2 -1
- package/dist/guard/has-key.d.mts +6 -3
- package/dist/guard/has-key.d.mts.map +1 -1
- package/dist/guard/has-key.mjs +2 -2
- package/dist/guard/key-is-in.d.mts +2 -2
- package/dist/guard/key-is-in.mjs +2 -2
- package/dist/json/json.d.mts +21 -21
- package/dist/json/json.mjs +21 -21
- package/dist/number/branded-types/int.d.mts +19 -19
- package/dist/number/branded-types/int.mjs +19 -19
- package/dist/number/branded-types/positive-int.d.mts +20 -20
- package/dist/number/branded-types/positive-int.mjs +20 -20
- package/dist/number/branded-types/positive-safe-int.d.mts +26 -26
- package/dist/number/branded-types/positive-safe-int.mjs +26 -26
- package/dist/number/branded-types/safe-int.d.mts +27 -27
- package/dist/number/branded-types/safe-int.mjs +27 -27
- package/dist/number/branded-types/uint.d.mts +19 -19
- package/dist/number/branded-types/uint.mjs +19 -19
- package/dist/number/num.d.mts +20 -20
- package/dist/number/num.mjs +16 -16
- package/dist/number/refined-number-utils.d.mts +11 -11
- package/dist/number/refined-number-utils.d.mts.map +1 -1
- package/dist/number/refined-number-utils.mjs +11 -11
- package/dist/object/object.d.mts +3 -3
- package/dist/object/object.d.mts.map +1 -1
- package/dist/object/object.mjs +3 -3
- package/dist/object/object.mjs.map +1 -1
- package/dist/others/tuple.d.mts +3 -3
- package/dist/others/tuple.mjs +3 -3
- package/dist/promise/promise.d.mts +4 -4
- package/dist/promise/promise.mjs +4 -4
- package/package.json +15 -13
- package/src/array/impl/array-utils-creation.test.mts +1 -1
- package/src/array/impl/array-utils-element-access.test.mts +16 -16
- package/src/array/impl/array-utils-modification.mts +38 -19
- package/src/array/impl/array-utils-reducing-value.mts +8 -8
- package/src/array/impl/array-utils-reducing-value.test.mts +23 -31
- package/src/array/impl/array-utils-search.mts +4 -4
- package/src/array/impl/array-utils-search.test.mts +10 -10
- package/src/array/impl/array-utils-set-op.mts +7 -7
- package/src/array/impl/array-utils-set-op.test.mts +13 -13
- package/src/array/impl/array-utils-size.mts +2 -2
- package/src/array/impl/array-utils-slicing.mts +2 -2
- package/src/array/impl/array-utils-transformation.test.mts +2 -2
- package/src/array/impl/array-utils-validation.mts +18 -18
- package/src/array/impl/array-utils-validation.test.mts +80 -78
- package/src/array/impl/array.test.mts +6 -6
- package/src/collections/imap-mapped.test.mts +16 -18
- package/src/collections/imap.mts +15 -15
- package/src/collections/imap.test.mts +31 -31
- package/src/collections/iset-mapped.mts +19 -19
- package/src/collections/iset-mapped.test.mts +52 -52
- package/src/collections/iset.mts +19 -19
- package/src/collections/iset.test.mts +130 -130
- package/src/collections/queue.mts +25 -25
- package/src/collections/queue.test.mts +33 -35
- package/src/collections/stack.mts +25 -25
- package/src/collections/stack.test.mts +15 -15
- package/src/functional/match.mts +1 -1
- package/src/functional/optional/impl/optional-expect-to-be.mts +2 -2
- package/src/functional/optional/impl/optional-is-none.mts +1 -1
- package/src/functional/optional/impl/optional-is-optional.mts +2 -2
- package/src/functional/optional/impl/optional-is-some.mts +1 -1
- package/src/functional/optional/impl/optional-none.mts +2 -2
- package/src/functional/optional/impl/optional-some.mts +2 -2
- package/src/functional/optional/impl/optional-to-nullable.mts +2 -2
- package/src/functional/optional/impl/optional-unwrap-or.mts +4 -4
- package/src/functional/optional/impl/optional-unwrap-throw.mts +1 -1
- package/src/functional/optional/impl/optional-unwrap.mts +2 -2
- package/src/functional/optional/impl/optional-zip.mts +1 -1
- package/src/functional/optional.test.mts +53 -54
- package/src/functional/pipe.mts +2 -2
- package/src/functional/pipe.test.mts +4 -4
- package/src/functional/result/impl/result-err.mts +1 -1
- package/src/functional/result/impl/result-expect-to-be.mts +2 -2
- package/src/functional/result/impl/result-from-promise.mts +1 -1
- package/src/functional/result/impl/result-from-throwable.mts +1 -1
- package/src/functional/result/impl/result-is-err.mts +2 -2
- package/src/functional/result/impl/result-is-ok.mts +2 -2
- package/src/functional/result/impl/result-is-result.mts +3 -3
- package/src/functional/result/impl/result-ok.mts +1 -1
- package/src/functional/result/impl/result-unwrap-err-or.mts +4 -4
- package/src/functional/result/impl/result-unwrap-err-throw.mts +1 -1
- package/src/functional/result/impl/result-unwrap-err.mts +2 -2
- package/src/functional/result/impl/result-unwrap-ok-or.mts +4 -4
- package/src/functional/result/impl/result-unwrap-ok.mts +2 -2
- package/src/functional/result/impl/result-unwrap-throw.mts +1 -1
- package/src/functional/result.test.mts +94 -152
- package/src/functional/ternary-result/impl/ternary-result-err.mts +1 -1
- package/src/functional/ternary-result/impl/ternary-result-from-promise.mts +1 -1
- package/src/functional/ternary-result/impl/ternary-result-from-throwable.mts +1 -1
- package/src/functional/ternary-result/impl/ternary-result-is-ternary-result.mts +3 -3
- package/src/functional/ternary-result.test.mts +8 -8
- package/src/globals.d.mts +2 -1
- package/src/guard/has-key.mts +8 -5
- package/src/guard/has-key.test.mts +0 -5
- package/src/guard/is-error.test.mts +143 -0
- package/src/guard/is-non-empty-string.test.mts +24 -24
- package/src/guard/is-non-null-object.test.mts +26 -26
- package/src/guard/is-primitive.test.mts +28 -28
- package/src/guard/is-record.test.mts +7 -7
- package/src/guard/is-type.test.mts +151 -155
- package/src/guard/key-is-in.mts +2 -2
- package/src/guard/key-is-in.test.mts +2 -2
- package/src/json/json.mts +21 -21
- package/src/json/json.test.mts +135 -174
- package/src/number/branded-types/finite-number.test.mts +13 -11
- package/src/number/branded-types/int.mts +19 -19
- package/src/number/branded-types/int.test.mts +14 -14
- package/src/number/branded-types/int16.test.mts +16 -16
- package/src/number/branded-types/int32.test.mts +16 -16
- package/src/number/branded-types/non-negative-finite-number.test.mts +12 -12
- package/src/number/branded-types/non-negative-int16.test.mts +16 -16
- package/src/number/branded-types/non-negative-int32.test.mts +16 -16
- package/src/number/branded-types/non-zero-finite-number.test.mts +14 -14
- package/src/number/branded-types/non-zero-int.test.mts +19 -19
- package/src/number/branded-types/non-zero-int16.test.mts +16 -16
- package/src/number/branded-types/non-zero-int32.test.mts +16 -16
- package/src/number/branded-types/non-zero-safe-int.test.mts +23 -23
- package/src/number/branded-types/non-zero-uint16.test.mts +16 -16
- package/src/number/branded-types/non-zero-uint32.test.mts +16 -16
- package/src/number/branded-types/positive-finite-number.test.mts +14 -14
- package/src/number/branded-types/positive-int.mts +20 -20
- package/src/number/branded-types/positive-int.test.mts +17 -17
- package/src/number/branded-types/positive-int16.test.mts +15 -15
- package/src/number/branded-types/positive-int32.test.mts +15 -15
- package/src/number/branded-types/positive-safe-int.mts +26 -26
- package/src/number/branded-types/positive-safe-int.test.mts +19 -19
- package/src/number/branded-types/positive-uint16.test.mts +16 -16
- package/src/number/branded-types/positive-uint32.test.mts +16 -16
- package/src/number/branded-types/safe-int.mts +27 -27
- package/src/number/branded-types/safe-int.test.mts +18 -18
- package/src/number/branded-types/safe-uint.test.mts +17 -17
- package/src/number/branded-types/uint.mts +19 -19
- package/src/number/branded-types/uint.test.mts +15 -15
- package/src/number/branded-types/uint16.test.mts +15 -15
- package/src/number/branded-types/uint32.test.mts +15 -15
- package/src/number/enum/int8.test.mts +1 -1
- package/src/number/enum/uint8.test.mts +1 -1
- package/src/number/num.mts +20 -20
- package/src/number/num.test.mts +26 -26
- package/src/number/refined-number-utils.mts +13 -13
- package/src/object/object.mts +4 -3
- package/src/object/object.test.mts +6 -6
- package/src/others/cast-mutable.test.mts +1 -1
- package/src/others/cast-readonly.test.mts +4 -4
- package/src/others/if-then.test.mts +24 -24
- package/src/others/tuple.mts +3 -3
- package/src/promise/promise.mts +4 -4
- package/src/promise/promise.test.mts +7 -13
|
@@ -19,11 +19,11 @@ import { type UnwrapErr } from './types.mjs';
|
|
|
19
19
|
* // Result.unwrapErr returns undefined for Ok results
|
|
20
20
|
*
|
|
21
21
|
* // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
|
|
22
|
-
* assert(Result.unwrapErr(okResult) === undefined);
|
|
22
|
+
* assert.isTrue(Result.unwrapErr(okResult) === undefined);
|
|
23
23
|
*
|
|
24
24
|
* // Result.unwrapErr returns the error value for Err results
|
|
25
25
|
*
|
|
26
|
-
* assert(Result.unwrapErr(errResult) === 'problem');
|
|
26
|
+
* assert.isTrue(Result.unwrapErr(errResult) === 'problem');
|
|
27
27
|
* ```
|
|
28
28
|
*
|
|
29
29
|
* @template R The `UnknownResult` type to unwrap.
|
|
@@ -12,15 +12,15 @@ import { type UnwrapOk } from './types.mjs';
|
|
|
12
12
|
*
|
|
13
13
|
* const errValue = Result.err('fail');
|
|
14
14
|
*
|
|
15
|
-
* assert(Result.unwrapOkOr(okValue, 0) === 10);
|
|
15
|
+
* assert.isTrue(Result.unwrapOkOr(okValue, 0) === 10);
|
|
16
16
|
*
|
|
17
|
-
* assert(Result.unwrapOkOr(errValue, 0) === 0);
|
|
17
|
+
* assert.isTrue(Result.unwrapOkOr(errValue, 0) === 0);
|
|
18
18
|
*
|
|
19
19
|
* const unwrapWithDefault = Result.unwrapOkOr(5);
|
|
20
20
|
*
|
|
21
|
-
* assert(unwrapWithDefault(Result.ok(3)) === 3);
|
|
21
|
+
* assert.isTrue(unwrapWithDefault(Result.ok(3)) === 3);
|
|
22
22
|
*
|
|
23
|
-
* assert(unwrapWithDefault(Result.err('no data')) === 5);
|
|
23
|
+
* assert.isTrue(unwrapWithDefault(Result.err('no data')) === 5);
|
|
24
24
|
* ```
|
|
25
25
|
*
|
|
26
26
|
* @template R The `UnknownResult` type to unwrap.
|
|
@@ -13,12 +13,12 @@ import { type UnwrapOk } from './types.mjs';
|
|
|
13
13
|
*
|
|
14
14
|
* // Result.unwrapOk returns the value for Ok results
|
|
15
15
|
*
|
|
16
|
-
* assert(Result.unwrapOk(okResult) === 42);
|
|
16
|
+
* assert.isTrue(Result.unwrapOk(okResult) === 42);
|
|
17
17
|
*
|
|
18
18
|
* // Result.unwrapOk returns undefined for Err results
|
|
19
19
|
*
|
|
20
20
|
* // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
|
|
21
|
-
* assert(Result.unwrapOk(errResult) === undefined);
|
|
21
|
+
* assert.isTrue(Result.unwrapOk(errResult) === undefined);
|
|
22
22
|
* ```
|
|
23
23
|
*/
|
|
24
24
|
export function unwrapOk<R extends Ok<unknown>>(result: R): UnwrapOk<R>;
|
|
@@ -18,7 +18,7 @@ import { type UnwrapErr, type UnwrapOk } from './types.mjs';
|
|
|
18
18
|
*
|
|
19
19
|
* const errResult = Result.err(new Error('fail'));
|
|
20
20
|
*
|
|
21
|
-
* assert(Result.unwrapThrow(okResult) === 'data');
|
|
21
|
+
* assert.isTrue(Result.unwrapThrow(okResult) === 'data');
|
|
22
22
|
*
|
|
23
23
|
* assert.throws(() => Result.unwrapThrow(errResult), /fail/u);
|
|
24
24
|
* ```
|
|
@@ -8,9 +8,9 @@ describe('Result test', () => {
|
|
|
8
8
|
test('creates Ok result', () => {
|
|
9
9
|
const result = Result.ok(42);
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
assert.isTrue(Result.isOk(result));
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
assert.isFalse(Result.isErr(result));
|
|
14
14
|
|
|
15
15
|
expect(result.value).toBe(42);
|
|
16
16
|
|
|
@@ -20,7 +20,7 @@ describe('Result test', () => {
|
|
|
20
20
|
test('creates Ok result with string', () => {
|
|
21
21
|
const result = Result.ok('success');
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
assert.isTrue(Result.isOk(result));
|
|
24
24
|
|
|
25
25
|
expect(result.value).toBe('success');
|
|
26
26
|
|
|
@@ -32,9 +32,9 @@ describe('Result test', () => {
|
|
|
32
32
|
test('creates Err result', () => {
|
|
33
33
|
const result = Result.err('error message');
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
assert.isTrue(Result.isErr(result));
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
assert.isFalse(Result.isOk(result));
|
|
38
38
|
|
|
39
39
|
expect(result.value).toBe('error message');
|
|
40
40
|
|
|
@@ -44,7 +44,7 @@ describe('Result test', () => {
|
|
|
44
44
|
test('creates Err result with number', () => {
|
|
45
45
|
const result = Result.err(404);
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
assert.isTrue(Result.isErr(result));
|
|
48
48
|
|
|
49
49
|
expect(result.value).toBe(404);
|
|
50
50
|
|
|
@@ -66,7 +66,7 @@ describe('Result test', () => {
|
|
|
66
66
|
test('returns false for Err results', () => {
|
|
67
67
|
const result: Result<number, string> = Result.err('error');
|
|
68
68
|
|
|
69
|
-
|
|
69
|
+
assert.isFalse(Result.isOk(result));
|
|
70
70
|
});
|
|
71
71
|
});
|
|
72
72
|
|
|
@@ -84,7 +84,7 @@ describe('Result test', () => {
|
|
|
84
84
|
test('returns false for Ok results', () => {
|
|
85
85
|
const result: Result<number, string> = Result.ok(42);
|
|
86
86
|
|
|
87
|
-
|
|
87
|
+
assert.isFalse(Result.isErr(result));
|
|
88
88
|
});
|
|
89
89
|
});
|
|
90
90
|
|
|
@@ -92,27 +92,27 @@ describe('Result test', () => {
|
|
|
92
92
|
test('recognizes Ok results', () => {
|
|
93
93
|
const result = Result.ok(42);
|
|
94
94
|
|
|
95
|
-
|
|
95
|
+
assert.isTrue(Result.isResult(result));
|
|
96
96
|
});
|
|
97
97
|
|
|
98
98
|
test('recognizes Err results', () => {
|
|
99
99
|
const result = Result.err('error');
|
|
100
100
|
|
|
101
|
-
|
|
101
|
+
assert.isTrue(Result.isResult(result));
|
|
102
102
|
});
|
|
103
103
|
|
|
104
104
|
test('rejects non-Result values', () => {
|
|
105
|
-
|
|
105
|
+
assert.isFalse(Result.isResult(42));
|
|
106
106
|
|
|
107
|
-
|
|
107
|
+
assert.isFalse(Result.isResult('string'));
|
|
108
108
|
|
|
109
|
-
|
|
109
|
+
assert.isFalse(Result.isResult(null));
|
|
110
110
|
|
|
111
|
-
|
|
111
|
+
assert.isFalse(Result.isResult(undefined));
|
|
112
112
|
|
|
113
|
-
|
|
113
|
+
assert.isFalse(Result.isResult({}));
|
|
114
114
|
|
|
115
|
-
|
|
115
|
+
assert.isFalse(Result.isResult({ type: 'unknown', value: 42 }));
|
|
116
116
|
});
|
|
117
117
|
});
|
|
118
118
|
|
|
@@ -122,11 +122,9 @@ describe('Result test', () => {
|
|
|
122
122
|
|
|
123
123
|
const mapped = Result.map(result, (x) => x * 2);
|
|
124
124
|
|
|
125
|
-
|
|
125
|
+
assert.isTrue(Result.isOk(mapped));
|
|
126
126
|
|
|
127
|
-
|
|
128
|
-
expect(mapped.value).toBe(10);
|
|
129
|
-
}
|
|
127
|
+
expect(mapped.value).toBe(10);
|
|
130
128
|
|
|
131
129
|
expectType<typeof mapped, Result<number, never>>('<=');
|
|
132
130
|
});
|
|
@@ -136,11 +134,9 @@ describe('Result test', () => {
|
|
|
136
134
|
|
|
137
135
|
const mapped = Result.map(result, (x) => x * 2);
|
|
138
136
|
|
|
139
|
-
|
|
137
|
+
assert.isTrue(Result.isErr(mapped));
|
|
140
138
|
|
|
141
|
-
|
|
142
|
-
expect(mapped.value).toBe('error');
|
|
143
|
-
}
|
|
139
|
+
expect(mapped.value).toBe('error');
|
|
144
140
|
|
|
145
141
|
expectType<typeof mapped, Result<number, string>>('<=');
|
|
146
142
|
});
|
|
@@ -152,21 +148,17 @@ describe('Result test', () => {
|
|
|
152
148
|
|
|
153
149
|
const mapped = doubler(okResult);
|
|
154
150
|
|
|
155
|
-
|
|
151
|
+
assert.isTrue(Result.isOk(mapped));
|
|
156
152
|
|
|
157
|
-
|
|
158
|
-
expect(mapped.value).toBe(10);
|
|
159
|
-
}
|
|
153
|
+
expect(mapped.value).toBe(10);
|
|
160
154
|
|
|
161
155
|
const errResult: Result<number, string> = Result.err('error');
|
|
162
156
|
|
|
163
157
|
const mappedErr = doubler(errResult);
|
|
164
158
|
|
|
165
|
-
|
|
159
|
+
assert.isTrue(Result.isErr(mappedErr));
|
|
166
160
|
|
|
167
|
-
|
|
168
|
-
expect(mappedErr.value).toBe('error');
|
|
169
|
-
}
|
|
161
|
+
expect(mappedErr.value).toBe('error');
|
|
170
162
|
});
|
|
171
163
|
|
|
172
164
|
test('should work with pipe when curried', () => {
|
|
@@ -176,11 +168,9 @@ describe('Result test', () => {
|
|
|
176
168
|
|
|
177
169
|
const result = pipe(Result.ok(5)).map(doubler).map(toStringFn).value;
|
|
178
170
|
|
|
179
|
-
|
|
171
|
+
assert.isTrue(Result.isOk(result));
|
|
180
172
|
|
|
181
|
-
|
|
182
|
-
expect(result.value).toBe('10');
|
|
183
|
-
}
|
|
173
|
+
expect(result.value).toBe('10');
|
|
184
174
|
});
|
|
185
175
|
});
|
|
186
176
|
|
|
@@ -190,11 +180,9 @@ describe('Result test', () => {
|
|
|
190
180
|
|
|
191
181
|
const mapped = Result.mapErr(result, (e) => e.toUpperCase());
|
|
192
182
|
|
|
193
|
-
|
|
183
|
+
assert.isTrue(Result.isErr(mapped));
|
|
194
184
|
|
|
195
|
-
|
|
196
|
-
expect(mapped.value).toBe('ERROR');
|
|
197
|
-
}
|
|
185
|
+
expect(mapped.value).toBe('ERROR');
|
|
198
186
|
|
|
199
187
|
expectType<typeof mapped, Result<number, string>>('<=');
|
|
200
188
|
});
|
|
@@ -204,13 +192,11 @@ describe('Result test', () => {
|
|
|
204
192
|
|
|
205
193
|
const mapped = Result.mapErr(result, (e: string) => e.toUpperCase());
|
|
206
194
|
|
|
207
|
-
|
|
195
|
+
assert.isTrue(Result.isOk(mapped));
|
|
208
196
|
|
|
209
|
-
|
|
210
|
-
expect(mapped.value).toBe(42);
|
|
211
|
-
}
|
|
197
|
+
expect(mapped.value).toBe(42);
|
|
212
198
|
|
|
213
|
-
expectType<typeof mapped,
|
|
199
|
+
expectType<typeof mapped, Ok<number>>('=');
|
|
214
200
|
});
|
|
215
201
|
|
|
216
202
|
test('should support curried form', () => {
|
|
@@ -220,21 +206,17 @@ describe('Result test', () => {
|
|
|
220
206
|
|
|
221
207
|
const mapped = errorUppercase(errResult);
|
|
222
208
|
|
|
223
|
-
|
|
209
|
+
assert.isTrue(Result.isErr(mapped));
|
|
224
210
|
|
|
225
|
-
|
|
226
|
-
expect(mapped.value).toBe('ERROR');
|
|
227
|
-
}
|
|
211
|
+
expect(mapped.value).toBe('ERROR');
|
|
228
212
|
|
|
229
213
|
const okResult: Result<number, string> = Result.ok(42);
|
|
230
214
|
|
|
231
215
|
const mappedOk = errorUppercase(okResult);
|
|
232
216
|
|
|
233
|
-
|
|
217
|
+
assert.isTrue(Result.isOk(mappedOk));
|
|
234
218
|
|
|
235
|
-
|
|
236
|
-
expect(mappedOk.value).toBe(42);
|
|
237
|
-
}
|
|
219
|
+
expect(mappedOk.value).toBe(42);
|
|
238
220
|
});
|
|
239
221
|
|
|
240
222
|
test('should work with pipe when curried', () => {
|
|
@@ -246,11 +228,9 @@ describe('Result test', () => {
|
|
|
246
228
|
.map(errorUppercase)
|
|
247
229
|
.map(errorPrefix).value;
|
|
248
230
|
|
|
249
|
-
|
|
231
|
+
assert.isTrue(Result.isErr(result));
|
|
250
232
|
|
|
251
|
-
|
|
252
|
-
expect(result.value).toBe('ERROR: FAILED');
|
|
253
|
-
}
|
|
233
|
+
expect(result.value).toBe('ERROR: FAILED');
|
|
254
234
|
});
|
|
255
235
|
});
|
|
256
236
|
|
|
@@ -357,13 +337,11 @@ describe('Result test', () => {
|
|
|
357
337
|
() => 0,
|
|
358
338
|
);
|
|
359
339
|
|
|
360
|
-
|
|
340
|
+
assert.isTrue(Result.isOk(folded));
|
|
361
341
|
|
|
362
|
-
|
|
363
|
-
expect(folded.value).toBe(84);
|
|
364
|
-
}
|
|
342
|
+
expect(folded.value).toBe(84);
|
|
365
343
|
|
|
366
|
-
expectType<typeof folded,
|
|
344
|
+
expectType<typeof folded, Ok<number>>('=');
|
|
367
345
|
});
|
|
368
346
|
|
|
369
347
|
test('folds Err result', () => {
|
|
@@ -375,13 +353,11 @@ describe('Result test', () => {
|
|
|
375
353
|
(e) => e.length,
|
|
376
354
|
);
|
|
377
355
|
|
|
378
|
-
|
|
356
|
+
assert.isTrue(Result.isErr(folded));
|
|
379
357
|
|
|
380
|
-
|
|
381
|
-
expect(folded.value).toBe(5); // length of 'error'
|
|
382
|
-
}
|
|
358
|
+
expect(folded.value).toBe(5); // length of 'error'
|
|
383
359
|
|
|
384
|
-
expectType<typeof folded,
|
|
360
|
+
expectType<typeof folded, Err<number>>('=');
|
|
385
361
|
});
|
|
386
362
|
|
|
387
363
|
test('should support curried form', () => {
|
|
@@ -394,21 +370,17 @@ describe('Result test', () => {
|
|
|
394
370
|
|
|
395
371
|
const foldedOk = folder(okResult);
|
|
396
372
|
|
|
397
|
-
|
|
373
|
+
assert.isTrue(Result.isOk(foldedOk));
|
|
398
374
|
|
|
399
|
-
|
|
400
|
-
expect(foldedOk.value).toBe(84);
|
|
401
|
-
}
|
|
375
|
+
expect(foldedOk.value).toBe(84);
|
|
402
376
|
|
|
403
377
|
const errResult: Result<number, string> = Result.err('error');
|
|
404
378
|
|
|
405
379
|
const foldedErr = folder(errResult);
|
|
406
380
|
|
|
407
|
-
|
|
381
|
+
assert.isTrue(Result.isErr(foldedErr));
|
|
408
382
|
|
|
409
|
-
|
|
410
|
-
expect(foldedErr.value).toBe(5);
|
|
411
|
-
}
|
|
383
|
+
expect(foldedErr.value).toBe(5);
|
|
412
384
|
});
|
|
413
385
|
|
|
414
386
|
test('should work with pipe when curried', () => {
|
|
@@ -419,19 +391,15 @@ describe('Result test', () => {
|
|
|
419
391
|
|
|
420
392
|
const result = pipe(Result.ok(21)).map(folder).value;
|
|
421
393
|
|
|
422
|
-
|
|
394
|
+
assert.isTrue(Result.isOk(result));
|
|
423
395
|
|
|
424
|
-
|
|
425
|
-
expect(result.value).toBe(42);
|
|
426
|
-
}
|
|
396
|
+
expect(result.value).toBe(42);
|
|
427
397
|
|
|
428
398
|
const errorResult = pipe(Result.err('error')).map(folder).value;
|
|
429
399
|
|
|
430
|
-
|
|
400
|
+
assert.isTrue(Result.isErr(errorResult));
|
|
431
401
|
|
|
432
|
-
|
|
433
|
-
expect(errorResult.value).toBe(0);
|
|
434
|
-
}
|
|
402
|
+
expect(errorResult.value).toBe(0);
|
|
435
403
|
});
|
|
436
404
|
});
|
|
437
405
|
|
|
@@ -442,7 +410,7 @@ describe('Result test', () => {
|
|
|
442
410
|
|
|
443
411
|
const result = await Result.fromPromise(asyncFn());
|
|
444
412
|
|
|
445
|
-
|
|
413
|
+
assert.isTrue(Result.isOk(result));
|
|
446
414
|
|
|
447
415
|
expect(Result.unwrapOk(result)).toBe(42);
|
|
448
416
|
});
|
|
@@ -455,7 +423,7 @@ describe('Result test', () => {
|
|
|
455
423
|
|
|
456
424
|
const result = await Result.fromPromise(asyncFn());
|
|
457
425
|
|
|
458
|
-
|
|
426
|
+
assert.isTrue(Result.isErr(result));
|
|
459
427
|
|
|
460
428
|
expect(Result.unwrapErr(result)).toBe(error);
|
|
461
429
|
});
|
|
@@ -528,29 +496,23 @@ describe('Result test', () => {
|
|
|
528
496
|
|
|
529
497
|
const result = divideBy2(okResult);
|
|
530
498
|
|
|
531
|
-
|
|
499
|
+
assert.isTrue(Result.isOk(result));
|
|
532
500
|
|
|
533
|
-
|
|
534
|
-
expect(result.value).toBe(5);
|
|
535
|
-
}
|
|
501
|
+
expect(result.value).toBe(5);
|
|
536
502
|
|
|
537
503
|
const divideByZero = Result.flatMap((x: number) => divide(x, 0));
|
|
538
504
|
|
|
539
505
|
const errorResult = divideByZero(Result.ok(10));
|
|
540
506
|
|
|
541
|
-
|
|
507
|
+
assert.isTrue(Result.isErr(errorResult));
|
|
542
508
|
|
|
543
|
-
|
|
544
|
-
expect(errorResult.value).toBe('Division by zero');
|
|
545
|
-
}
|
|
509
|
+
expect(errorResult.value).toBe('Division by zero');
|
|
546
510
|
|
|
547
511
|
const initialError = divideBy2(Result.err('initial error'));
|
|
548
512
|
|
|
549
|
-
|
|
513
|
+
assert.isTrue(Result.isErr(initialError));
|
|
550
514
|
|
|
551
|
-
|
|
552
|
-
expect(initialError.value).toBe('initial error');
|
|
553
|
-
}
|
|
515
|
+
expect(initialError.value).toBe('initial error');
|
|
554
516
|
});
|
|
555
517
|
|
|
556
518
|
test('should work with pipe when curried', () => {
|
|
@@ -569,11 +531,9 @@ describe('Result test', () => {
|
|
|
569
531
|
|
|
570
532
|
const result = pipe(Result.ok('42')).map(parser).map(doubler).value;
|
|
571
533
|
|
|
572
|
-
|
|
534
|
+
assert.isTrue(Result.isOk(result));
|
|
573
535
|
|
|
574
|
-
|
|
575
|
-
expect(result.value).toBe(84);
|
|
576
|
-
}
|
|
536
|
+
expect(result.value).toBe(84);
|
|
577
537
|
});
|
|
578
538
|
});
|
|
579
539
|
|
|
@@ -583,7 +543,7 @@ describe('Result test', () => {
|
|
|
583
543
|
|
|
584
544
|
const swapped = Result.swap(okResult);
|
|
585
545
|
|
|
586
|
-
|
|
546
|
+
assert.isTrue(Result.isErr(swapped));
|
|
587
547
|
|
|
588
548
|
expect(Result.unwrapErr(swapped)).toBe(42);
|
|
589
549
|
});
|
|
@@ -593,7 +553,7 @@ describe('Result test', () => {
|
|
|
593
553
|
|
|
594
554
|
const swapped = Result.swap(errResult);
|
|
595
555
|
|
|
596
|
-
|
|
556
|
+
assert.isTrue(Result.isOk(swapped));
|
|
597
557
|
|
|
598
558
|
expect(Result.unwrapOk(swapped)).toBe('error');
|
|
599
559
|
});
|
|
@@ -605,7 +565,7 @@ describe('Result test', () => {
|
|
|
605
565
|
|
|
606
566
|
const optional = Result.toOptional(okResult);
|
|
607
567
|
|
|
608
|
-
|
|
568
|
+
assert.isTrue(Optional.isSome(optional));
|
|
609
569
|
|
|
610
570
|
expect(Optional.unwrapThrow(optional)).toBe(42);
|
|
611
571
|
});
|
|
@@ -615,7 +575,7 @@ describe('Result test', () => {
|
|
|
615
575
|
|
|
616
576
|
const optional = Result.toOptional(errResult);
|
|
617
577
|
|
|
618
|
-
|
|
578
|
+
assert.isTrue(Optional.isNone(optional));
|
|
619
579
|
});
|
|
620
580
|
});
|
|
621
581
|
|
|
@@ -775,21 +735,17 @@ describe('Result test', () => {
|
|
|
775
735
|
|
|
776
736
|
const result = fallbackTo(okResult);
|
|
777
737
|
|
|
778
|
-
|
|
738
|
+
assert.isTrue(Result.isOk(result));
|
|
779
739
|
|
|
780
|
-
|
|
781
|
-
expect(result.value).toBe('primary');
|
|
782
|
-
}
|
|
740
|
+
expect(result.value).toBe('primary');
|
|
783
741
|
|
|
784
742
|
const errResult: Result<string, string> = Result.err('failed');
|
|
785
743
|
|
|
786
744
|
const fallbackResult = fallbackTo(errResult);
|
|
787
745
|
|
|
788
|
-
|
|
746
|
+
assert.isTrue(Result.isOk(fallbackResult));
|
|
789
747
|
|
|
790
|
-
|
|
791
|
-
expect(fallbackResult.value).toBe('fallback');
|
|
792
|
-
}
|
|
748
|
+
expect(fallbackResult.value).toBe('fallback');
|
|
793
749
|
});
|
|
794
750
|
|
|
795
751
|
test('should work with pipe when curried', () => {
|
|
@@ -797,19 +753,15 @@ describe('Result test', () => {
|
|
|
797
753
|
|
|
798
754
|
const okResult = pipe(Result.ok('original')).map(fallbackTo).value;
|
|
799
755
|
|
|
800
|
-
|
|
756
|
+
assert.isTrue(Result.isOk(okResult));
|
|
801
757
|
|
|
802
|
-
|
|
803
|
-
expect(okResult.value).toBe('original');
|
|
804
|
-
}
|
|
758
|
+
expect(okResult.value).toBe('original');
|
|
805
759
|
|
|
806
760
|
const errResult = pipe(Result.err('network error')).map(fallbackTo).value;
|
|
807
761
|
|
|
808
|
-
|
|
762
|
+
assert.isTrue(Result.isOk(errResult));
|
|
809
763
|
|
|
810
|
-
|
|
811
|
-
expect(errResult.value).toBe('backup');
|
|
812
|
-
}
|
|
764
|
+
expect(errResult.value).toBe('backup');
|
|
813
765
|
});
|
|
814
766
|
});
|
|
815
767
|
|
|
@@ -859,7 +811,7 @@ describe('Result test', () => {
|
|
|
859
811
|
test('should return Ok when function succeeds', () => {
|
|
860
812
|
const result = Result.fromThrowable(() => 42);
|
|
861
813
|
|
|
862
|
-
|
|
814
|
+
assert.isTrue(Result.isOk(result));
|
|
863
815
|
|
|
864
816
|
expect(Result.unwrapOk(result)).toBe(42);
|
|
865
817
|
|
|
@@ -871,7 +823,7 @@ describe('Result test', () => {
|
|
|
871
823
|
|
|
872
824
|
const result = Result.fromThrowable(() => obj);
|
|
873
825
|
|
|
874
|
-
|
|
826
|
+
assert.isTrue(Result.isOk(result));
|
|
875
827
|
|
|
876
828
|
assert.deepStrictEqual(Result.unwrapOk(result), obj);
|
|
877
829
|
});
|
|
@@ -883,15 +835,13 @@ describe('Result test', () => {
|
|
|
883
835
|
throw new Error(errorMessage);
|
|
884
836
|
});
|
|
885
837
|
|
|
886
|
-
|
|
838
|
+
assert.isTrue(Result.isErr(result));
|
|
887
839
|
|
|
888
|
-
|
|
889
|
-
const error = result.value;
|
|
840
|
+
const error = result.value;
|
|
890
841
|
|
|
891
|
-
|
|
842
|
+
expect(error).toBeInstanceOf(Error);
|
|
892
843
|
|
|
893
|
-
|
|
894
|
-
}
|
|
844
|
+
expect(error.message).toBe(errorMessage);
|
|
895
845
|
});
|
|
896
846
|
|
|
897
847
|
test('should return Err when function throws string', () => {
|
|
@@ -902,15 +852,13 @@ describe('Result test', () => {
|
|
|
902
852
|
throw errorMessage;
|
|
903
853
|
});
|
|
904
854
|
|
|
905
|
-
|
|
855
|
+
assert.isTrue(Result.isErr(result));
|
|
906
856
|
|
|
907
|
-
|
|
908
|
-
const error = result.value;
|
|
857
|
+
const error = result.value;
|
|
909
858
|
|
|
910
|
-
|
|
859
|
+
expect(error).toBeInstanceOf(Error);
|
|
911
860
|
|
|
912
|
-
|
|
913
|
-
}
|
|
861
|
+
expect(error.message).toBe(errorMessage);
|
|
914
862
|
});
|
|
915
863
|
|
|
916
864
|
test('should return Err when function throws non-string primitive', () => {
|
|
@@ -919,15 +867,13 @@ describe('Result test', () => {
|
|
|
919
867
|
throw 404;
|
|
920
868
|
});
|
|
921
869
|
|
|
922
|
-
|
|
870
|
+
assert.isTrue(Result.isErr(result));
|
|
923
871
|
|
|
924
|
-
|
|
925
|
-
const error = result.value;
|
|
872
|
+
const error = result.value;
|
|
926
873
|
|
|
927
|
-
|
|
874
|
+
expect(error).toBeInstanceOf(Error);
|
|
928
875
|
|
|
929
|
-
|
|
930
|
-
}
|
|
876
|
+
expect(error.message).toBe('404');
|
|
931
877
|
});
|
|
932
878
|
|
|
933
879
|
test('should work with JSON.parse', () => {
|
|
@@ -938,20 +884,18 @@ describe('Result test', () => {
|
|
|
938
884
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
939
885
|
const validResult = Result.fromThrowable(() => JSON.parse(validJson));
|
|
940
886
|
|
|
941
|
-
|
|
887
|
+
assert.isTrue(Result.isOk(validResult));
|
|
942
888
|
|
|
943
889
|
assert.deepStrictEqual(Result.unwrapOk(validResult), { key: 'value' });
|
|
944
890
|
|
|
945
891
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
946
892
|
const invalidResult = Result.fromThrowable(() => JSON.parse(invalidJson));
|
|
947
893
|
|
|
948
|
-
|
|
894
|
+
assert.isTrue(Result.isErr(invalidResult));
|
|
949
895
|
|
|
950
|
-
|
|
951
|
-
const error = invalidResult.value;
|
|
896
|
+
const error = invalidResult.value;
|
|
952
897
|
|
|
953
|
-
|
|
954
|
-
}
|
|
898
|
+
expect(error).toBeInstanceOf(Error);
|
|
955
899
|
});
|
|
956
900
|
|
|
957
901
|
test('should work with array access', () => {
|
|
@@ -970,13 +914,11 @@ describe('Result test', () => {
|
|
|
970
914
|
return value;
|
|
971
915
|
});
|
|
972
916
|
|
|
973
|
-
|
|
917
|
+
assert.isTrue(Result.isErr(result));
|
|
974
918
|
|
|
975
|
-
|
|
976
|
-
const error = result.value;
|
|
919
|
+
const error = result.value;
|
|
977
920
|
|
|
978
|
-
|
|
979
|
-
}
|
|
921
|
+
expect(error.message).toBe('Index out of bounds');
|
|
980
922
|
});
|
|
981
923
|
|
|
982
924
|
test('should preserve function return type', () => {
|
|
@@ -10,7 +10,7 @@ import { ErrTypeTagName } from './tag.mjs';
|
|
|
10
10
|
*
|
|
11
11
|
* assert.strictEqual(failure.$$tag, 'ts-data-forge::Result.err');
|
|
12
12
|
*
|
|
13
|
-
* assert.
|
|
13
|
+
* assert.isTrue(TernaryResult.isErr(failure));
|
|
14
14
|
* ```
|
|
15
15
|
*/
|
|
16
16
|
export const err = <const E,>(value: E): TernaryErr<E> => ({
|
|
@@ -19,7 +19,7 @@ type UnwrapPromise<P extends Promise<unknown>> =
|
|
|
19
19
|
*
|
|
20
20
|
* assert.deepStrictEqual(resolved, TernaryResult.ok('ok'));
|
|
21
21
|
*
|
|
22
|
-
* assert.
|
|
22
|
+
* assert.isTrue(TernaryResult.isErr(rejected));
|
|
23
23
|
* ```
|
|
24
24
|
*/
|
|
25
25
|
export const fromPromise = <P extends Promise<unknown>>(
|
|
@@ -17,7 +17,7 @@ import { ok } from './ternary-result-ok.mjs';
|
|
|
17
17
|
*
|
|
18
18
|
* assert.deepStrictEqual(success, TernaryResult.ok(2));
|
|
19
19
|
*
|
|
20
|
-
* assert.
|
|
20
|
+
* assert.isTrue(TernaryResult.isErr(failure));
|
|
21
21
|
* ```
|
|
22
22
|
*/
|
|
23
23
|
export const fromThrowable = <T,>(
|