@rickosborne/guard 2024.12.29 → 2024.12.30
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 +16 -274
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
**@rickosborne/guard v2024.12.
|
|
1
|
+
**@rickosborne/guard v2024.12.30**
|
|
2
2
|
|
|
3
3
|
***
|
|
4
4
|
|
|
@@ -10,106 +10,28 @@ Basic type guards building on [@rickosborne/typical](https://www.npmjs.com/packa
|
|
|
10
10
|
|
|
11
11
|
### MessageOrError
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
type MessageOrError = string | Error | () => string | Error;
|
|
15
|
-
```
|
|
13
|
+
> **MessageOrError**: string \| Error \| () => string \| Error
|
|
16
14
|
|
|
17
15
|
A message, an error, or something which can generate one of those.
|
|
18
16
|
|
|
19
17
|
## Functions
|
|
20
18
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
```ts
|
|
24
|
-
function assertDefined<T>(value, messageOrError): asserts value is NonNullable<T>
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
#### Type Parameters
|
|
28
|
-
|
|
29
|
-
• **T**
|
|
30
|
-
|
|
31
|
-
#### Parameters
|
|
32
|
-
|
|
33
|
-
##### value
|
|
34
|
-
|
|
35
|
-
`T`
|
|
36
|
-
|
|
37
|
-
##### messageOrError
|
|
38
|
-
|
|
39
|
-
[`MessageOrError`](README.md#messageorerror)
|
|
40
|
-
|
|
41
|
-
#### Returns
|
|
42
|
-
|
|
43
|
-
`asserts value is NonNullable<T>`
|
|
19
|
+
> **assertDefined**\<T\>(value, messageOrError): asserts value is NonNullable<T>
|
|
44
20
|
|
|
45
21
|
***
|
|
46
22
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
```ts
|
|
50
|
-
function assertInt(value, messageOrError): asserts value is number
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
#### Parameters
|
|
54
|
-
|
|
55
|
-
##### value
|
|
56
|
-
|
|
57
|
-
`unknown`
|
|
58
|
-
|
|
59
|
-
##### messageOrError
|
|
60
|
-
|
|
61
|
-
[`MessageOrError`](README.md#messageorerror)
|
|
62
|
-
|
|
63
|
-
#### Returns
|
|
64
|
-
|
|
65
|
-
`asserts value is number`
|
|
23
|
+
> **assertInt**(value, messageOrError): asserts value is number
|
|
66
24
|
|
|
67
25
|
***
|
|
68
26
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
```ts
|
|
72
|
-
function errorFromMessageOrError(messageOrError, defaultConstructor): Error
|
|
73
|
-
```
|
|
27
|
+
> **errorFromMessageOrError**(messageOrError, defaultConstructor): Error
|
|
74
28
|
|
|
75
29
|
Helper for guards which expect text or an error, or
|
|
76
30
|
can generate one when needed.
|
|
77
31
|
|
|
78
|
-
#### Parameters
|
|
79
|
-
|
|
80
|
-
##### messageOrError
|
|
81
|
-
|
|
82
|
-
[`MessageOrError`](README.md#messageorerror)
|
|
83
|
-
|
|
84
|
-
##### defaultConstructor
|
|
85
|
-
|
|
86
|
-
(`message`) => `Error`
|
|
87
|
-
|
|
88
|
-
#### Returns
|
|
89
|
-
|
|
90
|
-
`Error`
|
|
91
|
-
|
|
92
32
|
***
|
|
93
33
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
```ts
|
|
97
|
-
function expectInt(obj, messageOrError): number
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
#### Parameters
|
|
101
|
-
|
|
102
|
-
##### obj
|
|
103
|
-
|
|
104
|
-
`unknown`
|
|
105
|
-
|
|
106
|
-
##### messageOrError
|
|
107
|
-
|
|
108
|
-
[`MessageOrError`](README.md#messageorerror)
|
|
109
|
-
|
|
110
|
-
#### Returns
|
|
111
|
-
|
|
112
|
-
`number`
|
|
34
|
+
> **expectInt**(obj, messageOrError): number
|
|
113
35
|
|
|
114
36
|
***
|
|
115
37
|
|
|
@@ -119,12 +41,7 @@ Guard for whether the given value is an object which has a property with its own
|
|
|
119
41
|
|
|
120
42
|
#### Call Signature
|
|
121
43
|
|
|
122
|
-
|
|
123
|
-
function hasArray<Name>(
|
|
124
|
-
obj,
|
|
125
|
-
name,
|
|
126
|
-
predicate?): obj is { [K in string]: unknown[] }
|
|
127
|
-
```
|
|
44
|
+
> **hasArray**\<Name\>(obj, name, predicate?): obj is { [K in string]: unknown[] }
|
|
128
45
|
|
|
129
46
|
Guard for whether the given value is an object which has a property with its own array value.
|
|
130
47
|
This variant does not exhaustively check the items of the array.
|
|
@@ -133,32 +50,9 @@ This variant does not exhaustively check the items of the array.
|
|
|
133
50
|
|
|
134
51
|
• **Name** *extends* `string`
|
|
135
52
|
|
|
136
|
-
##### Parameters
|
|
137
|
-
|
|
138
|
-
###### obj
|
|
139
|
-
|
|
140
|
-
`unknown`
|
|
141
|
-
|
|
142
|
-
###### name
|
|
143
|
-
|
|
144
|
-
`Name`
|
|
145
|
-
|
|
146
|
-
###### predicate?
|
|
147
|
-
|
|
148
|
-
`undefined`
|
|
149
|
-
|
|
150
|
-
##### Returns
|
|
151
|
-
|
|
152
|
-
`obj is { [K in string]: unknown[] }`
|
|
153
|
-
|
|
154
53
|
#### Call Signature
|
|
155
54
|
|
|
156
|
-
|
|
157
|
-
function hasArray<Name, Item>(
|
|
158
|
-
obj,
|
|
159
|
-
name,
|
|
160
|
-
predicate?): obj is { [K in string]: Item[] }
|
|
161
|
-
```
|
|
55
|
+
> **hasArray**\<Name, Item\>(obj, name, predicate?): obj is { [K in string]: Item[] }
|
|
162
56
|
|
|
163
57
|
Guard for whether the given value is an object which has a property with its own array value.
|
|
164
58
|
This variant exhaustively checks the items of the array against the given predicate.
|
|
@@ -169,86 +63,29 @@ This variant exhaustively checks the items of the array against the given predic
|
|
|
169
63
|
|
|
170
64
|
• **Item**
|
|
171
65
|
|
|
172
|
-
##### Parameters
|
|
173
|
-
|
|
174
|
-
###### obj
|
|
175
|
-
|
|
176
|
-
`unknown`
|
|
177
|
-
|
|
178
|
-
###### name
|
|
179
|
-
|
|
180
|
-
`Name`
|
|
181
|
-
|
|
182
|
-
###### predicate?
|
|
183
|
-
|
|
184
|
-
(`item`, `index`, `items`) => `item is Item`
|
|
185
|
-
|
|
186
|
-
##### Returns
|
|
187
|
-
|
|
188
|
-
`obj is { [K in string]: Item[] }`
|
|
189
|
-
|
|
190
66
|
***
|
|
191
67
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
```ts
|
|
195
|
-
function hasNumber<Name>(obj, name): obj is { [k in string]: string }
|
|
196
|
-
```
|
|
68
|
+
> **hasNumber**\<Name\>(obj, name): obj is { [k in string]: string }
|
|
197
69
|
|
|
198
70
|
#### Type Parameters
|
|
199
71
|
|
|
200
72
|
• **Name** *extends* `string`
|
|
201
73
|
|
|
202
|
-
#### Parameters
|
|
203
|
-
|
|
204
|
-
##### obj
|
|
205
|
-
|
|
206
|
-
`unknown`
|
|
207
|
-
|
|
208
|
-
##### name
|
|
209
|
-
|
|
210
|
-
`Name`
|
|
211
|
-
|
|
212
|
-
#### Returns
|
|
213
|
-
|
|
214
|
-
`obj is { [k in string]: string }`
|
|
215
|
-
|
|
216
74
|
***
|
|
217
75
|
|
|
218
76
|
### hasOwn()
|
|
219
77
|
|
|
220
78
|
#### Call Signature
|
|
221
79
|
|
|
222
|
-
|
|
223
|
-
function hasOwn<Name>(obj, name): obj is { [K in string]: unknown }
|
|
224
|
-
```
|
|
80
|
+
> **hasOwn**\<Name\>(obj, name): obj is { [K in string]: unknown }
|
|
225
81
|
|
|
226
82
|
##### Type Parameters
|
|
227
83
|
|
|
228
84
|
• **Name** *extends* `string`
|
|
229
85
|
|
|
230
|
-
##### Parameters
|
|
231
|
-
|
|
232
|
-
###### obj
|
|
233
|
-
|
|
234
|
-
`unknown`
|
|
235
|
-
|
|
236
|
-
###### name
|
|
237
|
-
|
|
238
|
-
`Name`
|
|
239
|
-
|
|
240
|
-
##### Returns
|
|
241
|
-
|
|
242
|
-
`obj is { [K in string]: unknown }`
|
|
243
|
-
|
|
244
86
|
#### Call Signature
|
|
245
87
|
|
|
246
|
-
|
|
247
|
-
function hasOwn<Name, T>(
|
|
248
|
-
obj,
|
|
249
|
-
name,
|
|
250
|
-
predicate): obj is { [K in string]: T }
|
|
251
|
-
```
|
|
88
|
+
> **hasOwn**\<Name, T\>(obj, name, predicate): obj is { [K in string]: T }
|
|
252
89
|
|
|
253
90
|
##### Type Parameters
|
|
254
91
|
|
|
@@ -256,123 +93,28 @@ function hasOwn<Name, T>(
|
|
|
256
93
|
|
|
257
94
|
• **T**
|
|
258
95
|
|
|
259
|
-
##### Parameters
|
|
260
|
-
|
|
261
|
-
###### obj
|
|
262
|
-
|
|
263
|
-
`unknown`
|
|
264
|
-
|
|
265
|
-
###### name
|
|
266
|
-
|
|
267
|
-
`Name`
|
|
268
|
-
|
|
269
|
-
###### predicate
|
|
270
|
-
|
|
271
|
-
(`value`) => `value is T`
|
|
272
|
-
|
|
273
|
-
##### Returns
|
|
274
|
-
|
|
275
|
-
`obj is { [K in string]: T }`
|
|
276
|
-
|
|
277
96
|
***
|
|
278
97
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
```ts
|
|
282
|
-
function isInt(obj): obj is number
|
|
283
|
-
```
|
|
284
|
-
|
|
285
|
-
#### Parameters
|
|
286
|
-
|
|
287
|
-
##### obj
|
|
288
|
-
|
|
289
|
-
`unknown`
|
|
290
|
-
|
|
291
|
-
#### Returns
|
|
292
|
-
|
|
293
|
-
`obj is number`
|
|
98
|
+
> **isInt**(obj): obj is number
|
|
294
99
|
|
|
295
100
|
***
|
|
296
101
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
```ts
|
|
300
|
-
function isListOf<T>(list, predicate): list is T[]
|
|
301
|
-
```
|
|
302
|
-
|
|
303
|
-
#### Type Parameters
|
|
304
|
-
|
|
305
|
-
• **T**
|
|
306
|
-
|
|
307
|
-
#### Parameters
|
|
308
|
-
|
|
309
|
-
##### list
|
|
310
|
-
|
|
311
|
-
`unknown`
|
|
312
|
-
|
|
313
|
-
##### predicate
|
|
314
|
-
|
|
315
|
-
(`item`, `index`, `items`) => `item is T`
|
|
316
|
-
|
|
317
|
-
#### Returns
|
|
318
|
-
|
|
319
|
-
`list is T[]`
|
|
102
|
+
> **isListOf**\<T\>(list, predicate): list is T[]
|
|
320
103
|
|
|
321
104
|
***
|
|
322
105
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
```ts
|
|
326
|
-
function isObject(obj): obj is object
|
|
327
|
-
```
|
|
328
|
-
|
|
329
|
-
#### Parameters
|
|
330
|
-
|
|
331
|
-
##### obj
|
|
332
|
-
|
|
333
|
-
`unknown`
|
|
334
|
-
|
|
335
|
-
#### Returns
|
|
336
|
-
|
|
337
|
-
`obj is object`
|
|
106
|
+
> **isObject**(obj): obj is object
|
|
338
107
|
|
|
339
108
|
***
|
|
340
109
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
```ts
|
|
344
|
-
function isPlainObject(obj): obj is Record<never, never>
|
|
345
|
-
```
|
|
346
|
-
|
|
347
|
-
#### Parameters
|
|
348
|
-
|
|
349
|
-
##### obj
|
|
350
|
-
|
|
351
|
-
`unknown`
|
|
352
|
-
|
|
353
|
-
#### Returns
|
|
354
|
-
|
|
355
|
-
`obj is Record<never, never>`
|
|
110
|
+
> **isPlainObject**(obj): obj is Record<never, never>
|
|
356
111
|
|
|
357
112
|
***
|
|
358
113
|
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
```ts
|
|
362
|
-
function isUnaryPredicate(obj): obj is UnaryPredicate<unknown>
|
|
363
|
-
```
|
|
114
|
+
> **isUnaryPredicate**(obj): obj is UnaryPredicate<unknown>
|
|
364
115
|
|
|
365
116
|
Tests whether the given object is a function and takes at
|
|
366
117
|
least one parameter, and could maybe act as a unary predicate.
|
|
367
118
|
Warning! Since no type information is available at runtime,
|
|
368
119
|
it may not actually act as a predicate!
|
|
369
120
|
|
|
370
|
-
#### Parameters
|
|
371
|
-
|
|
372
|
-
##### obj
|
|
373
|
-
|
|
374
|
-
`unknown`
|
|
375
|
-
|
|
376
|
-
#### Returns
|
|
377
|
-
|
|
378
|
-
`obj is UnaryPredicate<unknown>`
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"url": "https://rickosborne.org"
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@rickosborne/typical": "2024.12.
|
|
7
|
+
"@rickosborne/typical": "2024.12.30"
|
|
8
8
|
},
|
|
9
9
|
"description": "Rick Osborne's collection of type guards.",
|
|
10
10
|
"engines": {
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"type": "git",
|
|
32
32
|
"url": "git+https://github.com/rickosborne/es-js-ts.git"
|
|
33
33
|
},
|
|
34
|
-
"version": "2024.12.
|
|
34
|
+
"version": "2024.12.30"
|
|
35
35
|
}
|