@xylabs/assert 4.13.20 → 4.13.22

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 CHANGED
@@ -12,13 +12,420 @@
12
12
  [![snyk-badge][]][snyk-link]
13
13
  [![socket-badge][]][socket-link]
14
14
 
15
- Version: 4.13.19
16
15
 
17
16
  Base functionality used throughout XY Labs TypeScript/JavaScript libraries
18
17
 
19
- ## Documentation
18
+ ## Reference
19
+
20
+ **@xylabs/assert**
21
+
22
+ ***
23
+
24
+ ## Functions
25
+
26
+ - [assertDefinedEx](#functions/assertDefinedEx)
27
+ - [assertEx](#functions/assertEx)
28
+
29
+ ### functions
30
+
31
+ ### <a id="assertDefinedEx"></a>assertDefinedEx
32
+
33
+ [**@xylabs/assert**](#../README)
34
+
35
+ ***
36
+
37
+ Implementation of assertDefinedEx that handles all overloads.
38
+
39
+ ## Call Signature
40
+
41
+ ```ts
42
+ function assertDefinedEx<T>(expr, messageFunc?): T;
43
+ ```
44
+
45
+ Asserts that a value is defined (not undefined) and returns the value.
46
+ Throws an error if the value is undefined.
47
+
48
+ ### Type Parameters
49
+
50
+ ### T
51
+
52
+ `T`
53
+
54
+ The type of value to check
55
+
56
+ ### Parameters
57
+
58
+ ### expr
59
+
60
+ Expression to be evaluated for being defined
61
+
62
+ `undefined` | `T`
63
+
64
+ ### messageFunc?
65
+
66
+ `AssertExMessageFunc`\<`T`\>
67
+
68
+ Function that returns a message for the error if expression is undefined
69
+
70
+ ### Returns
71
+
72
+ `T`
73
+
74
+ The value of the expression (guaranteed to be defined)
75
+
76
+ ### Throws
77
+
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
+ ```
91
+
92
+ ## Call Signature
93
+
94
+ ```ts
95
+ function assertDefinedEx<T, R>(expr, errorFunc?): T;
96
+ ```
97
+
98
+ Asserts that a value is defined (not undefined) and returns the value.
99
+ Throws a custom error if the value is undefined.
100
+
101
+ ### Type Parameters
102
+
103
+ ### T
104
+
105
+ `T`
106
+
107
+ The type of value to check
108
+
109
+ ### R
110
+
111
+ `R` *extends* `Error`
112
+
113
+ The type of error to throw
114
+
115
+ ### Parameters
116
+
117
+ ### expr
118
+
119
+ Expression to be evaluated for being defined
120
+
121
+ `undefined` | `T`
122
+
123
+ ### errorFunc?
124
+
125
+ `AssertExErrorFunc`\<`T`, `R`\>
126
+
127
+ Function that returns a custom error instance if expression is undefined
128
+
129
+ ### Returns
130
+
131
+ `T`
132
+
133
+ The value of the expression (guaranteed to be defined)
134
+
135
+ ### Throws
136
+
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
+ ```
145
+
146
+ ## Call Signature
147
+
148
+ ```ts
149
+ function assertDefinedEx<T>(expr): T;
150
+ ```
151
+
152
+ Asserts that a value is defined (not undefined) and returns the value.
153
+ Throws an error if the value is undefined.
154
+
155
+ ### Type Parameters
156
+
157
+ ### T
158
+
159
+ `T`
160
+
161
+ The type of value to check
162
+
163
+ ### Parameters
164
+
165
+ ### expr
166
+
167
+ Expression to be evaluated for being defined
168
+
169
+ `undefined` | `T`
170
+
171
+ ### Returns
172
+
173
+ `T`
174
+
175
+ The value of the expression (guaranteed to be defined)
176
+
177
+ ### Deprecated
178
+
179
+ Use overload with message function instead - passing a message will soon be required
180
+
181
+ ### Throws
182
+
183
+ Error with a generic message
184
+
185
+ ## Call Signature
186
+
187
+ ```ts
188
+ function assertDefinedEx<T>(expr, message?): T;
189
+ ```
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
+
194
+ ### Type Parameters
195
+
196
+ ### T
197
+
198
+ `T`
199
+
200
+ The type of value to check
201
+
202
+ ### Parameters
203
+
204
+ ### expr
205
+
206
+ Expression to be evaluated for being defined
207
+
208
+ `undefined` | `T`
209
+
210
+ ### message?
211
+
212
+ `string`
213
+
214
+ Error message if expression is undefined
215
+
216
+ ### Returns
217
+
218
+ `T`
219
+
220
+ The value of the expression (guaranteed to be defined)
221
+
222
+ ### Deprecated
223
+
224
+ Replace string with () => string for consistency
225
+
226
+ ### Throws
227
+
228
+ Error with the provided message
229
+
230
+ ### <a id="assertEx"></a>assertEx
231
+
232
+ [**@xylabs/assert**](#../README)
233
+
234
+ ***
235
+
236
+ Implementation of assertEx that handles all overloads.
237
+
238
+ ## Call Signature
239
+
240
+ ```ts
241
+ function assertEx<T>(expr, messageFunc?): T;
242
+ ```
243
+
244
+ Asserts that an expression is truthy and returns the value.
245
+ Throws an error if the expression is falsy.
246
+
247
+ ### Type Parameters
248
+
249
+ ### T
250
+
251
+ `T`
252
+
253
+ The type of value to check
254
+
255
+ ### Parameters
256
+
257
+ ### expr
258
+
259
+ Expression to be evaluated for truthiness
260
+
261
+ `undefined` | `null` | `T`
262
+
263
+ ### messageFunc?
264
+
265
+ `AssertExMessageFunc`\<`T`\>
266
+
267
+ Function that returns a message for the error if expression is falsy
268
+
269
+ ### Returns
270
+
271
+ `T`
272
+
273
+ The value of the expression (guaranteed to be truthy)
274
+
275
+ ### Throws
276
+
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
+ ```
290
+
291
+ ## Call Signature
292
+
293
+ ```ts
294
+ function assertEx<T, R>(expr, errorFunc?): T;
295
+ ```
296
+
297
+ Asserts that an expression is truthy and returns the value.
298
+ Throws a custom error if the expression is falsy.
299
+
300
+ ### Type Parameters
301
+
302
+ ### T
303
+
304
+ `T`
305
+
306
+ The type of value to check
307
+
308
+ ### R
309
+
310
+ `R` *extends* `Error`
311
+
312
+ The type of error to throw
313
+
314
+ ### Parameters
315
+
316
+ ### expr
317
+
318
+ Expression to be evaluated for truthiness
319
+
320
+ `undefined` | `null` | `T`
321
+
322
+ ### errorFunc?
323
+
324
+ `AssertExErrorFunc`\<`T`, `R`\>
325
+
326
+ Function that returns a custom error instance if expression is falsy
327
+
328
+ ### Returns
329
+
330
+ `T`
331
+
332
+ The value of the expression (guaranteed to be truthy)
333
+
334
+ ### Throws
335
+
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
+ ```
344
+
345
+ ## Call Signature
346
+
347
+ ```ts
348
+ function assertEx<T>(expr): T;
349
+ ```
350
+
351
+ Asserts that an expression is truthy and returns the value.
352
+ Throws an error if the expression is falsy.
353
+
354
+ ### Type Parameters
355
+
356
+ ### T
357
+
358
+ `T`
359
+
360
+ The type of value to check
361
+
362
+ ### Parameters
363
+
364
+ ### expr
365
+
366
+ Expression to be evaluated for truthiness
367
+
368
+ `undefined` | `null` | `T`
369
+
370
+ ### Returns
371
+
372
+ `T`
373
+
374
+ The value of the expression (guaranteed to be truthy)
375
+
376
+ ### Deprecated
377
+
378
+ Use overload with message function instead - passing a message will soon be required
379
+
380
+ ### Throws
381
+
382
+ Error with a generic message
383
+
384
+ ## Call Signature
385
+
386
+ ```ts
387
+ function assertEx<T>(expr, message?): T;
388
+ ```
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
+
393
+ ### Type Parameters
394
+
395
+ ### T
396
+
397
+ `T`
398
+
399
+ The type of value to check
400
+
401
+ ### Parameters
402
+
403
+ ### expr
404
+
405
+ Expression to be evaluated for truthiness
406
+
407
+ `undefined` | `null` | `T`
408
+
409
+ ### message?
410
+
411
+ `string`
412
+
413
+ Error message if expression is falsy
414
+
415
+ ### Returns
416
+
417
+ `T`
418
+
419
+ The value of the expression (guaranteed to be truthy)
420
+
421
+ ### Deprecated
422
+
423
+ Replace string with () => string for consistency
424
+
425
+ ### Throws
426
+
427
+ Error with the provided message
20
428
 
21
- Coming Soon!
22
429
 
23
430
  Part of [sdk-js](https://www.npmjs.com/package/@xyo-network/sdk-js)
24
431
 
@@ -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
- * Intended for defined checks for variables
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
- * @param expr - Expression to be evaluated for truthiness
7
- * @param message - Message in Error if expression is false, may be a function that returns a string
8
- * @throws AssertExError
9
- * @returns Value of expression
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
- * @deprecated - passing a message will soon be required
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
- * @deprecated - replace string with () => string
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;;;;;;;;GAQG;AAEH,iBAAS,eAAe,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,SAAS,EAAE,WAAW,CAAC,EAAE,mBAAmB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;AACzF,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;AACzG;;GAEG;AACH,iBAAS,eAAe,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,SAAS,GAAG,CAAC,CAAA;AACnD;;GAEG;AACH,iBAAS,eAAe,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,SAAS,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,CAAC,CAAA;AAcrE,OAAO,EAAE,eAAe,EAAE,CAAA"}
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
- * Intended for simple truthiness checks for variables
5
- *
6
+ * @template T - The type of value to check
6
7
  * @param expr - Expression to be evaluated for truthiness
7
- * @param message - Message in Error if expression is false, may be a function that returns a string
8
- * @throws AssertExError
9
- * @returns Value of expression
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
- * @deprecated - passing a message will soon be required
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
- * @deprecated - replace string with () => string
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;;;;;;;;GAQG;AAEH,iBAAS,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,EAAE,WAAW,CAAC,EAAE,mBAAmB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;AACzF,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;AACzG;;GAEG;AACH,iBAAS,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,GAAG,CAAC,CAAA;AACnD;;GAEG;AACH,iBAAS,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,CAAC,CAAA;AAerE,OAAO,EAAE,QAAQ,EAAE,CAAA"}
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 *\n * Intended for defined checks for variables\n *\n * @param expr - Expression to be evaluated for truthiness\n * @param message - Message in Error if expression is false, may be a function that returns a string\n * @throws AssertExError\n * @returns Value of expression\n */\n\nfunction assertDefinedEx<T>(expr: T | undefined, messageFunc?: AssertExMessageFunc<T>): T\nfunction assertDefinedEx<T, R extends Error>(expr: T | undefined, errorFunc?: AssertExErrorFunc<T, R>): T\n/**\n * @deprecated - passing a message will soon be required\n */\nfunction assertDefinedEx<T>(expr: T | undefined): T\n/**\n * @deprecated - replace string with () => string\n */\nfunction assertDefinedEx<T>(expr: T | undefined, message?: string): T\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 *\n * Intended for simple truthiness checks for variables\n *\n * @param expr - Expression to be evaluated for truthiness\n * @param message - Message in Error if expression is false, may be a function that returns a string\n * @throws AssertExError\n * @returns Value of expression\n */\n\nfunction assertEx<T>(expr: T | null | undefined, messageFunc?: AssertExMessageFunc<T>): T\nfunction assertEx<T, R extends Error>(expr: T | null | undefined, errorFunc?: AssertExErrorFunc<T, R>): T\n/**\n * @deprecated - passing a message will soon be required\n */\nfunction assertEx<T>(expr: T | null | undefined): T\n/**\n * @deprecated - replace string with () => string\n */\nfunction assertEx<T>(expr: T | null | undefined, message?: string): T\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":";AAsBA,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;;;ACXA,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":[]}
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":[]}
@@ -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;AACjE,MAAM,MAAM,iBAAiB,CAAC,CAAC,EAAE,CAAC,SAAS,KAAK,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,GAAG,IAAI,KAAK,CAAC,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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xylabs/assert",
3
- "version": "4.13.20",
3
+ "version": "4.13.22",
4
4
  "description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
5
5
  "keywords": [
6
6
  "xylabs",
@@ -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
- * Intended for defined checks for variables
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
- * @param expr - Expression to be evaluated for truthiness
8
- * @param message - Message in Error if expression is false, may be a function that returns a string
9
- * @throws AssertExError
10
- * @returns Value of expression
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
- * @deprecated - passing a message will soon be required
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
- * @deprecated - replace string with () => string
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
- * Intended for simple truthiness checks for variables
6
- *
7
+ * @template T - The type of value to check
7
8
  * @param expr - Expression to be evaluated for truthiness
8
- * @param message - Message in Error if expression is false, may be a function that returns a string
9
- * @throws AssertExError
10
- * @returns Value of expression
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
- * @deprecated - passing a message will soon be required
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
- * @deprecated - replace string with () => string
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