@xylabs/assert 5.0.80 → 5.0.82
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 +4 -172
- package/package.json +5 -7
- package/src/assertDefinedEx.ts +0 -62
- package/src/assertEx.ts +0 -62
- package/src/index.ts +0 -3
- package/src/types.ts +0 -36
package/README.md
CHANGED
|
@@ -59,7 +59,7 @@ The type of value to check
|
|
|
59
59
|
|
|
60
60
|
Expression to be evaluated for being defined
|
|
61
61
|
|
|
62
|
-
`
|
|
62
|
+
`T` | `undefined`
|
|
63
63
|
|
|
64
64
|
### messageFunc?
|
|
65
65
|
|
|
@@ -118,7 +118,7 @@ The type of error to throw
|
|
|
118
118
|
|
|
119
119
|
Expression to be evaluated for being defined
|
|
120
120
|
|
|
121
|
-
`
|
|
121
|
+
`T` | `undefined`
|
|
122
122
|
|
|
123
123
|
### errorFunc?
|
|
124
124
|
|
|
@@ -143,90 +143,6 @@ Custom error returned by errorFunc
|
|
|
143
143
|
const user = assertDefinedEx(getUser(), () => new UserNotFoundError('User not found'))
|
|
144
144
|
```
|
|
145
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
146
|
### <a id="assertEx"></a>assertEx
|
|
231
147
|
|
|
232
148
|
[**@xylabs/assert**](#../README)
|
|
@@ -258,7 +174,7 @@ The type of value to check
|
|
|
258
174
|
|
|
259
175
|
Expression to be evaluated for truthiness
|
|
260
176
|
|
|
261
|
-
`
|
|
177
|
+
`T` | `null` | `undefined`
|
|
262
178
|
|
|
263
179
|
### messageFunc?
|
|
264
180
|
|
|
@@ -317,7 +233,7 @@ The type of error to throw
|
|
|
317
233
|
|
|
318
234
|
Expression to be evaluated for truthiness
|
|
319
235
|
|
|
320
|
-
`
|
|
236
|
+
`T` | `null` | `undefined`
|
|
321
237
|
|
|
322
238
|
### errorFunc?
|
|
323
239
|
|
|
@@ -342,90 +258,6 @@ Custom error returned by errorFunc
|
|
|
342
258
|
const user = assertEx(getUser(), () => new UserNotFoundError('User not found'))
|
|
343
259
|
```
|
|
344
260
|
|
|
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
|
|
428
|
-
|
|
429
261
|
|
|
430
262
|
Part of [sdk-js](https://www.npmjs.com/package/@xyo-network/sdk-js)
|
|
431
263
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xylabs/assert",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.82",
|
|
4
4
|
"description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"xylabs",
|
|
@@ -28,25 +28,23 @@
|
|
|
28
28
|
"exports": {
|
|
29
29
|
".": {
|
|
30
30
|
"types": "./dist/neutral/index.d.ts",
|
|
31
|
-
"source": "./src/index.ts",
|
|
32
31
|
"default": "./dist/neutral/index.mjs"
|
|
33
32
|
},
|
|
34
33
|
"./package.json": "./package.json"
|
|
35
34
|
},
|
|
36
35
|
"module": "./dist/neutral/index.mjs",
|
|
37
|
-
"source": "./src/index.ts",
|
|
38
36
|
"types": "./dist/neutral/index.d.ts",
|
|
39
37
|
"files": [
|
|
40
38
|
"dist",
|
|
41
|
-
"src",
|
|
42
39
|
"!**/*.bench.*",
|
|
43
40
|
"!**/*.spec.*",
|
|
44
41
|
"!**/*.test.*"
|
|
45
42
|
],
|
|
46
43
|
"devDependencies": {
|
|
47
|
-
"@xylabs/ts-scripts-yarn3": "~7.
|
|
48
|
-
"@xylabs/tsconfig": "~7.
|
|
49
|
-
"typescript": "~5.9.3"
|
|
44
|
+
"@xylabs/ts-scripts-yarn3": "~7.4.11",
|
|
45
|
+
"@xylabs/tsconfig": "~7.4.11",
|
|
46
|
+
"typescript": "~5.9.3",
|
|
47
|
+
"vitest": "^4.0.18"
|
|
50
48
|
},
|
|
51
49
|
"engines": {
|
|
52
50
|
"node": ">=18"
|
package/src/assertDefinedEx.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import type { AssertExErrorFunc, AssertExMessageFunc } from './types.ts'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Asserts that a value is defined (not undefined) and returns the value.
|
|
5
|
-
* Throws an error if the value is undefined.
|
|
6
|
-
*
|
|
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')
|
|
16
|
-
*
|
|
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
|
-
* ```
|
|
22
|
-
*/
|
|
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
|
-
*/
|
|
41
|
-
function assertDefinedEx<T, R extends Error>(expr: T | undefined, errorFunc?: AssertExErrorFunc<T, R>): T
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Implementation of assertDefinedEx that handles all overloads.
|
|
45
|
-
*
|
|
46
|
-
*/
|
|
47
|
-
function assertDefinedEx<T, R extends Error, P extends AssertExMessageFunc<T> | AssertExErrorFunc<T, R>>(
|
|
48
|
-
expr: T | undefined,
|
|
49
|
-
func?: P,
|
|
50
|
-
): T {
|
|
51
|
-
if (expr !== undefined) return expr
|
|
52
|
-
if (typeof func === 'function') {
|
|
53
|
-
const errorOrMessage = func(expr)
|
|
54
|
-
throw typeof errorOrMessage === 'string' ? new Error(errorOrMessage) : errorOrMessage
|
|
55
|
-
}
|
|
56
|
-
if (func !== undefined) {
|
|
57
|
-
throw new Error('Invalid assertEx usage: second argument must be a function or undefined')
|
|
58
|
-
}
|
|
59
|
-
throw new Error('Assertion failed: value is undefined')
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export { assertDefinedEx }
|
package/src/assertEx.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import type { AssertExErrorFunc, AssertExMessageFunc } from './types.ts'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Asserts that an expression is truthy and returns the value.
|
|
5
|
-
* Throws an error if the expression is falsy.
|
|
6
|
-
*
|
|
7
|
-
* @template T - The type of value to check
|
|
8
|
-
* @param expr - Expression to be evaluated for truthiness
|
|
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
|
-
* ```
|
|
22
|
-
*/
|
|
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
|
-
*/
|
|
41
|
-
function assertEx<T, R extends Error>(expr: T | null | undefined, errorFunc?: AssertExErrorFunc<T, R>): T
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Implementation of assertEx that handles all overloads.
|
|
45
|
-
*/
|
|
46
|
-
function assertEx<T, R extends Error, P extends AssertExMessageFunc<T> | AssertExErrorFunc<T, R>>(
|
|
47
|
-
expr: T | null | undefined,
|
|
48
|
-
func?: P,
|
|
49
|
-
): T {
|
|
50
|
-
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
51
|
-
if (expr) return expr
|
|
52
|
-
if (typeof func === 'function') {
|
|
53
|
-
const errorOrMessage = func(expr)
|
|
54
|
-
throw typeof errorOrMessage === 'string' ? new Error(errorOrMessage) : errorOrMessage
|
|
55
|
-
}
|
|
56
|
-
if (func !== undefined) {
|
|
57
|
-
throw new Error('Invalid assertEx usage: second argument must be a function or undefined')
|
|
58
|
-
}
|
|
59
|
-
throw new Error('Assertion failed')
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export { assertEx }
|
package/src/index.ts
DELETED
package/src/types.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
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
|
-
*/
|
|
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
|
-
*/
|
|
36
|
-
export type AssertExErrorFunc<T, R extends Error> = (value?: T | null) => R
|