@xylabs/assert 4.13.21 → 4.13.23
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 +143 -84
- package/dist/neutral/assertDefinedEx.d.ts +49 -7
- package/dist/neutral/assertDefinedEx.d.ts.map +1 -1
- package/dist/neutral/assertEx.d.ts +49 -7
- package/dist/neutral/assertEx.d.ts.map +1 -1
- package/dist/neutral/index.mjs.map +1 -1
- package/dist/neutral/types.d.ts +33 -0
- package/dist/neutral/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/assertDefinedEx.ts +57 -8
- package/src/assertEx.ts +56 -8
- package/src/types.ts +34 -0
package/README.md
CHANGED
|
@@ -15,17 +15,12 @@
|
|
|
15
15
|
|
|
16
16
|
Base functionality used throughout XY Labs TypeScript/JavaScript libraries
|
|
17
17
|
|
|
18
|
-
##
|
|
18
|
+
## Reference
|
|
19
19
|
|
|
20
20
|
**@xylabs/assert**
|
|
21
21
|
|
|
22
22
|
***
|
|
23
23
|
|
|
24
|
-
## Type Aliases
|
|
25
|
-
|
|
26
|
-
- [AssertExMessageFunc](#type-aliases/AssertExMessageFunc)
|
|
27
|
-
- [AssertExErrorFunc](#type-aliases/AssertExErrorFunc)
|
|
28
|
-
|
|
29
24
|
## Functions
|
|
30
25
|
|
|
31
26
|
- [assertDefinedEx](#functions/assertDefinedEx)
|
|
@@ -39,13 +34,16 @@ Base functionality used throughout XY Labs TypeScript/JavaScript libraries
|
|
|
39
34
|
|
|
40
35
|
***
|
|
41
36
|
|
|
37
|
+
Implementation of assertDefinedEx that handles all overloads.
|
|
38
|
+
|
|
42
39
|
## Call Signature
|
|
43
40
|
|
|
44
41
|
```ts
|
|
45
42
|
function assertDefinedEx<T>(expr, messageFunc?): T;
|
|
46
43
|
```
|
|
47
44
|
|
|
48
|
-
|
|
45
|
+
Asserts that a value is defined (not undefined) and returns the value.
|
|
46
|
+
Throws an error if the value is undefined.
|
|
49
47
|
|
|
50
48
|
### Type Parameters
|
|
51
49
|
|
|
@@ -53,27 +51,43 @@ Intended for defined checks for variables
|
|
|
53
51
|
|
|
54
52
|
`T`
|
|
55
53
|
|
|
54
|
+
The type of value to check
|
|
55
|
+
|
|
56
56
|
### Parameters
|
|
57
57
|
|
|
58
58
|
### expr
|
|
59
59
|
|
|
60
|
-
Expression to be evaluated for
|
|
60
|
+
Expression to be evaluated for being defined
|
|
61
61
|
|
|
62
62
|
`undefined` | `T`
|
|
63
63
|
|
|
64
64
|
### messageFunc?
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
`AssertExMessageFunc`\<`T`\>
|
|
67
|
+
|
|
68
|
+
Function that returns a message for the error if expression is undefined
|
|
67
69
|
|
|
68
70
|
### Returns
|
|
69
71
|
|
|
70
72
|
`T`
|
|
71
73
|
|
|
72
|
-
|
|
74
|
+
The value of the expression (guaranteed to be defined)
|
|
73
75
|
|
|
74
76
|
### Throws
|
|
75
77
|
|
|
76
|
-
|
|
78
|
+
Error with the message returned by messageFunc
|
|
79
|
+
|
|
80
|
+
### Example
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
// Simple usage with a message function
|
|
84
|
+
const value = assertDefinedEx(possiblyUndefined, () => 'Value must be defined')
|
|
85
|
+
|
|
86
|
+
// Using with type narrowing
|
|
87
|
+
const config: Config | undefined = loadConfig()
|
|
88
|
+
const safeConfig = assertDefinedEx(config, () => 'Config failed to load')
|
|
89
|
+
// safeConfig is now type Config (not Config | undefined)
|
|
90
|
+
```
|
|
77
91
|
|
|
78
92
|
## Call Signature
|
|
79
93
|
|
|
@@ -81,7 +95,8 @@ AssertExError
|
|
|
81
95
|
function assertDefinedEx<T, R>(expr, errorFunc?): T;
|
|
82
96
|
```
|
|
83
97
|
|
|
84
|
-
|
|
98
|
+
Asserts that a value is defined (not undefined) and returns the value.
|
|
99
|
+
Throws a custom error if the value is undefined.
|
|
85
100
|
|
|
86
101
|
### Type Parameters
|
|
87
102
|
|
|
@@ -89,31 +104,44 @@ Intended for defined checks for variables
|
|
|
89
104
|
|
|
90
105
|
`T`
|
|
91
106
|
|
|
107
|
+
The type of value to check
|
|
108
|
+
|
|
92
109
|
### R
|
|
93
110
|
|
|
94
111
|
`R` *extends* `Error`
|
|
95
112
|
|
|
113
|
+
The type of error to throw
|
|
114
|
+
|
|
96
115
|
### Parameters
|
|
97
116
|
|
|
98
117
|
### expr
|
|
99
118
|
|
|
100
|
-
Expression to be evaluated for
|
|
119
|
+
Expression to be evaluated for being defined
|
|
101
120
|
|
|
102
121
|
`undefined` | `T`
|
|
103
122
|
|
|
104
123
|
### errorFunc?
|
|
105
124
|
|
|
106
|
-
|
|
125
|
+
`AssertExErrorFunc`\<`T`, `R`\>
|
|
126
|
+
|
|
127
|
+
Function that returns a custom error instance if expression is undefined
|
|
107
128
|
|
|
108
129
|
### Returns
|
|
109
130
|
|
|
110
131
|
`T`
|
|
111
132
|
|
|
112
|
-
|
|
133
|
+
The value of the expression (guaranteed to be defined)
|
|
113
134
|
|
|
114
135
|
### Throws
|
|
115
136
|
|
|
116
|
-
|
|
137
|
+
Custom error returned by errorFunc
|
|
138
|
+
|
|
139
|
+
### Example
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
// Using with a custom error
|
|
143
|
+
const user = assertDefinedEx(getUser(), () => new UserNotFoundError('User not found'))
|
|
144
|
+
```
|
|
117
145
|
|
|
118
146
|
## Call Signature
|
|
119
147
|
|
|
@@ -121,25 +149,38 @@ AssertExError
|
|
|
121
149
|
function assertDefinedEx<T>(expr): T;
|
|
122
150
|
```
|
|
123
151
|
|
|
152
|
+
Asserts that a value is defined (not undefined) and returns the value.
|
|
153
|
+
Throws an error if the value is undefined.
|
|
154
|
+
|
|
124
155
|
### Type Parameters
|
|
125
156
|
|
|
126
157
|
### T
|
|
127
158
|
|
|
128
159
|
`T`
|
|
129
160
|
|
|
161
|
+
The type of value to check
|
|
162
|
+
|
|
130
163
|
### Parameters
|
|
131
164
|
|
|
132
165
|
### expr
|
|
133
166
|
|
|
167
|
+
Expression to be evaluated for being defined
|
|
168
|
+
|
|
134
169
|
`undefined` | `T`
|
|
135
170
|
|
|
136
171
|
### Returns
|
|
137
172
|
|
|
138
173
|
`T`
|
|
139
174
|
|
|
175
|
+
The value of the expression (guaranteed to be defined)
|
|
176
|
+
|
|
140
177
|
### Deprecated
|
|
141
178
|
|
|
142
|
-
- passing a message will soon be required
|
|
179
|
+
Use overload with message function instead - passing a message will soon be required
|
|
180
|
+
|
|
181
|
+
### Throws
|
|
182
|
+
|
|
183
|
+
Error with a generic message
|
|
143
184
|
|
|
144
185
|
## Call Signature
|
|
145
186
|
|
|
@@ -147,29 +188,44 @@ function assertDefinedEx<T>(expr): T;
|
|
|
147
188
|
function assertDefinedEx<T>(expr, message?): T;
|
|
148
189
|
```
|
|
149
190
|
|
|
191
|
+
Asserts that a value is defined (not undefined) and returns the value.
|
|
192
|
+
Throws an error with the provided message if the value is undefined.
|
|
193
|
+
|
|
150
194
|
### Type Parameters
|
|
151
195
|
|
|
152
196
|
### T
|
|
153
197
|
|
|
154
198
|
`T`
|
|
155
199
|
|
|
200
|
+
The type of value to check
|
|
201
|
+
|
|
156
202
|
### Parameters
|
|
157
203
|
|
|
158
204
|
### expr
|
|
159
205
|
|
|
206
|
+
Expression to be evaluated for being defined
|
|
207
|
+
|
|
160
208
|
`undefined` | `T`
|
|
161
209
|
|
|
162
210
|
### message?
|
|
163
211
|
|
|
164
212
|
`string`
|
|
165
213
|
|
|
214
|
+
Error message if expression is undefined
|
|
215
|
+
|
|
166
216
|
### Returns
|
|
167
217
|
|
|
168
218
|
`T`
|
|
169
219
|
|
|
220
|
+
The value of the expression (guaranteed to be defined)
|
|
221
|
+
|
|
170
222
|
### Deprecated
|
|
171
223
|
|
|
172
|
-
|
|
224
|
+
Replace string with () => string for consistency
|
|
225
|
+
|
|
226
|
+
### Throws
|
|
227
|
+
|
|
228
|
+
Error with the provided message
|
|
173
229
|
|
|
174
230
|
### <a id="assertEx"></a>assertEx
|
|
175
231
|
|
|
@@ -177,13 +233,16 @@ function assertDefinedEx<T>(expr, message?): T;
|
|
|
177
233
|
|
|
178
234
|
***
|
|
179
235
|
|
|
236
|
+
Implementation of assertEx that handles all overloads.
|
|
237
|
+
|
|
180
238
|
## Call Signature
|
|
181
239
|
|
|
182
240
|
```ts
|
|
183
241
|
function assertEx<T>(expr, messageFunc?): T;
|
|
184
242
|
```
|
|
185
243
|
|
|
186
|
-
|
|
244
|
+
Asserts that an expression is truthy and returns the value.
|
|
245
|
+
Throws an error if the expression is falsy.
|
|
187
246
|
|
|
188
247
|
### Type Parameters
|
|
189
248
|
|
|
@@ -191,6 +250,8 @@ Intended for simple truthiness checks for variables
|
|
|
191
250
|
|
|
192
251
|
`T`
|
|
193
252
|
|
|
253
|
+
The type of value to check
|
|
254
|
+
|
|
194
255
|
### Parameters
|
|
195
256
|
|
|
196
257
|
### expr
|
|
@@ -201,17 +262,31 @@ Expression to be evaluated for truthiness
|
|
|
201
262
|
|
|
202
263
|
### messageFunc?
|
|
203
264
|
|
|
204
|
-
|
|
265
|
+
`AssertExMessageFunc`\<`T`\>
|
|
266
|
+
|
|
267
|
+
Function that returns a message for the error if expression is falsy
|
|
205
268
|
|
|
206
269
|
### Returns
|
|
207
270
|
|
|
208
271
|
`T`
|
|
209
272
|
|
|
210
|
-
|
|
273
|
+
The value of the expression (guaranteed to be truthy)
|
|
211
274
|
|
|
212
275
|
### Throws
|
|
213
276
|
|
|
214
|
-
|
|
277
|
+
Error with the message returned by messageFunc
|
|
278
|
+
|
|
279
|
+
### Example
|
|
280
|
+
|
|
281
|
+
```typescript
|
|
282
|
+
// Simple usage with a message function
|
|
283
|
+
const value = assertEx(possiblyFalsy, () => 'Value must be truthy')
|
|
284
|
+
|
|
285
|
+
// Using with type narrowing
|
|
286
|
+
const config: Config | null = loadConfig()
|
|
287
|
+
const safeConfig = assertEx(config, () => 'Config failed to load')
|
|
288
|
+
// safeConfig is now type Config (not Config | null)
|
|
289
|
+
```
|
|
215
290
|
|
|
216
291
|
## Call Signature
|
|
217
292
|
|
|
@@ -219,7 +294,8 @@ AssertExError
|
|
|
219
294
|
function assertEx<T, R>(expr, errorFunc?): T;
|
|
220
295
|
```
|
|
221
296
|
|
|
222
|
-
|
|
297
|
+
Asserts that an expression is truthy and returns the value.
|
|
298
|
+
Throws a custom error if the expression is falsy.
|
|
223
299
|
|
|
224
300
|
### Type Parameters
|
|
225
301
|
|
|
@@ -227,10 +303,14 @@ Intended for simple truthiness checks for variables
|
|
|
227
303
|
|
|
228
304
|
`T`
|
|
229
305
|
|
|
306
|
+
The type of value to check
|
|
307
|
+
|
|
230
308
|
### R
|
|
231
309
|
|
|
232
310
|
`R` *extends* `Error`
|
|
233
311
|
|
|
312
|
+
The type of error to throw
|
|
313
|
+
|
|
234
314
|
### Parameters
|
|
235
315
|
|
|
236
316
|
### expr
|
|
@@ -241,17 +321,26 @@ Expression to be evaluated for truthiness
|
|
|
241
321
|
|
|
242
322
|
### errorFunc?
|
|
243
323
|
|
|
244
|
-
|
|
324
|
+
`AssertExErrorFunc`\<`T`, `R`\>
|
|
325
|
+
|
|
326
|
+
Function that returns a custom error instance if expression is falsy
|
|
245
327
|
|
|
246
328
|
### Returns
|
|
247
329
|
|
|
248
330
|
`T`
|
|
249
331
|
|
|
250
|
-
|
|
332
|
+
The value of the expression (guaranteed to be truthy)
|
|
251
333
|
|
|
252
334
|
### Throws
|
|
253
335
|
|
|
254
|
-
|
|
336
|
+
Custom error returned by errorFunc
|
|
337
|
+
|
|
338
|
+
### Example
|
|
339
|
+
|
|
340
|
+
```typescript
|
|
341
|
+
// Using with a custom error
|
|
342
|
+
const user = assertEx(getUser(), () => new UserNotFoundError('User not found'))
|
|
343
|
+
```
|
|
255
344
|
|
|
256
345
|
## Call Signature
|
|
257
346
|
|
|
@@ -259,25 +348,38 @@ AssertExError
|
|
|
259
348
|
function assertEx<T>(expr): T;
|
|
260
349
|
```
|
|
261
350
|
|
|
351
|
+
Asserts that an expression is truthy and returns the value.
|
|
352
|
+
Throws an error if the expression is falsy.
|
|
353
|
+
|
|
262
354
|
### Type Parameters
|
|
263
355
|
|
|
264
356
|
### T
|
|
265
357
|
|
|
266
358
|
`T`
|
|
267
359
|
|
|
360
|
+
The type of value to check
|
|
361
|
+
|
|
268
362
|
### Parameters
|
|
269
363
|
|
|
270
364
|
### expr
|
|
271
365
|
|
|
366
|
+
Expression to be evaluated for truthiness
|
|
367
|
+
|
|
272
368
|
`undefined` | `null` | `T`
|
|
273
369
|
|
|
274
370
|
### Returns
|
|
275
371
|
|
|
276
372
|
`T`
|
|
277
373
|
|
|
374
|
+
The value of the expression (guaranteed to be truthy)
|
|
375
|
+
|
|
278
376
|
### Deprecated
|
|
279
377
|
|
|
280
|
-
- passing a message will soon be required
|
|
378
|
+
Use overload with message function instead - passing a message will soon be required
|
|
379
|
+
|
|
380
|
+
### Throws
|
|
381
|
+
|
|
382
|
+
Error with a generic message
|
|
281
383
|
|
|
282
384
|
## Call Signature
|
|
283
385
|
|
|
@@ -285,87 +387,44 @@ function assertEx<T>(expr): T;
|
|
|
285
387
|
function assertEx<T>(expr, message?): T;
|
|
286
388
|
```
|
|
287
389
|
|
|
390
|
+
Asserts that an expression is truthy and returns the value.
|
|
391
|
+
Throws an error with the provided message if the expression is falsy.
|
|
392
|
+
|
|
288
393
|
### Type Parameters
|
|
289
394
|
|
|
290
395
|
### T
|
|
291
396
|
|
|
292
397
|
`T`
|
|
293
398
|
|
|
399
|
+
The type of value to check
|
|
400
|
+
|
|
294
401
|
### Parameters
|
|
295
402
|
|
|
296
403
|
### expr
|
|
297
404
|
|
|
405
|
+
Expression to be evaluated for truthiness
|
|
406
|
+
|
|
298
407
|
`undefined` | `null` | `T`
|
|
299
408
|
|
|
300
409
|
### message?
|
|
301
410
|
|
|
302
411
|
`string`
|
|
303
412
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
`T`
|
|
307
|
-
|
|
308
|
-
### Deprecated
|
|
309
|
-
|
|
310
|
-
- replace string with () => string
|
|
311
|
-
|
|
312
|
-
### type-aliases
|
|
313
|
-
|
|
314
|
-
### <a id="AssertExErrorFunc"></a>AssertExErrorFunc
|
|
315
|
-
|
|
316
|
-
[**@xylabs/assert**](#../README)
|
|
317
|
-
|
|
318
|
-
***
|
|
319
|
-
|
|
320
|
-
```ts
|
|
321
|
-
type AssertExErrorFunc<T, R> = (value?) => R;
|
|
322
|
-
```
|
|
323
|
-
|
|
324
|
-
## Type Parameters
|
|
325
|
-
|
|
326
|
-
### T
|
|
327
|
-
|
|
328
|
-
`T`
|
|
329
|
-
|
|
330
|
-
### R
|
|
331
|
-
|
|
332
|
-
`R` *extends* `Error`
|
|
333
|
-
|
|
334
|
-
## Parameters
|
|
413
|
+
Error message if expression is falsy
|
|
335
414
|
|
|
336
|
-
###
|
|
337
|
-
|
|
338
|
-
`T` | `null`
|
|
339
|
-
|
|
340
|
-
## Returns
|
|
341
|
-
|
|
342
|
-
`R`
|
|
343
|
-
|
|
344
|
-
### <a id="AssertExMessageFunc"></a>AssertExMessageFunc
|
|
345
|
-
|
|
346
|
-
[**@xylabs/assert**](#../README)
|
|
347
|
-
|
|
348
|
-
***
|
|
349
|
-
|
|
350
|
-
```ts
|
|
351
|
-
type AssertExMessageFunc<T> = (value?) => string;
|
|
352
|
-
```
|
|
353
|
-
|
|
354
|
-
## Type Parameters
|
|
355
|
-
|
|
356
|
-
### T
|
|
415
|
+
### Returns
|
|
357
416
|
|
|
358
417
|
`T`
|
|
359
418
|
|
|
360
|
-
|
|
419
|
+
The value of the expression (guaranteed to be truthy)
|
|
361
420
|
|
|
362
|
-
###
|
|
421
|
+
### Deprecated
|
|
363
422
|
|
|
364
|
-
|
|
423
|
+
Replace string with () => string for consistency
|
|
365
424
|
|
|
366
|
-
|
|
425
|
+
### Throws
|
|
367
426
|
|
|
368
|
-
|
|
427
|
+
Error with the provided message
|
|
369
428
|
|
|
370
429
|
|
|
371
430
|
Part of [sdk-js](https://www.npmjs.com/package/@xyo-network/sdk-js)
|
|
@@ -1,21 +1,63 @@
|
|
|
1
1
|
import type { AssertExErrorFunc, AssertExMessageFunc } from './types.ts';
|
|
2
2
|
/**
|
|
3
|
+
* Asserts that a value is defined (not undefined) and returns the value.
|
|
4
|
+
* Throws an error if the value is undefined.
|
|
3
5
|
*
|
|
4
|
-
*
|
|
6
|
+
* @template T - The type of value to check
|
|
7
|
+
* @param expr - Expression to be evaluated for being defined
|
|
8
|
+
* @param messageFunc - Function that returns a message for the error if expression is undefined
|
|
9
|
+
* @throws Error with the message returned by messageFunc
|
|
10
|
+
* @returns The value of the expression (guaranteed to be defined)
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* // Simple usage with a message function
|
|
14
|
+
* const value = assertDefinedEx(possiblyUndefined, () => 'Value must be defined')
|
|
5
15
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
16
|
+
* // Using with type narrowing
|
|
17
|
+
* const config: Config | undefined = loadConfig()
|
|
18
|
+
* const safeConfig = assertDefinedEx(config, () => 'Config failed to load')
|
|
19
|
+
* // safeConfig is now type Config (not Config | undefined)
|
|
20
|
+
* ```
|
|
10
21
|
*/
|
|
11
22
|
declare function assertDefinedEx<T>(expr: T | undefined, messageFunc?: AssertExMessageFunc<T>): T;
|
|
23
|
+
/**
|
|
24
|
+
* Asserts that a value is defined (not undefined) and returns the value.
|
|
25
|
+
* Throws a custom error if the value is undefined.
|
|
26
|
+
*
|
|
27
|
+
* @template T - The type of value to check
|
|
28
|
+
* @template R - The type of error to throw
|
|
29
|
+
* @param expr - Expression to be evaluated for being defined
|
|
30
|
+
* @param errorFunc - Function that returns a custom error instance if expression is undefined
|
|
31
|
+
* @throws Custom error returned by errorFunc
|
|
32
|
+
* @returns The value of the expression (guaranteed to be defined)
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* // Using with a custom error
|
|
36
|
+
* const user = assertDefinedEx(getUser(), () => new UserNotFoundError('User not found'))
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
12
39
|
declare function assertDefinedEx<T, R extends Error>(expr: T | undefined, errorFunc?: AssertExErrorFunc<T, R>): T;
|
|
13
40
|
/**
|
|
14
|
-
*
|
|
41
|
+
* Asserts that a value is defined (not undefined) and returns the value.
|
|
42
|
+
* Throws an error if the value is undefined.
|
|
43
|
+
*
|
|
44
|
+
* @deprecated Use overload with message function instead - passing a message will soon be required
|
|
45
|
+
* @template T - The type of value to check
|
|
46
|
+
* @param expr - Expression to be evaluated for being defined
|
|
47
|
+
* @throws Error with a generic message
|
|
48
|
+
* @returns The value of the expression (guaranteed to be defined)
|
|
15
49
|
*/
|
|
16
50
|
declare function assertDefinedEx<T>(expr: T | undefined): T;
|
|
17
51
|
/**
|
|
18
|
-
*
|
|
52
|
+
* Asserts that a value is defined (not undefined) and returns the value.
|
|
53
|
+
* Throws an error with the provided message if the value is undefined.
|
|
54
|
+
*
|
|
55
|
+
* @deprecated Replace string with () => string for consistency
|
|
56
|
+
* @template T - The type of value to check
|
|
57
|
+
* @param expr - Expression to be evaluated for being defined
|
|
58
|
+
* @param message - Error message if expression is undefined
|
|
59
|
+
* @throws Error with the provided message
|
|
60
|
+
* @returns The value of the expression (guaranteed to be defined)
|
|
19
61
|
*/
|
|
20
62
|
declare function assertDefinedEx<T>(expr: T | undefined, message?: string): T;
|
|
21
63
|
export { assertDefinedEx };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assertDefinedEx.d.ts","sourceRoot":"","sources":["../../src/assertDefinedEx.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAExE
|
|
1
|
+
{"version":3,"file":"assertDefinedEx.d.ts","sourceRoot":"","sources":["../../src/assertDefinedEx.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAExE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,iBAAS,eAAe,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,SAAS,EAAE,WAAW,CAAC,EAAE,mBAAmB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;AAEzF;;;;;;;;;;;;;;;GAeG;AACH,iBAAS,eAAe,CAAC,CAAC,EAAE,CAAC,SAAS,KAAK,EAAE,IAAI,EAAE,CAAC,GAAG,SAAS,EAAE,SAAS,CAAC,EAAE,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAA;AAEzG;;;;;;;;;GASG;AACH,iBAAS,eAAe,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,SAAS,GAAG,CAAC,CAAA;AAEnD;;;;;;;;;;GAUG;AACH,iBAAS,eAAe,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,SAAS,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,CAAC,CAAA;AAmBrE,OAAO,EAAE,eAAe,EAAE,CAAA"}
|
|
@@ -1,21 +1,63 @@
|
|
|
1
1
|
import type { AssertExErrorFunc, AssertExMessageFunc } from './types.ts';
|
|
2
2
|
/**
|
|
3
|
+
* Asserts that an expression is truthy and returns the value.
|
|
4
|
+
* Throws an error if the expression is falsy.
|
|
3
5
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
+
* @template T - The type of value to check
|
|
6
7
|
* @param expr - Expression to be evaluated for truthiness
|
|
7
|
-
* @param
|
|
8
|
-
* @throws
|
|
9
|
-
* @returns
|
|
8
|
+
* @param messageFunc - Function that returns a message for the error if expression is falsy
|
|
9
|
+
* @throws Error with the message returned by messageFunc
|
|
10
|
+
* @returns The value of the expression (guaranteed to be truthy)
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* // Simple usage with a message function
|
|
14
|
+
* const value = assertEx(possiblyFalsy, () => 'Value must be truthy')
|
|
15
|
+
*
|
|
16
|
+
* // Using with type narrowing
|
|
17
|
+
* const config: Config | null = loadConfig()
|
|
18
|
+
* const safeConfig = assertEx(config, () => 'Config failed to load')
|
|
19
|
+
* // safeConfig is now type Config (not Config | null)
|
|
20
|
+
* ```
|
|
10
21
|
*/
|
|
11
22
|
declare function assertEx<T>(expr: T | null | undefined, messageFunc?: AssertExMessageFunc<T>): T;
|
|
23
|
+
/**
|
|
24
|
+
* Asserts that an expression is truthy and returns the value.
|
|
25
|
+
* Throws a custom error if the expression is falsy.
|
|
26
|
+
*
|
|
27
|
+
* @template T - The type of value to check
|
|
28
|
+
* @template R - The type of error to throw
|
|
29
|
+
* @param expr - Expression to be evaluated for truthiness
|
|
30
|
+
* @param errorFunc - Function that returns a custom error instance if expression is falsy
|
|
31
|
+
* @throws Custom error returned by errorFunc
|
|
32
|
+
* @returns The value of the expression (guaranteed to be truthy)
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* // Using with a custom error
|
|
36
|
+
* const user = assertEx(getUser(), () => new UserNotFoundError('User not found'))
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
12
39
|
declare function assertEx<T, R extends Error>(expr: T | null | undefined, errorFunc?: AssertExErrorFunc<T, R>): T;
|
|
13
40
|
/**
|
|
14
|
-
*
|
|
41
|
+
* Asserts that an expression is truthy and returns the value.
|
|
42
|
+
* Throws an error if the expression is falsy.
|
|
43
|
+
*
|
|
44
|
+
* @deprecated Use overload with message function instead - passing a message will soon be required
|
|
45
|
+
* @template T - The type of value to check
|
|
46
|
+
* @param expr - Expression to be evaluated for truthiness
|
|
47
|
+
* @throws Error with a generic message
|
|
48
|
+
* @returns The value of the expression (guaranteed to be truthy)
|
|
15
49
|
*/
|
|
16
50
|
declare function assertEx<T>(expr: T | null | undefined): T;
|
|
17
51
|
/**
|
|
18
|
-
*
|
|
52
|
+
* Asserts that an expression is truthy and returns the value.
|
|
53
|
+
* Throws an error with the provided message if the expression is falsy.
|
|
54
|
+
*
|
|
55
|
+
* @deprecated Replace string with () => string for consistency
|
|
56
|
+
* @template T - The type of value to check
|
|
57
|
+
* @param expr - Expression to be evaluated for truthiness
|
|
58
|
+
* @param message - Error message if expression is falsy
|
|
59
|
+
* @throws Error with the provided message
|
|
60
|
+
* @returns The value of the expression (guaranteed to be truthy)
|
|
19
61
|
*/
|
|
20
62
|
declare function assertEx<T>(expr: T | null | undefined, message?: string): T;
|
|
21
63
|
export { assertEx };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assertEx.d.ts","sourceRoot":"","sources":["../../src/assertEx.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAExE
|
|
1
|
+
{"version":3,"file":"assertEx.d.ts","sourceRoot":"","sources":["../../src/assertEx.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAExE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,iBAAS,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,EAAE,WAAW,CAAC,EAAE,mBAAmB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;AAEzF;;;;;;;;;;;;;;;GAeG;AACH,iBAAS,QAAQ,CAAC,CAAC,EAAE,CAAC,SAAS,KAAK,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,EAAE,SAAS,CAAC,EAAE,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAA;AAEzG;;;;;;;;;GASG;AACH,iBAAS,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,GAAG,CAAC,CAAA;AAEnD;;;;;;;;;;GAUG;AACH,iBAAS,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,CAAC,CAAA;AAmBrE,OAAO,EAAE,QAAQ,EAAE,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/assertDefinedEx.ts","../../src/assertEx.ts"],"sourcesContent":["import type { AssertExErrorFunc, AssertExMessageFunc } from './types.ts'\n\n/**\n
|
|
1
|
+
{"version":3,"sources":["../../src/assertDefinedEx.ts","../../src/assertEx.ts"],"sourcesContent":["import type { AssertExErrorFunc, AssertExMessageFunc } from './types.ts'\n\n/**\n * Asserts that a value is defined (not undefined) and returns the value.\n * Throws an error if the value is undefined.\n *\n * @template T - The type of value to check\n * @param expr - Expression to be evaluated for being defined\n * @param messageFunc - Function that returns a message for the error if expression is undefined\n * @throws Error with the message returned by messageFunc\n * @returns The value of the expression (guaranteed to be defined)\n * @example\n * ```typescript\n * // Simple usage with a message function\n * const value = assertDefinedEx(possiblyUndefined, () => 'Value must be defined')\n *\n * // Using with type narrowing\n * const config: Config | undefined = loadConfig()\n * const safeConfig = assertDefinedEx(config, () => 'Config failed to load')\n * // safeConfig is now type Config (not Config | undefined)\n * ```\n */\nfunction assertDefinedEx<T>(expr: T | undefined, messageFunc?: AssertExMessageFunc<T>): T\n\n/**\n * Asserts that a value is defined (not undefined) and returns the value.\n * Throws a custom error if the value is undefined.\n *\n * @template T - The type of value to check\n * @template R - The type of error to throw\n * @param expr - Expression to be evaluated for being defined\n * @param errorFunc - Function that returns a custom error instance if expression is undefined\n * @throws Custom error returned by errorFunc\n * @returns The value of the expression (guaranteed to be defined)\n * @example\n * ```typescript\n * // Using with a custom error\n * const user = assertDefinedEx(getUser(), () => new UserNotFoundError('User not found'))\n * ```\n */\nfunction assertDefinedEx<T, R extends Error>(expr: T | undefined, errorFunc?: AssertExErrorFunc<T, R>): T\n\n/**\n * Asserts that a value is defined (not undefined) and returns the value.\n * Throws an error if the value is undefined.\n *\n * @deprecated Use overload with message function instead - passing a message will soon be required\n * @template T - The type of value to check\n * @param expr - Expression to be evaluated for being defined\n * @throws Error with a generic message\n * @returns The value of the expression (guaranteed to be defined)\n */\nfunction assertDefinedEx<T>(expr: T | undefined): T\n\n/**\n * Asserts that a value is defined (not undefined) and returns the value.\n * Throws an error with the provided message if the value is undefined.\n *\n * @deprecated Replace string with () => string for consistency\n * @template T - The type of value to check\n * @param expr - Expression to be evaluated for being defined\n * @param message - Error message if expression is undefined\n * @throws Error with the provided message\n * @returns The value of the expression (guaranteed to be defined)\n */\nfunction assertDefinedEx<T>(expr: T | undefined, message?: string): T\n\n/**\n * Implementation of assertDefinedEx that handles all overloads.\n *\n */\nfunction assertDefinedEx<T, R extends Error, P extends string | AssertExMessageFunc<T> | AssertExErrorFunc<T, R>>(\n expr: T | undefined,\n messageOrFunc?: P,\n): T {\n if (expr !== undefined) return expr\n if (typeof messageOrFunc === 'function') {\n const errorOrMessage = messageOrFunc(expr)\n throw typeof errorOrMessage === 'string' ? new Error(errorOrMessage) : errorOrMessage\n }\n // a string was sent\n throw new Error(messageOrFunc)\n}\n\nexport { assertDefinedEx }\n","import type { AssertExErrorFunc, AssertExMessageFunc } from './types.ts'\n\n/**\n * Asserts that an expression is truthy and returns the value.\n * Throws an error if the expression is falsy.\n *\n * @template T - The type of value to check\n * @param expr - Expression to be evaluated for truthiness\n * @param messageFunc - Function that returns a message for the error if expression is falsy\n * @throws Error with the message returned by messageFunc\n * @returns The value of the expression (guaranteed to be truthy)\n * @example\n * ```typescript\n * // Simple usage with a message function\n * const value = assertEx(possiblyFalsy, () => 'Value must be truthy')\n *\n * // Using with type narrowing\n * const config: Config | null = loadConfig()\n * const safeConfig = assertEx(config, () => 'Config failed to load')\n * // safeConfig is now type Config (not Config | null)\n * ```\n */\nfunction assertEx<T>(expr: T | null | undefined, messageFunc?: AssertExMessageFunc<T>): T\n\n/**\n * Asserts that an expression is truthy and returns the value.\n * Throws a custom error if the expression is falsy.\n *\n * @template T - The type of value to check\n * @template R - The type of error to throw\n * @param expr - Expression to be evaluated for truthiness\n * @param errorFunc - Function that returns a custom error instance if expression is falsy\n * @throws Custom error returned by errorFunc\n * @returns The value of the expression (guaranteed to be truthy)\n * @example\n * ```typescript\n * // Using with a custom error\n * const user = assertEx(getUser(), () => new UserNotFoundError('User not found'))\n * ```\n */\nfunction assertEx<T, R extends Error>(expr: T | null | undefined, errorFunc?: AssertExErrorFunc<T, R>): T\n\n/**\n * Asserts that an expression is truthy and returns the value.\n * Throws an error if the expression is falsy.\n *\n * @deprecated Use overload with message function instead - passing a message will soon be required\n * @template T - The type of value to check\n * @param expr - Expression to be evaluated for truthiness\n * @throws Error with a generic message\n * @returns The value of the expression (guaranteed to be truthy)\n */\nfunction assertEx<T>(expr: T | null | undefined): T\n\n/**\n * Asserts that an expression is truthy and returns the value.\n * Throws an error with the provided message if the expression is falsy.\n *\n * @deprecated Replace string with () => string for consistency\n * @template T - The type of value to check\n * @param expr - Expression to be evaluated for truthiness\n * @param message - Error message if expression is falsy\n * @throws Error with the provided message\n * @returns The value of the expression (guaranteed to be truthy)\n */\nfunction assertEx<T>(expr: T | null | undefined, message?: string): T\n\n/**\n * Implementation of assertEx that handles all overloads.\n */\nfunction assertEx<T, R extends Error, P extends string | AssertExMessageFunc<T> | AssertExErrorFunc<T, R>>(\n expr: T | null | undefined,\n messageOrFunc?: P,\n): T {\n // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions\n if (expr) return expr\n if (typeof messageOrFunc === 'function') {\n const errorOrMessage = messageOrFunc(expr)\n throw typeof errorOrMessage === 'string' ? new Error(errorOrMessage) : errorOrMessage\n }\n // a string was sent\n throw new Error(messageOrFunc)\n}\n\nexport { assertEx }\n"],"mappings":";AAuEA,SAAS,gBACP,MACA,eACG;AACH,MAAI,SAAS,OAAW,QAAO;AAC/B,MAAI,OAAO,kBAAkB,YAAY;AACvC,UAAM,iBAAiB,cAAc,IAAI;AACzC,UAAM,OAAO,mBAAmB,WAAW,IAAI,MAAM,cAAc,IAAI;AAAA,EACzE;AAEA,QAAM,IAAI,MAAM,aAAa;AAC/B;;;ACZA,SAAS,SACP,MACA,eACG;AAEH,MAAI,KAAM,QAAO;AACjB,MAAI,OAAO,kBAAkB,YAAY;AACvC,UAAM,iBAAiB,cAAc,IAAI;AACzC,UAAM,OAAO,mBAAmB,WAAW,IAAI,MAAM,cAAc,IAAI;AAAA,EACzE;AAEA,QAAM,IAAI,MAAM,aAAa;AAC/B;","names":[]}
|
package/dist/neutral/types.d.ts
CHANGED
|
@@ -1,3 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
* A function that takes a possibly null or undefined value and returns an error message string.
|
|
4
|
+
* Used in assertion functions to provide custom error messages.
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
* @template T - The type of value being asserted
|
|
8
|
+
* @param value - The value being asserted (may be null or undefined)
|
|
9
|
+
* @returns A string message to be used in the error thrown by the assertion
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const messageFunc: AssertExMessageFunc<User> = (user) =>
|
|
13
|
+
* `User ${user ? user.id : 'unknown'} is not valid or accessible`
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
1
16
|
export type AssertExMessageFunc<T> = (value?: T | null) => string;
|
|
17
|
+
/**
|
|
18
|
+
* A function that takes a possibly null or undefined value and returns a custom error instance.
|
|
19
|
+
* Used in assertion functions to provide specific error types with custom properties.
|
|
20
|
+
*
|
|
21
|
+
* @internal
|
|
22
|
+
* @template T - The type of value being asserted
|
|
23
|
+
* @template R - The specific error type to be returned, must extend Error
|
|
24
|
+
* @param value - The value being asserted (may be null or undefined)
|
|
25
|
+
* @returns An instance of the custom error type
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const errorFunc: AssertExErrorFunc<User, UserNotFoundError> = (user) =>
|
|
29
|
+
* new UserNotFoundError(`User ${user?.id || 'unknown'} not found`, {
|
|
30
|
+
* userId: user?.id,
|
|
31
|
+
* timestamp: new Date()
|
|
32
|
+
* })
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
2
35
|
export type AssertExErrorFunc<T, R extends Error> = (value?: T | null) => R;
|
|
3
36
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,mBAAmB,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,GAAG,IAAI,KAAK,MAAM,CAAA;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,mBAAmB,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,GAAG,IAAI,KAAK,MAAM,CAAA;AAEjE;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,MAAM,iBAAiB,CAAC,CAAC,EAAE,CAAC,SAAS,KAAK,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,GAAG,IAAI,KAAK,CAAC,CAAA"}
|
package/package.json
CHANGED
package/src/assertDefinedEx.ts
CHANGED
|
@@ -1,25 +1,74 @@
|
|
|
1
1
|
import type { AssertExErrorFunc, AssertExMessageFunc } from './types.ts'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
+
* Asserts that a value is defined (not undefined) and returns the value.
|
|
5
|
+
* Throws an error if the value is undefined.
|
|
4
6
|
*
|
|
5
|
-
*
|
|
7
|
+
* @template T - The type of value to check
|
|
8
|
+
* @param expr - Expression to be evaluated for being defined
|
|
9
|
+
* @param messageFunc - Function that returns a message for the error if expression is undefined
|
|
10
|
+
* @throws Error with the message returned by messageFunc
|
|
11
|
+
* @returns The value of the expression (guaranteed to be defined)
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Simple usage with a message function
|
|
15
|
+
* const value = assertDefinedEx(possiblyUndefined, () => 'Value must be defined')
|
|
6
16
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
17
|
+
* // Using with type narrowing
|
|
18
|
+
* const config: Config | undefined = loadConfig()
|
|
19
|
+
* const safeConfig = assertDefinedEx(config, () => 'Config failed to load')
|
|
20
|
+
* // safeConfig is now type Config (not Config | undefined)
|
|
21
|
+
* ```
|
|
11
22
|
*/
|
|
12
|
-
|
|
13
23
|
function assertDefinedEx<T>(expr: T | undefined, messageFunc?: AssertExMessageFunc<T>): T
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Asserts that a value is defined (not undefined) and returns the value.
|
|
27
|
+
* Throws a custom error if the value is undefined.
|
|
28
|
+
*
|
|
29
|
+
* @template T - The type of value to check
|
|
30
|
+
* @template R - The type of error to throw
|
|
31
|
+
* @param expr - Expression to be evaluated for being defined
|
|
32
|
+
* @param errorFunc - Function that returns a custom error instance if expression is undefined
|
|
33
|
+
* @throws Custom error returned by errorFunc
|
|
34
|
+
* @returns The value of the expression (guaranteed to be defined)
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* // Using with a custom error
|
|
38
|
+
* const user = assertDefinedEx(getUser(), () => new UserNotFoundError('User not found'))
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
14
41
|
function assertDefinedEx<T, R extends Error>(expr: T | undefined, errorFunc?: AssertExErrorFunc<T, R>): T
|
|
42
|
+
|
|
15
43
|
/**
|
|
16
|
-
*
|
|
44
|
+
* Asserts that a value is defined (not undefined) and returns the value.
|
|
45
|
+
* Throws an error if the value is undefined.
|
|
46
|
+
*
|
|
47
|
+
* @deprecated Use overload with message function instead - passing a message will soon be required
|
|
48
|
+
* @template T - The type of value to check
|
|
49
|
+
* @param expr - Expression to be evaluated for being defined
|
|
50
|
+
* @throws Error with a generic message
|
|
51
|
+
* @returns The value of the expression (guaranteed to be defined)
|
|
17
52
|
*/
|
|
18
53
|
function assertDefinedEx<T>(expr: T | undefined): T
|
|
54
|
+
|
|
19
55
|
/**
|
|
20
|
-
*
|
|
56
|
+
* Asserts that a value is defined (not undefined) and returns the value.
|
|
57
|
+
* Throws an error with the provided message if the value is undefined.
|
|
58
|
+
*
|
|
59
|
+
* @deprecated Replace string with () => string for consistency
|
|
60
|
+
* @template T - The type of value to check
|
|
61
|
+
* @param expr - Expression to be evaluated for being defined
|
|
62
|
+
* @param message - Error message if expression is undefined
|
|
63
|
+
* @throws Error with the provided message
|
|
64
|
+
* @returns The value of the expression (guaranteed to be defined)
|
|
21
65
|
*/
|
|
22
66
|
function assertDefinedEx<T>(expr: T | undefined, message?: string): T
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Implementation of assertDefinedEx that handles all overloads.
|
|
70
|
+
*
|
|
71
|
+
*/
|
|
23
72
|
function assertDefinedEx<T, R extends Error, P extends string | AssertExMessageFunc<T> | AssertExErrorFunc<T, R>>(
|
|
24
73
|
expr: T | undefined,
|
|
25
74
|
messageOrFunc?: P,
|
package/src/assertEx.ts
CHANGED
|
@@ -1,25 +1,73 @@
|
|
|
1
1
|
import type { AssertExErrorFunc, AssertExMessageFunc } from './types.ts'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
+
* Asserts that an expression is truthy and returns the value.
|
|
5
|
+
* Throws an error if the expression is falsy.
|
|
4
6
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
+
* @template T - The type of value to check
|
|
7
8
|
* @param expr - Expression to be evaluated for truthiness
|
|
8
|
-
* @param
|
|
9
|
-
* @throws
|
|
10
|
-
* @returns
|
|
9
|
+
* @param messageFunc - Function that returns a message for the error if expression is falsy
|
|
10
|
+
* @throws Error with the message returned by messageFunc
|
|
11
|
+
* @returns The value of the expression (guaranteed to be truthy)
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Simple usage with a message function
|
|
15
|
+
* const value = assertEx(possiblyFalsy, () => 'Value must be truthy')
|
|
16
|
+
*
|
|
17
|
+
* // Using with type narrowing
|
|
18
|
+
* const config: Config | null = loadConfig()
|
|
19
|
+
* const safeConfig = assertEx(config, () => 'Config failed to load')
|
|
20
|
+
* // safeConfig is now type Config (not Config | null)
|
|
21
|
+
* ```
|
|
11
22
|
*/
|
|
12
|
-
|
|
13
23
|
function assertEx<T>(expr: T | null | undefined, messageFunc?: AssertExMessageFunc<T>): T
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Asserts that an expression is truthy and returns the value.
|
|
27
|
+
* Throws a custom error if the expression is falsy.
|
|
28
|
+
*
|
|
29
|
+
* @template T - The type of value to check
|
|
30
|
+
* @template R - The type of error to throw
|
|
31
|
+
* @param expr - Expression to be evaluated for truthiness
|
|
32
|
+
* @param errorFunc - Function that returns a custom error instance if expression is falsy
|
|
33
|
+
* @throws Custom error returned by errorFunc
|
|
34
|
+
* @returns The value of the expression (guaranteed to be truthy)
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* // Using with a custom error
|
|
38
|
+
* const user = assertEx(getUser(), () => new UserNotFoundError('User not found'))
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
14
41
|
function assertEx<T, R extends Error>(expr: T | null | undefined, errorFunc?: AssertExErrorFunc<T, R>): T
|
|
42
|
+
|
|
15
43
|
/**
|
|
16
|
-
*
|
|
44
|
+
* Asserts that an expression is truthy and returns the value.
|
|
45
|
+
* Throws an error if the expression is falsy.
|
|
46
|
+
*
|
|
47
|
+
* @deprecated Use overload with message function instead - passing a message will soon be required
|
|
48
|
+
* @template T - The type of value to check
|
|
49
|
+
* @param expr - Expression to be evaluated for truthiness
|
|
50
|
+
* @throws Error with a generic message
|
|
51
|
+
* @returns The value of the expression (guaranteed to be truthy)
|
|
17
52
|
*/
|
|
18
53
|
function assertEx<T>(expr: T | null | undefined): T
|
|
54
|
+
|
|
19
55
|
/**
|
|
20
|
-
*
|
|
56
|
+
* Asserts that an expression is truthy and returns the value.
|
|
57
|
+
* Throws an error with the provided message if the expression is falsy.
|
|
58
|
+
*
|
|
59
|
+
* @deprecated Replace string with () => string for consistency
|
|
60
|
+
* @template T - The type of value to check
|
|
61
|
+
* @param expr - Expression to be evaluated for truthiness
|
|
62
|
+
* @param message - Error message if expression is falsy
|
|
63
|
+
* @throws Error with the provided message
|
|
64
|
+
* @returns The value of the expression (guaranteed to be truthy)
|
|
21
65
|
*/
|
|
22
66
|
function assertEx<T>(expr: T | null | undefined, message?: string): T
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Implementation of assertEx that handles all overloads.
|
|
70
|
+
*/
|
|
23
71
|
function assertEx<T, R extends Error, P extends string | AssertExMessageFunc<T> | AssertExErrorFunc<T, R>>(
|
|
24
72
|
expr: T | null | undefined,
|
|
25
73
|
messageOrFunc?: P,
|
package/src/types.ts
CHANGED
|
@@ -1,2 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
* A function that takes a possibly null or undefined value and returns an error message string.
|
|
4
|
+
* Used in assertion functions to provide custom error messages.
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
* @template T - The type of value being asserted
|
|
8
|
+
* @param value - The value being asserted (may be null or undefined)
|
|
9
|
+
* @returns A string message to be used in the error thrown by the assertion
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const messageFunc: AssertExMessageFunc<User> = (user) =>
|
|
13
|
+
* `User ${user ? user.id : 'unknown'} is not valid or accessible`
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
1
16
|
export type AssertExMessageFunc<T> = (value?: T | null) => string
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A function that takes a possibly null or undefined value and returns a custom error instance.
|
|
20
|
+
* Used in assertion functions to provide specific error types with custom properties.
|
|
21
|
+
*
|
|
22
|
+
* @internal
|
|
23
|
+
* @template T - The type of value being asserted
|
|
24
|
+
* @template R - The specific error type to be returned, must extend Error
|
|
25
|
+
* @param value - The value being asserted (may be null or undefined)
|
|
26
|
+
* @returns An instance of the custom error type
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const errorFunc: AssertExErrorFunc<User, UserNotFoundError> = (user) =>
|
|
30
|
+
* new UserNotFoundError(`User ${user?.id || 'unknown'} not found`, {
|
|
31
|
+
* userId: user?.id,
|
|
32
|
+
* timestamp: new Date()
|
|
33
|
+
* })
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
2
36
|
export type AssertExErrorFunc<T, R extends Error> = (value?: T | null) => R
|