ts-data-forge 5.1.1 → 6.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/dist/array/impl/array-utils-reducing-value.d.mts +28 -0
- package/dist/array/impl/array-utils-reducing-value.d.mts.map +1 -1
- package/dist/array/impl/array-utils-reducing-value.mjs +4 -1
- package/dist/array/impl/array-utils-reducing-value.mjs.map +1 -1
- package/dist/array/impl/index.mjs +1 -1
- package/dist/collections/imap.d.mts +1 -1
- package/dist/collections/iset-mapped.d.mts +1 -1
- package/dist/collections/iset.d.mts +1 -1
- 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/package.json +52 -48
- package/src/array/impl/array-utils-overload-type-error.test.mts +4 -2
- package/src/array/impl/array-utils-reducing-value.mts +48 -0
- package/src/collections/imap.mts +1 -1
- package/src/collections/iset-mapped.mts +1 -1
- package/src/collections/iset.mts +1 -1
- package/src/functional/optional.test.mts +7 -5
- package/src/functional/result.test.mts +10 -8
- package/src/functional/ternary-result.test.mts +4 -4
- package/src/guard/has-key.test.mts +89 -186
- package/src/guard/is-record.mts +1 -2
- package/src/guard/is-record.test.mts +57 -67
- package/src/json/json.test.mts +5 -5
- package/src/number/branded-types/finite-number.test.mts +15 -11
- package/src/number/branded-types/int.test.mts +13 -13
- package/src/number/branded-types/int16.test.mts +15 -15
- package/src/number/branded-types/int32.test.mts +15 -15
- package/src/number/branded-types/non-negative-finite-number.test.mts +19 -15
- package/src/number/branded-types/non-negative-int16.test.mts +15 -15
- package/src/number/branded-types/non-negative-int32.test.mts +15 -15
- package/src/number/branded-types/non-zero-finite-number.test.mts +18 -18
- package/src/number/branded-types/non-zero-int.test.mts +18 -14
- package/src/number/branded-types/non-zero-int16.test.mts +19 -15
- package/src/number/branded-types/non-zero-int32.test.mts +19 -15
- package/src/number/branded-types/non-zero-safe-int.test.mts +22 -18
- package/src/number/branded-types/non-zero-uint16.test.mts +15 -15
- package/src/number/branded-types/non-zero-uint32.test.mts +15 -15
- package/src/number/branded-types/positive-finite-number.test.mts +18 -18
- package/src/number/branded-types/positive-int.test.mts +22 -16
- package/src/number/branded-types/positive-int16.test.mts +14 -14
- package/src/number/branded-types/positive-int32.test.mts +14 -14
- package/src/number/branded-types/positive-safe-int.test.mts +20 -18
- package/src/number/branded-types/positive-uint16.test.mts +15 -15
- package/src/number/branded-types/positive-uint32.test.mts +15 -15
- package/src/number/branded-types/safe-int.test.mts +21 -17
- package/src/number/branded-types/safe-uint.test.mts +22 -16
- package/src/number/branded-types/uint.test.mts +14 -14
- package/src/number/branded-types/uint16.test.mts +14 -14
- package/src/number/branded-types/uint32.test.mts +14 -14
- package/src/number/enum/int8.test.mts +1 -1
- package/src/number/enum/uint8.test.mts +1 -1
- package/src/others/memoize-function.test.mts +1 -0
|
@@ -1,223 +1,126 @@
|
|
|
1
1
|
import { expectType } from '../expect-type.mjs';
|
|
2
2
|
import { hasKey, type HasKeyReturnType } from './has-key.mjs';
|
|
3
3
|
|
|
4
|
-
{
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
expectType<
|
|
11
|
-
HasKeyReturnType<Readonly<{ a: 0 }> | Readonly<{ b: 1 }>, 'b'>,
|
|
12
|
-
Readonly<{ b: 1 }>
|
|
13
|
-
>('=');
|
|
14
|
-
|
|
15
|
-
expectType<
|
|
16
|
-
HasKeyReturnType<Readonly<{ a: 0 }> | Readonly<{ b: 1 }>, 'd'>,
|
|
17
|
-
never
|
|
18
|
-
>('=');
|
|
19
|
-
|
|
20
|
-
expectType<
|
|
21
|
-
HasKeyReturnType<
|
|
22
|
-
| Readonly<{ a: 0 }>
|
|
23
|
-
| Readonly<{ a: 1; b: 1 }>
|
|
24
|
-
| Readonly<{ b: 2 }>
|
|
25
|
-
| Readonly<{ c: 3 }>,
|
|
26
|
-
'a'
|
|
27
|
-
>,
|
|
28
|
-
Readonly<{ a: 0 }> | Readonly<{ a: 1; b: 1 }>
|
|
29
|
-
>('=');
|
|
30
|
-
|
|
31
|
-
expectType<
|
|
32
|
-
HasKeyReturnType<
|
|
33
|
-
| Readonly<{ a: 0 }>
|
|
34
|
-
| Readonly<{ a: 1; b: 1 }>
|
|
35
|
-
| Readonly<{ b: 2 }>
|
|
36
|
-
| Readonly<{ c: 3 }>,
|
|
37
|
-
'b'
|
|
38
|
-
>,
|
|
39
|
-
Readonly<{ a: 1; b: 1 }> | Readonly<{ b: 2 }>
|
|
40
|
-
>('=');
|
|
41
|
-
|
|
42
|
-
expectType<
|
|
43
|
-
HasKeyReturnType<
|
|
44
|
-
| ReadonlyRecord<string, number>
|
|
45
|
-
| Readonly<{ a: 0 }>
|
|
46
|
-
| Readonly<{ a: 1; b: 1 }>
|
|
47
|
-
| Readonly<{ b: 2 }>,
|
|
48
|
-
'a'
|
|
49
|
-
>,
|
|
50
|
-
| Readonly<{ a: 0 }>
|
|
51
|
-
| Readonly<{ a: 1; b: 1 }>
|
|
52
|
-
| (ReadonlyRecord<'a', number> & ReadonlyRecord<string, number>)
|
|
53
|
-
>('=');
|
|
54
|
-
|
|
55
|
-
expectType<
|
|
56
|
-
HasKeyReturnType<ReadonlyRecord<string, unknown>, 'a'>,
|
|
57
|
-
ReadonlyRecord<'a', unknown> & ReadonlyRecord<string, unknown>
|
|
58
|
-
>('=');
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
{
|
|
62
|
-
type R = Readonly<{ a: 0 }> | Readonly<{ b: 1 }>;
|
|
63
|
-
|
|
64
|
-
const obj: R = { a: 0 } as R;
|
|
65
|
-
|
|
66
|
-
if (hasKey(obj, 'a')) {
|
|
67
|
-
expectType<typeof obj.a, 0>('=');
|
|
68
|
-
|
|
69
|
-
expectType<typeof obj, Readonly<{ a: 0 }>>('=');
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (hasKey(obj, 'c')) {
|
|
73
|
-
expectType<typeof obj, never>('=');
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
{
|
|
78
|
-
type R =
|
|
79
|
-
| Readonly<{ a: 0 }>
|
|
80
|
-
| Readonly<{ a: 1; b: 1 }>
|
|
81
|
-
| Readonly<{ b: 2 }>
|
|
82
|
-
| Readonly<{ c: 3 }>;
|
|
83
|
-
|
|
84
|
-
const obj: R = { a: 0 } as R;
|
|
85
|
-
|
|
86
|
-
if (hasKey(obj, 'a') && hasKey(obj, 'b')) {
|
|
87
|
-
expectType<typeof obj.a, 1>('=');
|
|
88
|
-
|
|
89
|
-
expectType<typeof obj.b, 1>('=');
|
|
90
|
-
|
|
91
|
-
expectType<typeof obj, Readonly<{ a: 1; b: 1 }>>('=');
|
|
92
|
-
}
|
|
93
|
-
}
|
|
4
|
+
test('hasKey type inferences', () => {
|
|
5
|
+
{
|
|
6
|
+
expectType<
|
|
7
|
+
HasKeyReturnType<Readonly<{ a: 0 }> | Readonly<{ b: 1 }>, 'a'>,
|
|
8
|
+
Readonly<{ a: 0 }>
|
|
9
|
+
>('=');
|
|
94
10
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
| Readonly<{ a: 1; b: 1 }>
|
|
100
|
-
| Readonly<{ b: 2 }>;
|
|
11
|
+
expectType<
|
|
12
|
+
HasKeyReturnType<Readonly<{ a: 0 }> | Readonly<{ b: 1 }>, 'b'>,
|
|
13
|
+
Readonly<{ b: 1 }>
|
|
14
|
+
>('=');
|
|
101
15
|
|
|
102
|
-
|
|
16
|
+
expectType<
|
|
17
|
+
HasKeyReturnType<Readonly<{ a: 0 }> | Readonly<{ b: 1 }>, 'd'>,
|
|
18
|
+
never
|
|
19
|
+
>('=');
|
|
103
20
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
21
|
+
expectType<
|
|
22
|
+
HasKeyReturnType<
|
|
23
|
+
| Readonly<{ a: 0 }>
|
|
24
|
+
| Readonly<{ a: 1; b: 1 }>
|
|
25
|
+
| Readonly<{ b: 2 }>
|
|
26
|
+
| Readonly<{ c: 3 }>,
|
|
27
|
+
'a'
|
|
28
|
+
>,
|
|
29
|
+
Readonly<{ a: 0 }> | Readonly<{ a: 1; b: 1 }>
|
|
30
|
+
>('=');
|
|
111
31
|
|
|
112
|
-
|
|
113
|
-
|
|
32
|
+
expectType<
|
|
33
|
+
HasKeyReturnType<
|
|
34
|
+
| Readonly<{ a: 0 }>
|
|
35
|
+
| Readonly<{ a: 1; b: 1 }>
|
|
36
|
+
| Readonly<{ b: 2 }>
|
|
37
|
+
| Readonly<{ c: 3 }>,
|
|
38
|
+
'b'
|
|
39
|
+
>,
|
|
40
|
+
Readonly<{ a: 1; b: 1 }> | Readonly<{ b: 2 }>
|
|
41
|
+
>('=');
|
|
114
42
|
|
|
115
43
|
expectType<
|
|
116
|
-
|
|
44
|
+
HasKeyReturnType<
|
|
45
|
+
| ReadonlyRecord<string, number>
|
|
46
|
+
| Readonly<{ a: 0 }>
|
|
47
|
+
| Readonly<{ a: 1; b: 1 }>
|
|
48
|
+
| Readonly<{ b: 2 }>,
|
|
49
|
+
'a'
|
|
50
|
+
>,
|
|
117
51
|
| Readonly<{ a: 0 }>
|
|
118
52
|
| Readonly<{ a: 1; b: 1 }>
|
|
119
53
|
| (ReadonlyRecord<'a', number> & ReadonlyRecord<string, number>)
|
|
120
54
|
>('=');
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (hasKey(obj, 'a') && hasKey(obj, 'b')) {
|
|
124
|
-
expectType<typeof obj.a, number>('=');
|
|
125
|
-
|
|
126
|
-
expectType<typeof obj.b, number>('=');
|
|
127
55
|
|
|
128
56
|
expectType<
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
| (ReadonlyRecord<'a', number> &
|
|
132
|
-
ReadonlyRecord<'b', number> &
|
|
133
|
-
ReadonlyRecord<string, number>)
|
|
57
|
+
HasKeyReturnType<ReadonlyRecord<string, unknown>, 'a'>,
|
|
58
|
+
ReadonlyRecord<'a', unknown> & ReadonlyRecord<string, unknown>
|
|
134
59
|
>('=');
|
|
135
60
|
}
|
|
136
|
-
}
|
|
137
61
|
|
|
138
|
-
{
|
|
139
|
-
|
|
62
|
+
{
|
|
63
|
+
type R = Readonly<{ a: 0 }> | Readonly<{ b: 1 }>;
|
|
140
64
|
|
|
141
|
-
|
|
142
|
-
expectType<typeof o.a, unknown>('=');
|
|
65
|
+
const obj: R = { a: 0 } as R;
|
|
143
66
|
|
|
144
|
-
|
|
145
|
-
typeof
|
|
146
|
-
ReadonlyRecord<'a', unknown> & ReadonlyRecord<string, unknown>
|
|
147
|
-
>('=');
|
|
148
|
-
}
|
|
67
|
+
if (hasKey(obj, 'a')) {
|
|
68
|
+
expectType<typeof obj.a, 0>('=');
|
|
149
69
|
|
|
150
|
-
|
|
151
|
-
|
|
70
|
+
expectType<typeof obj, Readonly<{ a: 0 }>>('=');
|
|
71
|
+
}
|
|
152
72
|
|
|
153
|
-
|
|
154
|
-
typeof
|
|
155
|
-
|
|
156
|
-
>('=');
|
|
73
|
+
if (hasKey(obj, 'c')) {
|
|
74
|
+
expectType<typeof obj, never>('=');
|
|
75
|
+
}
|
|
157
76
|
}
|
|
158
77
|
|
|
159
|
-
|
|
160
|
-
|
|
78
|
+
{
|
|
79
|
+
type R =
|
|
80
|
+
| Readonly<{ a: 0 }>
|
|
81
|
+
| Readonly<{ a: 1; b: 1 }>
|
|
82
|
+
| Readonly<{ b: 2 }>
|
|
83
|
+
| Readonly<{ c: 3 }>;
|
|
161
84
|
|
|
162
|
-
|
|
85
|
+
const obj: R = { a: 0 } as R;
|
|
163
86
|
|
|
164
|
-
|
|
165
|
-
typeof
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
87
|
+
if (hasKey(obj, 'a') && hasKey(obj, 'b')) {
|
|
88
|
+
expectType<typeof obj.a, 1>('=');
|
|
89
|
+
|
|
90
|
+
expectType<typeof obj.b, 1>('=');
|
|
91
|
+
|
|
92
|
+
expectType<typeof obj, Readonly<{ a: 1; b: 1 }>>('=');
|
|
93
|
+
}
|
|
170
94
|
}
|
|
171
95
|
|
|
172
96
|
{
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
* which doesn't work well.
|
|
179
|
-
*/
|
|
180
|
-
|
|
181
|
-
const hasOwnNaive = <R extends UnknownRecord, K extends string>(
|
|
182
|
-
obj: R,
|
|
183
|
-
key: K,
|
|
184
|
-
): obj is R & Record<K, R[K]> => hasKey(obj, key);
|
|
185
|
-
|
|
186
|
-
{
|
|
187
|
-
type O =
|
|
188
|
-
| Readonly<{ a: 0 }>
|
|
189
|
-
| Readonly<{ a: 1; b: 1 }>
|
|
190
|
-
| Readonly<{ b: 2 }>
|
|
191
|
-
| Record<string, number>;
|
|
97
|
+
type R =
|
|
98
|
+
| ReadonlyRecord<string, number>
|
|
99
|
+
| Readonly<{ a: 0 }>
|
|
100
|
+
| Readonly<{ a: 1; b: 1 }>
|
|
101
|
+
| Readonly<{ b: 2 }>;
|
|
192
102
|
|
|
193
|
-
|
|
103
|
+
const obj: R = { a: 0 } as R;
|
|
194
104
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
105
|
+
expectType<
|
|
106
|
+
R,
|
|
107
|
+
| ReadonlyRecord<string, number>
|
|
108
|
+
| Readonly<{ a: 0 }>
|
|
109
|
+
| Readonly<{ a: 1; b: 1 }>
|
|
110
|
+
| Readonly<{ b: 2 }>
|
|
111
|
+
>('=');
|
|
198
112
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
expectType<typeof o2.a, number>('=');
|
|
202
|
-
}
|
|
203
|
-
}
|
|
113
|
+
if (hasKey(obj, 'a')) {
|
|
114
|
+
expectType<typeof obj.a, number>('=');
|
|
204
115
|
|
|
205
|
-
|
|
206
|
-
|
|
116
|
+
expectType<
|
|
117
|
+
typeof obj,
|
|
207
118
|
| Readonly<{ a: 0 }>
|
|
208
119
|
| Readonly<{ a: 1; b: 1 }>
|
|
209
|
-
|
|
|
210
|
-
|
|
211
|
-
const o2 = { b: 2 } as O;
|
|
212
|
-
|
|
213
|
-
if (hasOwnNaive(o2, 'a')) {
|
|
214
|
-
expectType<typeof o2.a, unknown>('=');
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// eslint-disable-next-line no-restricted-syntax
|
|
218
|
-
if ('a' in o2) {
|
|
219
|
-
expectType<typeof o2.a, 0 | 1>('=');
|
|
220
|
-
}
|
|
120
|
+
| (ReadonlyRecord<'a', number> & ReadonlyRecord<string, number>)
|
|
121
|
+
>('=');
|
|
221
122
|
}
|
|
222
123
|
}
|
|
223
|
-
|
|
124
|
+
|
|
125
|
+
assert.strictEqual(true, true);
|
|
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).
|
|
@@ -2,110 +2,100 @@ import { expectType } from '../expect-type.mjs';
|
|
|
2
2
|
import { isRecord } from './is-record.mjs';
|
|
3
3
|
|
|
4
4
|
describe(isRecord, () => {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
describe('arrays are Records', () => {
|
|
6
|
+
test('number[] is assignable to Record<number, unknown>', () => {
|
|
7
|
+
const arr: Record<number, unknown> = [1, 2, 3];
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
const unk: unknown = arr;
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
assert.isTrue(isRecord(unk));
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
expectType<typeof unk, UnknownRecord>('<=');
|
|
14
|
+
});
|
|
13
15
|
|
|
14
|
-
|
|
16
|
+
test('string[] is assignable to Record<number, unknown>', () => {
|
|
17
|
+
const arr: Record<number, unknown> = ['1', '2', '3'];
|
|
15
18
|
|
|
16
|
-
|
|
17
|
-
expectType<typeof unk, UnknownRecord>('=');
|
|
18
|
-
}
|
|
19
|
+
const unk: unknown = arr;
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
test('{} is a record', () => {
|
|
24
|
-
const obj = {} as const;
|
|
25
|
-
|
|
26
|
-
const unk: unknown = obj;
|
|
21
|
+
assert.isTrue(isRecord(unk));
|
|
27
22
|
|
|
28
|
-
|
|
23
|
+
expectType<typeof unk, UnknownRecord>('<=');
|
|
24
|
+
});
|
|
29
25
|
|
|
30
|
-
|
|
26
|
+
test('[] is a record', () => {
|
|
27
|
+
const arr: unknown = [] as const;
|
|
31
28
|
|
|
32
|
-
|
|
29
|
+
const unk: unknown = arr;
|
|
33
30
|
|
|
34
|
-
|
|
35
|
-
expectType<typeof unk, UnknownRecord>('=');
|
|
36
|
-
}
|
|
31
|
+
assert.isTrue(isRecord(unk));
|
|
37
32
|
|
|
38
|
-
|
|
33
|
+
expectType<typeof unk, UnknownRecord>('<=');
|
|
34
|
+
});
|
|
39
35
|
});
|
|
40
36
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const unk: unknown = obj;
|
|
45
|
-
|
|
46
|
-
const res = isRecord(unk);
|
|
47
|
-
|
|
48
|
-
expectType<typeof obj, readonly never[]>('=');
|
|
37
|
+
describe('non-null objects are Records', () => {
|
|
38
|
+
test('{ x: 1 } is a record', () => {
|
|
39
|
+
const obj = { x: 1 } as const;
|
|
49
40
|
|
|
50
|
-
|
|
41
|
+
const unk: unknown = obj;
|
|
51
42
|
|
|
52
|
-
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
test('null is not a record', () => {
|
|
56
|
-
const obj = null;
|
|
43
|
+
assert.isTrue(isRecord(unk));
|
|
57
44
|
|
|
58
|
-
|
|
45
|
+
expectType<typeof unk, UnknownRecord>('<=');
|
|
46
|
+
});
|
|
59
47
|
|
|
60
|
-
|
|
48
|
+
test('{} is a record', () => {
|
|
49
|
+
const obj = {} as const;
|
|
61
50
|
|
|
62
|
-
|
|
51
|
+
const unk: unknown = obj;
|
|
63
52
|
|
|
64
|
-
|
|
53
|
+
assert.isTrue(isRecord(unk));
|
|
65
54
|
|
|
66
|
-
|
|
55
|
+
expectType<typeof unk, UnknownRecord>('<=');
|
|
56
|
+
});
|
|
67
57
|
});
|
|
68
58
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const unk: unknown = obj;
|
|
59
|
+
describe('primitives are not Records', () => {
|
|
60
|
+
test('null is not a record', () => {
|
|
61
|
+
const obj = null;
|
|
73
62
|
|
|
74
|
-
|
|
63
|
+
const unk: unknown = obj;
|
|
75
64
|
|
|
76
|
-
|
|
65
|
+
assert.isFalse(isRecord(unk));
|
|
77
66
|
|
|
78
|
-
|
|
67
|
+
expectType<typeof unk, UnknownRecord>('!<=');
|
|
68
|
+
});
|
|
79
69
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
test('3 is not a record', () => {
|
|
84
|
-
const obj = 3;
|
|
70
|
+
test('undefined is not a record', () => {
|
|
71
|
+
const prm = undefined;
|
|
85
72
|
|
|
86
|
-
|
|
73
|
+
const unk: unknown = prm;
|
|
87
74
|
|
|
88
|
-
|
|
75
|
+
assert.isFalse(isRecord(unk));
|
|
89
76
|
|
|
90
|
-
|
|
77
|
+
expectType<typeof unk, UnknownRecord>('!<=');
|
|
78
|
+
});
|
|
91
79
|
|
|
92
|
-
|
|
80
|
+
test('3 is not a record', () => {
|
|
81
|
+
const prm = 3;
|
|
93
82
|
|
|
94
|
-
|
|
95
|
-
});
|
|
83
|
+
const unk: unknown = prm;
|
|
96
84
|
|
|
97
|
-
|
|
98
|
-
const obj = 'str';
|
|
85
|
+
assert.isFalse(isRecord(unk));
|
|
99
86
|
|
|
100
|
-
|
|
87
|
+
expectType<typeof unk, UnknownRecord>('!<=');
|
|
88
|
+
});
|
|
101
89
|
|
|
102
|
-
|
|
90
|
+
test('"str" is not a record', () => {
|
|
91
|
+
const prm = 'str';
|
|
103
92
|
|
|
104
|
-
|
|
93
|
+
const unk: unknown = prm;
|
|
105
94
|
|
|
106
|
-
|
|
95
|
+
assert.isFalse(isRecord(unk));
|
|
107
96
|
|
|
108
|
-
|
|
97
|
+
expectType<typeof unk, UnknownRecord>('!<=');
|
|
98
|
+
});
|
|
109
99
|
});
|
|
110
100
|
|
|
111
101
|
// test('Map is not a record', () => {
|
package/src/json/json.test.mts
CHANGED
|
@@ -118,11 +118,11 @@ describe('parse', () => {
|
|
|
118
118
|
});
|
|
119
119
|
|
|
120
120
|
test('should not throw errors', () => {
|
|
121
|
-
expect(() => Json.parse('{{{')).not.
|
|
121
|
+
expect(() => Json.parse('{{{')).not.toThrowError();
|
|
122
122
|
|
|
123
|
-
expect(() => Json.parse('null null')).not.
|
|
123
|
+
expect(() => Json.parse('null null')).not.toThrowError();
|
|
124
124
|
|
|
125
|
-
expect(() => Json.parse(String(undefined))).not.
|
|
125
|
+
expect(() => Json.parse(String(undefined))).not.toThrowError();
|
|
126
126
|
});
|
|
127
127
|
|
|
128
128
|
test('should use reviver function to transform values', () => {
|
|
@@ -323,9 +323,9 @@ describe('stringify', () => {
|
|
|
323
323
|
|
|
324
324
|
mut_circularArray.push(mut_circularArray);
|
|
325
325
|
|
|
326
|
-
expect(() => Json.stringify(mut_circularArray)).not.
|
|
326
|
+
expect(() => Json.stringify(mut_circularArray)).not.toThrowError();
|
|
327
327
|
|
|
328
|
-
expect(() => Json.stringify({ fn: () => {} })).not.
|
|
328
|
+
expect(() => Json.stringify({ fn: () => {} })).not.toThrowError();
|
|
329
329
|
});
|
|
330
330
|
|
|
331
331
|
test('should use replacer function to filter values', () => {
|
|
@@ -6,27 +6,31 @@ import { asNonZeroFiniteNumber } from './non-zero-finite-number.mjs';
|
|
|
6
6
|
describe('FiniteNumber test', () => {
|
|
7
7
|
describe(asFiniteNumber, () => {
|
|
8
8
|
test('accepts valid finite numbers', () => {
|
|
9
|
-
expect(() => asFiniteNumber(0)).not.
|
|
9
|
+
expect(() => asFiniteNumber(0)).not.toThrowError();
|
|
10
10
|
|
|
11
|
-
expect(() => asFiniteNumber(1)).not.
|
|
11
|
+
expect(() => asFiniteNumber(1)).not.toThrowError();
|
|
12
12
|
|
|
13
|
-
expect(() => asFiniteNumber(-1)).not.
|
|
13
|
+
expect(() => asFiniteNumber(-1)).not.toThrowError();
|
|
14
14
|
|
|
15
|
-
expect(() => asFiniteNumber(3.14)).not.
|
|
15
|
+
expect(() => asFiniteNumber(3.14)).not.toThrowError();
|
|
16
16
|
|
|
17
|
-
expect(() => asFiniteNumber(-2.5)).not.
|
|
17
|
+
expect(() => asFiniteNumber(-2.5)).not.toThrowError();
|
|
18
18
|
|
|
19
|
-
expect(() => asFiniteNumber(Number.MAX_VALUE)).not.
|
|
19
|
+
expect(() => asFiniteNumber(Number.MAX_VALUE)).not.toThrowError();
|
|
20
20
|
|
|
21
|
-
expect(() => asFiniteNumber(-Number.MAX_VALUE)).not.
|
|
21
|
+
expect(() => asFiniteNumber(-Number.MAX_VALUE)).not.toThrowError();
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
test('rejects non-finite numbers', () => {
|
|
25
|
-
expect(() => asFiniteNumber(Number.NaN)).
|
|
25
|
+
expect(() => asFiniteNumber(Number.NaN)).toThrowError(TypeError);
|
|
26
26
|
|
|
27
|
-
expect(() => asFiniteNumber(Number.POSITIVE_INFINITY)).
|
|
27
|
+
expect(() => asFiniteNumber(Number.POSITIVE_INFINITY)).toThrowError(
|
|
28
|
+
TypeError,
|
|
29
|
+
);
|
|
28
30
|
|
|
29
|
-
expect(() => asFiniteNumber(Number.NEGATIVE_INFINITY)).
|
|
31
|
+
expect(() => asFiniteNumber(Number.NEGATIVE_INFINITY)).toThrowError(
|
|
32
|
+
TypeError,
|
|
33
|
+
);
|
|
30
34
|
});
|
|
31
35
|
|
|
32
36
|
test('returns the same value for valid inputs', () => {
|
|
@@ -44,7 +48,7 @@ describe('FiniteNumber test', () => {
|
|
|
44
48
|
] as const)(
|
|
45
49
|
`asFiniteNumber($name) should throw a TypeError`,
|
|
46
50
|
({ value }) => {
|
|
47
|
-
expect(() => asFiniteNumber(value)).
|
|
51
|
+
expect(() => asFiniteNumber(value)).toThrowError(
|
|
48
52
|
new TypeError(`Expected a finite number, got: ${value}`),
|
|
49
53
|
);
|
|
50
54
|
},
|
|
@@ -6,31 +6,31 @@ import { asNonZeroInt } from './non-zero-int.mjs';
|
|
|
6
6
|
describe('Int test', () => {
|
|
7
7
|
describe(asInt, () => {
|
|
8
8
|
test('accepts valid integers', () => {
|
|
9
|
-
expect(() => asInt(0)).not.
|
|
9
|
+
expect(() => asInt(0)).not.toThrowError();
|
|
10
10
|
|
|
11
|
-
expect(() => asInt(1)).not.
|
|
11
|
+
expect(() => asInt(1)).not.toThrowError();
|
|
12
12
|
|
|
13
|
-
expect(() => asInt(-1)).not.
|
|
13
|
+
expect(() => asInt(-1)).not.toThrowError();
|
|
14
14
|
|
|
15
|
-
expect(() => asInt(42)).not.
|
|
15
|
+
expect(() => asInt(42)).not.toThrowError();
|
|
16
16
|
|
|
17
|
-
expect(() => asInt(-42)).not.
|
|
17
|
+
expect(() => asInt(-42)).not.toThrowError();
|
|
18
18
|
|
|
19
|
-
expect(() => asInt(Number.MAX_SAFE_INTEGER)).not.
|
|
19
|
+
expect(() => asInt(Number.MAX_SAFE_INTEGER)).not.toThrowError();
|
|
20
20
|
|
|
21
|
-
expect(() => asInt(Number.MIN_SAFE_INTEGER)).not.
|
|
21
|
+
expect(() => asInt(Number.MIN_SAFE_INTEGER)).not.toThrowError();
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
test('rejects non-integers', () => {
|
|
25
|
-
expect(() => asInt(Number.NaN)).
|
|
25
|
+
expect(() => asInt(Number.NaN)).toThrowError(TypeError);
|
|
26
26
|
|
|
27
|
-
expect(() => asInt(Number.POSITIVE_INFINITY)).
|
|
27
|
+
expect(() => asInt(Number.POSITIVE_INFINITY)).toThrowError(TypeError);
|
|
28
28
|
|
|
29
|
-
expect(() => asInt(Number.NEGATIVE_INFINITY)).
|
|
29
|
+
expect(() => asInt(Number.NEGATIVE_INFINITY)).toThrowError(TypeError);
|
|
30
30
|
|
|
31
|
-
expect(() => asInt(1.2)).
|
|
31
|
+
expect(() => asInt(1.2)).toThrowError(TypeError);
|
|
32
32
|
|
|
33
|
-
expect(() => asInt(-3.4)).
|
|
33
|
+
expect(() => asInt(-3.4)).toThrowError(TypeError);
|
|
34
34
|
});
|
|
35
35
|
|
|
36
36
|
test('returns the same value for valid inputs', () => {
|
|
@@ -48,7 +48,7 @@ describe('Int test', () => {
|
|
|
48
48
|
{ name: '1.2', value: 1.2 },
|
|
49
49
|
{ name: '-3.4', value: -3.4 },
|
|
50
50
|
] as const)(`asInt($name) should throw a TypeError`, ({ value }) => {
|
|
51
|
-
expect(() => asInt(value)).
|
|
51
|
+
expect(() => asInt(value)).toThrowError(
|
|
52
52
|
new TypeError(`Expected an integer, got: ${value}`),
|
|
53
53
|
);
|
|
54
54
|
});
|