ts-data-forge 5.1.0 → 6.0.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/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/is-record.d.mts.map +1 -1
- package/dist/guard/is-record.mjs +1 -1
- package/dist/guard/is-record.mjs.map +1 -1
- 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.mts +1 -2
- package/src/guard/is-record.test.mts +57 -67
- 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
|
@@ -13,11 +13,11 @@ import { ErrTypeTagName, OkTypeTagName, WarnTypeTagName } from './tag.mjs';
|
|
|
13
13
|
*
|
|
14
14
|
* const notResult = { $$tag: 'ts-data-forge::Result.ok' };
|
|
15
15
|
*
|
|
16
|
-
* assert.
|
|
16
|
+
* assert.isTrue(TernaryResult.isTernaryResult(okValue));
|
|
17
17
|
*
|
|
18
|
-
* assert.
|
|
18
|
+
* assert.isTrue(TernaryResult.isTernaryResult(warnValue));
|
|
19
19
|
*
|
|
20
|
-
* assert.
|
|
20
|
+
* assert.isFalse(TernaryResult.isTernaryResult(notResult));
|
|
21
21
|
* ```
|
|
22
22
|
*/
|
|
23
23
|
export const isTernaryResult = (
|
|
@@ -10,15 +10,15 @@ describe('TernaryResult test', () => {
|
|
|
10
10
|
|
|
11
11
|
const err = TernaryResult.err(new Error('boom'));
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
assert.isTrue(TernaryResult.isOk(ok));
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
assert.isTrue(TernaryResult.isWarn(warn));
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
assert.isTrue(TernaryResult.isErr(err));
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
assert.isTrue(TernaryResult.isTernaryResult(ok));
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
assert.isFalse(TernaryResult.isTernaryResult({}));
|
|
22
22
|
|
|
23
23
|
expectType<typeof ok, TernaryResult<number, never, never>>('<=');
|
|
24
24
|
|
|
@@ -85,7 +85,7 @@ describe('TernaryResult test', () => {
|
|
|
85
85
|
|
|
86
86
|
expect(TernaryResult.unwrapOk(okResult)).toBe(3);
|
|
87
87
|
|
|
88
|
-
|
|
88
|
+
assert.isTrue(TernaryResult.isWarn(warnResult));
|
|
89
89
|
|
|
90
90
|
if (TernaryResult.isWarn(warnResult)) {
|
|
91
91
|
expect(warnResult.value).toBe(3);
|
|
@@ -242,7 +242,7 @@ describe('TernaryResult test', () => {
|
|
|
242
242
|
Promise.reject(new Error('bad')),
|
|
243
243
|
);
|
|
244
244
|
|
|
245
|
-
|
|
245
|
+
assert.isTrue(TernaryResult.isErr(rejected));
|
|
246
246
|
});
|
|
247
247
|
|
|
248
248
|
test('fromThrowable converts thrown values', () => {
|
|
@@ -255,7 +255,7 @@ describe('TernaryResult test', () => {
|
|
|
255
255
|
throw new Error('boom');
|
|
256
256
|
});
|
|
257
257
|
|
|
258
|
-
|
|
258
|
+
assert.isTrue(TernaryResult.isErr(errorResult));
|
|
259
259
|
});
|
|
260
260
|
|
|
261
261
|
test('unwrapOkOr curried version', () => {
|
package/src/globals.d.mts
CHANGED
|
@@ -13,7 +13,8 @@ type ArgArrayIndex<Ar extends readonly unknown[]> =
|
|
|
13
13
|
|
|
14
14
|
type ArgArrayIndexWithNegative<Ar extends readonly unknown[]> =
|
|
15
15
|
IsFixedLengthList<Ar> extends true
|
|
16
|
-
?
|
|
16
|
+
? // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
|
17
|
+
IndexOfTuple<[...Ar, 0]> | NegativeIndexOfTuple<Ar>
|
|
17
18
|
: SizeType.ArgArrWithNegative;
|
|
18
19
|
|
|
19
20
|
// https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/length
|
package/src/guard/has-key.mts
CHANGED
|
@@ -21,9 +21,9 @@
|
|
|
21
21
|
*
|
|
22
22
|
* if (hasKey(maybeUser, 'id')) {
|
|
23
23
|
* // `maybeUser` is now known to have an `id` property.
|
|
24
|
-
* assert(maybeUser.id === 42);
|
|
24
|
+
* assert.isTrue(maybeUser.id === 42);
|
|
25
25
|
* } else {
|
|
26
|
-
* assert.fail(
|
|
26
|
+
* assert.fail();
|
|
27
27
|
* }
|
|
28
28
|
* ```
|
|
29
29
|
*
|
|
@@ -58,11 +58,14 @@ export type HasKeyReturnType<
|
|
|
58
58
|
> = R extends R // union distribution
|
|
59
59
|
? K extends keyof R
|
|
60
60
|
? string extends keyof R
|
|
61
|
-
?
|
|
61
|
+
? // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
|
62
|
+
ReadonlyRecord<K, R[keyof R]> & R
|
|
62
63
|
: number extends keyof R
|
|
63
|
-
?
|
|
64
|
+
? // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
|
65
|
+
ReadonlyRecord<K, R[keyof R]> & R
|
|
64
66
|
: symbol extends keyof R
|
|
65
|
-
?
|
|
67
|
+
? // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
|
68
|
+
ReadonlyRecord<K, R[keyof R]> & R
|
|
66
69
|
: R
|
|
67
70
|
: never // omit union member that does not have key K
|
|
68
71
|
: never; // dummy case for union distribution
|
|
@@ -61,7 +61,6 @@ import { hasKey, type HasKeyReturnType } from './has-key.mjs';
|
|
|
61
61
|
{
|
|
62
62
|
type R = Readonly<{ a: 0 }> | Readonly<{ b: 1 }>;
|
|
63
63
|
|
|
64
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
65
64
|
const obj: R = { a: 0 } as R;
|
|
66
65
|
|
|
67
66
|
if (hasKey(obj, 'a')) {
|
|
@@ -82,7 +81,6 @@ import { hasKey, type HasKeyReturnType } from './has-key.mjs';
|
|
|
82
81
|
| Readonly<{ b: 2 }>
|
|
83
82
|
| Readonly<{ c: 3 }>;
|
|
84
83
|
|
|
85
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
86
84
|
const obj: R = { a: 0 } as R;
|
|
87
85
|
|
|
88
86
|
if (hasKey(obj, 'a') && hasKey(obj, 'b')) {
|
|
@@ -101,7 +99,6 @@ import { hasKey, type HasKeyReturnType } from './has-key.mjs';
|
|
|
101
99
|
| Readonly<{ a: 1; b: 1 }>
|
|
102
100
|
| Readonly<{ b: 2 }>;
|
|
103
101
|
|
|
104
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
105
102
|
const obj: R = { a: 0 } as R;
|
|
106
103
|
|
|
107
104
|
expectType<
|
|
@@ -193,7 +190,6 @@ import { hasKey, type HasKeyReturnType } from './has-key.mjs';
|
|
|
193
190
|
| Readonly<{ b: 2 }>
|
|
194
191
|
| Record<string, number>;
|
|
195
192
|
|
|
196
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
197
193
|
const o2 = { b: 2 } as O;
|
|
198
194
|
|
|
199
195
|
if (hasOwnNaive(o2, 'a')) {
|
|
@@ -212,7 +208,6 @@ import { hasKey, type HasKeyReturnType } from './has-key.mjs';
|
|
|
212
208
|
| Readonly<{ a: 1; b: 1 }>
|
|
213
209
|
| Readonly<{ b: 2 }>;
|
|
214
210
|
|
|
215
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
216
211
|
const o2 = { b: 2 } as O;
|
|
217
212
|
|
|
218
213
|
if (hasOwnNaive(o2, 'a')) {
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/* eslint-disable functional/immutable-data */
|
|
2
|
+
/* eslint-disable functional/no-class-inheritance */
|
|
3
|
+
/* eslint-disable unicorn/error-message */
|
|
4
|
+
import { isError as isErrorBySindreSorhus } from '@sindresorhus/is';
|
|
5
|
+
import { expectType } from '../expect-type.mjs';
|
|
6
|
+
|
|
7
|
+
describe('isError from @sindresorhus/is', () => {
|
|
8
|
+
test('should return true for native Error instances', () => {
|
|
9
|
+
assert.isTrue(isErrorBySindreSorhus(new Error()));
|
|
10
|
+
|
|
11
|
+
assert.isTrue(isErrorBySindreSorhus(new Error('test message')));
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('should return true for native Error subclasses', () => {
|
|
15
|
+
assert.isTrue(isErrorBySindreSorhus(new TypeError()));
|
|
16
|
+
|
|
17
|
+
assert.isTrue(isErrorBySindreSorhus(new RangeError()));
|
|
18
|
+
|
|
19
|
+
assert.isTrue(isErrorBySindreSorhus(new ReferenceError()));
|
|
20
|
+
|
|
21
|
+
assert.isTrue(isErrorBySindreSorhus(new SyntaxError()));
|
|
22
|
+
|
|
23
|
+
assert.isTrue(isErrorBySindreSorhus(new EvalError()));
|
|
24
|
+
|
|
25
|
+
assert.isTrue(isErrorBySindreSorhus(new URIError()));
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('should return true for custom Error subclasses', () => {
|
|
29
|
+
class CustomError extends Error {
|
|
30
|
+
constructor(message?: string) {
|
|
31
|
+
super(message);
|
|
32
|
+
|
|
33
|
+
this.name = 'CustomError';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
assert.isTrue(isErrorBySindreSorhus(new CustomError()));
|
|
38
|
+
|
|
39
|
+
assert.isTrue(
|
|
40
|
+
isErrorBySindreSorhus(new CustomError('custom error message')),
|
|
41
|
+
);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('should return true for Error subclass with additional properties', () => {
|
|
45
|
+
class ExtendedError extends Error {
|
|
46
|
+
code: number;
|
|
47
|
+
|
|
48
|
+
constructor(message: string, code: number) {
|
|
49
|
+
super(message);
|
|
50
|
+
|
|
51
|
+
this.name = 'ExtendedError';
|
|
52
|
+
|
|
53
|
+
this.code = code;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
assert.isTrue(
|
|
58
|
+
isErrorBySindreSorhus(new ExtendedError('error with code', 404)),
|
|
59
|
+
);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('should return true for deeply nested Error subclass', () => {
|
|
63
|
+
class BaseCustomError extends Error {
|
|
64
|
+
constructor(message?: string) {
|
|
65
|
+
super(message);
|
|
66
|
+
|
|
67
|
+
this.name = 'BaseCustomError';
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
class DerivedCustomError extends BaseCustomError {
|
|
72
|
+
constructor(message?: string) {
|
|
73
|
+
super(message);
|
|
74
|
+
|
|
75
|
+
this.name = 'DerivedCustomError';
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
assert.isTrue(isErrorBySindreSorhus(new DerivedCustomError()));
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('should return false for Error-like objects', () => {
|
|
83
|
+
const errorLikeObject = {
|
|
84
|
+
name: 'Error',
|
|
85
|
+
message: 'This is not a real error',
|
|
86
|
+
stack: 'fake stack trace',
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
assert.isFalse(isErrorBySindreSorhus(errorLikeObject));
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('should return false for non-Error values', () => {
|
|
93
|
+
assert.isFalse(isErrorBySindreSorhus(null));
|
|
94
|
+
|
|
95
|
+
assert.isFalse(isErrorBySindreSorhus(undefined));
|
|
96
|
+
|
|
97
|
+
assert.isFalse(isErrorBySindreSorhus(0));
|
|
98
|
+
|
|
99
|
+
assert.isFalse(isErrorBySindreSorhus(''));
|
|
100
|
+
|
|
101
|
+
assert.isFalse(isErrorBySindreSorhus('Error'));
|
|
102
|
+
|
|
103
|
+
assert.isFalse(isErrorBySindreSorhus(true));
|
|
104
|
+
|
|
105
|
+
assert.isFalse(isErrorBySindreSorhus({}));
|
|
106
|
+
|
|
107
|
+
assert.isFalse(isErrorBySindreSorhus([]));
|
|
108
|
+
|
|
109
|
+
assert.isFalse(isErrorBySindreSorhus(() => {}));
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test('should act as a type guard', () => {
|
|
113
|
+
const value: Error | string = new Error('test');
|
|
114
|
+
|
|
115
|
+
if (isErrorBySindreSorhus(value)) {
|
|
116
|
+
expectType<typeof value, Error>('=');
|
|
117
|
+
|
|
118
|
+
expect(value.message).toBe('test');
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test('should work with AggregateError', () => {
|
|
123
|
+
const aggregateError = new AggregateError(
|
|
124
|
+
[new Error('error 1'), new Error('error 2')],
|
|
125
|
+
'Multiple errors occurred',
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
assert.isTrue(isErrorBySindreSorhus(aggregateError));
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test('should return false for errors created with Object.create', () => {
|
|
132
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
133
|
+
const protoError = Object.create(Error.prototype);
|
|
134
|
+
|
|
135
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
136
|
+
protoError.message = 'proto error';
|
|
137
|
+
|
|
138
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
139
|
+
protoError.name = 'ProtoError';
|
|
140
|
+
|
|
141
|
+
assert.isFalse(isErrorBySindreSorhus(protoError));
|
|
142
|
+
});
|
|
143
|
+
});
|
|
@@ -3,51 +3,51 @@ import { isNonEmptyString } from './is-non-empty-string.mjs';
|
|
|
3
3
|
|
|
4
4
|
describe(isNonEmptyString, () => {
|
|
5
5
|
test('should return true for non-empty strings', () => {
|
|
6
|
-
|
|
6
|
+
assert.isTrue(isNonEmptyString('hello'));
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
assert.isTrue(isNonEmptyString('a'));
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
assert.isTrue(isNonEmptyString(' ')); // Space is not empty
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
assert.isTrue(isNonEmptyString(' multiple spaces '));
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
assert.isTrue(isNonEmptyString('123'));
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
assert.isTrue(isNonEmptyString('special!@#$%'));
|
|
17
17
|
});
|
|
18
18
|
|
|
19
19
|
test('should return false for empty string', () => {
|
|
20
|
-
|
|
20
|
+
assert.isFalse(isNonEmptyString(''));
|
|
21
21
|
});
|
|
22
22
|
|
|
23
23
|
test('should return false for non-string values', () => {
|
|
24
|
-
|
|
24
|
+
assert.isFalse(isNonEmptyString(null));
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
assert.isFalse(isNonEmptyString(undefined));
|
|
27
27
|
|
|
28
28
|
// @ts-expect-error Testing non-string types
|
|
29
|
-
|
|
29
|
+
assert.isFalse(isNonEmptyString(42));
|
|
30
30
|
|
|
31
31
|
// @ts-expect-error Testing non-string types
|
|
32
|
-
|
|
32
|
+
assert.isFalse(isNonEmptyString(0));
|
|
33
33
|
|
|
34
34
|
// @ts-expect-error Testing non-string types
|
|
35
|
-
|
|
35
|
+
assert.isFalse(isNonEmptyString(true));
|
|
36
36
|
|
|
37
37
|
// @ts-expect-error Testing non-string types
|
|
38
|
-
|
|
38
|
+
assert.isFalse(isNonEmptyString(false));
|
|
39
39
|
|
|
40
40
|
// @ts-expect-error Testing non-string types
|
|
41
|
-
|
|
41
|
+
assert.isFalse(isNonEmptyString({}));
|
|
42
42
|
|
|
43
43
|
// @ts-expect-error Testing non-string types
|
|
44
|
-
|
|
44
|
+
assert.isFalse(isNonEmptyString([]));
|
|
45
45
|
|
|
46
46
|
// @ts-expect-error Testing non-string types
|
|
47
|
-
|
|
47
|
+
assert.isFalse(isNonEmptyString(['string']));
|
|
48
48
|
|
|
49
49
|
// @ts-expect-error Testing non-string types
|
|
50
|
-
|
|
50
|
+
assert.isFalse(isNonEmptyString(() => 'string'));
|
|
51
51
|
});
|
|
52
52
|
|
|
53
53
|
test('should act as a type guard', () => {
|
|
@@ -93,22 +93,22 @@ describe(isNonEmptyString, () => {
|
|
|
93
93
|
});
|
|
94
94
|
|
|
95
95
|
test('should handle string edge cases', () => {
|
|
96
|
-
|
|
96
|
+
assert.isTrue(isNonEmptyString('\n')); // Newline
|
|
97
97
|
|
|
98
|
-
|
|
98
|
+
assert.isTrue(isNonEmptyString('\t')); // Tab
|
|
99
99
|
|
|
100
|
-
|
|
100
|
+
assert.isTrue(isNonEmptyString('\r')); // Carriage return
|
|
101
101
|
|
|
102
|
-
|
|
102
|
+
assert.isTrue(isNonEmptyString('\0')); // Null character
|
|
103
103
|
|
|
104
|
-
|
|
104
|
+
assert.isTrue(isNonEmptyString('🎉')); // Emoji
|
|
105
105
|
|
|
106
|
-
|
|
106
|
+
assert.isTrue(isNonEmptyString('你好')); // Unicode characters
|
|
107
107
|
});
|
|
108
108
|
|
|
109
109
|
test('should not accept String objects', () => {
|
|
110
110
|
// @ts-expect-error Testing non-string types
|
|
111
111
|
// eslint-disable-next-line unicorn/new-for-builtins
|
|
112
|
-
|
|
112
|
+
assert.isFalse(isNonEmptyString(new String('hello') as unknown));
|
|
113
113
|
});
|
|
114
114
|
});
|
|
@@ -3,73 +3,73 @@ import { isNonNullObject } from './is-non-null-object.mjs';
|
|
|
3
3
|
|
|
4
4
|
describe(isNonNullObject, () => {
|
|
5
5
|
test('should return true for plain objects', () => {
|
|
6
|
-
|
|
6
|
+
assert.isTrue(isNonNullObject({}));
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
assert.isTrue(isNonNullObject({ a: 1, b: 'test' }));
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
assert.isTrue(isNonNullObject({ nested: { value: true } }));
|
|
11
11
|
});
|
|
12
12
|
|
|
13
13
|
test('should return true for arrays', () => {
|
|
14
|
-
|
|
14
|
+
assert.isTrue(isNonNullObject([]));
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
assert.isTrue(isNonNullObject([1, 2, 3]));
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
assert.isTrue(isNonNullObject(['a', 'b', 'c']));
|
|
19
19
|
});
|
|
20
20
|
|
|
21
21
|
test('should return true for functions', () => {
|
|
22
|
-
|
|
22
|
+
assert.isFalse(isNonNullObject(() => {}));
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
assert.isFalse(isNonNullObject(async () => {}));
|
|
25
25
|
|
|
26
26
|
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
|
|
27
|
-
|
|
27
|
+
assert.isFalse(isNonNullObject(class MyClass {}));
|
|
28
28
|
});
|
|
29
29
|
|
|
30
30
|
test('should return true for built-in objects', () => {
|
|
31
|
-
|
|
31
|
+
assert.isTrue(isNonNullObject(new Date()));
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
assert.isTrue(isNonNullObject(/test/u));
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
assert.isTrue(isNonNullObject(/regex/u));
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
assert.isTrue(isNonNullObject(new Map()));
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
assert.isTrue(isNonNullObject(new Set()));
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
assert.isTrue(isNonNullObject(new Error('test')));
|
|
42
42
|
});
|
|
43
43
|
|
|
44
44
|
test('should return true for boxed primitives', () => {
|
|
45
45
|
// eslint-disable-next-line unicorn/new-for-builtins
|
|
46
|
-
|
|
46
|
+
assert.isTrue(isNonNullObject(new String('hello')));
|
|
47
47
|
|
|
48
48
|
// eslint-disable-next-line unicorn/new-for-builtins
|
|
49
|
-
|
|
49
|
+
assert.isTrue(isNonNullObject(new Number(42)));
|
|
50
50
|
|
|
51
51
|
// eslint-disable-next-line unicorn/new-for-builtins
|
|
52
|
-
|
|
52
|
+
assert.isTrue(isNonNullObject(new Boolean(true)));
|
|
53
53
|
});
|
|
54
54
|
|
|
55
55
|
test('should return false for null', () => {
|
|
56
|
-
|
|
56
|
+
assert.isFalse(isNonNullObject(null));
|
|
57
57
|
});
|
|
58
58
|
|
|
59
59
|
test('should return false for primitive values', () => {
|
|
60
|
-
|
|
60
|
+
assert.isFalse(isNonNullObject(undefined));
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
assert.isFalse(isNonNullObject('string'));
|
|
63
63
|
|
|
64
|
-
|
|
64
|
+
assert.isFalse(isNonNullObject(42));
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
assert.isFalse(isNonNullObject(true));
|
|
67
67
|
|
|
68
|
-
|
|
68
|
+
assert.isFalse(isNonNullObject(false));
|
|
69
69
|
|
|
70
|
-
|
|
70
|
+
assert.isFalse(isNonNullObject(Symbol('test')));
|
|
71
71
|
|
|
72
|
-
|
|
72
|
+
assert.isFalse(isNonNullObject(123n));
|
|
73
73
|
});
|
|
74
74
|
|
|
75
75
|
test('should act as a type guard', () => {
|
|
@@ -4,9 +4,9 @@ import { isSymbol } from './is-type.mjs';
|
|
|
4
4
|
|
|
5
5
|
describe(isPrimitive, () => {
|
|
6
6
|
test('should return true for string primitives', () => {
|
|
7
|
-
|
|
7
|
+
assert.isTrue(isPrimitive('hello'));
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
assert.isTrue(isPrimitive(''));
|
|
10
10
|
|
|
11
11
|
const value: unknown = 'test';
|
|
12
12
|
|
|
@@ -19,77 +19,77 @@ describe(isPrimitive, () => {
|
|
|
19
19
|
});
|
|
20
20
|
|
|
21
21
|
test('should return true for number primitives', () => {
|
|
22
|
-
|
|
22
|
+
assert.isTrue(isPrimitive(42));
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
assert.isTrue(isPrimitive(0));
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
assert.isTrue(isPrimitive(-3.14));
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
assert.isTrue(isPrimitive(Number.NaN));
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
assert.isTrue(isPrimitive(Number.POSITIVE_INFINITY));
|
|
31
31
|
});
|
|
32
32
|
|
|
33
33
|
test('should return true for boolean primitives', () => {
|
|
34
|
-
|
|
34
|
+
assert.isTrue(isPrimitive(true));
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
assert.isTrue(isPrimitive(false));
|
|
37
37
|
});
|
|
38
38
|
|
|
39
39
|
test('should return true for symbol primitives', () => {
|
|
40
|
-
|
|
40
|
+
assert.isTrue(isPrimitive(Symbol('test')));
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
assert.isTrue(isPrimitive(Symbol.iterator));
|
|
43
43
|
});
|
|
44
44
|
|
|
45
45
|
test('should return true for bigint primitives', () => {
|
|
46
|
-
|
|
46
|
+
assert.isTrue(isPrimitive(123n));
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
assert.isTrue(isPrimitive(0n));
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
assert.isTrue(isPrimitive(-123n));
|
|
51
51
|
});
|
|
52
52
|
|
|
53
53
|
test('should return true for null', () => {
|
|
54
54
|
// Note: null is considered an object by typeof, so isPrimitive returns false
|
|
55
|
-
|
|
55
|
+
assert.isTrue(isPrimitive(null));
|
|
56
56
|
});
|
|
57
57
|
|
|
58
58
|
test('should return true for undefined', () => {
|
|
59
|
-
|
|
59
|
+
assert.isTrue(isPrimitive(undefined));
|
|
60
60
|
});
|
|
61
61
|
|
|
62
62
|
test('should return false for objects', () => {
|
|
63
|
-
|
|
63
|
+
assert.isFalse(isPrimitive({}));
|
|
64
64
|
|
|
65
|
-
|
|
65
|
+
assert.isFalse(isPrimitive({ a: 1 }));
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
assert.isFalse(isPrimitive(new Date()));
|
|
68
68
|
});
|
|
69
69
|
|
|
70
70
|
test('should return false for arrays', () => {
|
|
71
|
-
|
|
71
|
+
assert.isFalse(isPrimitive([]));
|
|
72
72
|
|
|
73
|
-
|
|
73
|
+
assert.isFalse(isPrimitive([1, 2, 3]));
|
|
74
74
|
});
|
|
75
75
|
|
|
76
76
|
test('should return false for functions', () => {
|
|
77
|
-
|
|
77
|
+
assert.isFalse(isPrimitive(() => {}));
|
|
78
78
|
|
|
79
|
-
|
|
79
|
+
assert.isFalse(isPrimitive(() => {}));
|
|
80
80
|
|
|
81
|
-
|
|
81
|
+
assert.isFalse(isPrimitive(async () => {}));
|
|
82
82
|
});
|
|
83
83
|
|
|
84
84
|
test('should return false for boxed primitives', () => {
|
|
85
85
|
// eslint-disable-next-line unicorn/new-for-builtins
|
|
86
|
-
|
|
86
|
+
assert.isFalse(isPrimitive(new String('hello')));
|
|
87
87
|
|
|
88
88
|
// eslint-disable-next-line unicorn/new-for-builtins
|
|
89
|
-
|
|
89
|
+
assert.isFalse(isPrimitive(new Number(42)));
|
|
90
90
|
|
|
91
91
|
// eslint-disable-next-line unicorn/new-for-builtins
|
|
92
|
-
|
|
92
|
+
assert.isFalse(isPrimitive(new Boolean(true)));
|
|
93
93
|
});
|
|
94
94
|
|
|
95
95
|
test('should narrow types correctly in conditional', () => {
|
|
@@ -120,7 +120,7 @@ describe(isPrimitive, () => {
|
|
|
120
120
|
|
|
121
121
|
expect(primitives[4]).toBeUndefined();
|
|
122
122
|
|
|
123
|
-
|
|
123
|
+
assert.isTrue(isSymbol(primitives[5]));
|
|
124
124
|
|
|
125
125
|
assert.deepStrictEqual(nonPrimitives, [{}, []]);
|
|
126
126
|
});
|
package/src/guard/is-record.mts
CHANGED
|
@@ -38,8 +38,7 @@ import { isNonNullObject } from './is-non-null-object.mjs';
|
|
|
38
38
|
* @see {@link isNonNullObject} - For checking any object type (includes arrays)
|
|
39
39
|
* @see {@link hasKey} - For checking if a record has specific keys
|
|
40
40
|
*/
|
|
41
|
-
export const isRecord = (u: unknown): u is UnknownRecord =>
|
|
42
|
-
isNonNullObject(u) && !Array.isArray(u);
|
|
41
|
+
export const isRecord = (u: unknown): u is UnknownRecord => isNonNullObject(u);
|
|
43
42
|
|
|
44
43
|
/**
|
|
45
44
|
* Type guard that checks if a value is a mutable record (an object with mutable string keys).
|