@rickosborne/guard 2024.12.29 → 2024.12.32

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
@@ -1,378 +1,154 @@
1
- **@rickosborne/guard v2024.12.29**
2
-
3
- ***
4
-
5
1
  # @rickosborne/guard
6
2
 
7
3
  Basic type guards building on [@rickosborne/typical](https://www.npmjs.com/package/@rickosborne/typical).
4
+ ***
8
5
 
9
- ## Type Aliases
10
-
11
- ### MessageOrError
12
-
13
- ```ts
14
- type MessageOrError = string | Error | () => string | Error;
15
- ```
6
+ ## API
16
7
 
17
- A message, an error, or something which can generate one of those.
8
+ ### Functions
18
9
 
19
- ## Functions
10
+ #### assertDefined
20
11
 
21
- ### assertDefined()
12
+ <a id="api-assertdefined"></a>
22
13
 
23
- ```ts
24
- function assertDefined<T>(value, messageOrError): asserts value is NonNullable<T>
14
+ ```typescript
15
+ declare function assertDefined<T>(value: T, messageOrError: MessageOrError): asserts value is NonNullable<T>;
25
16
  ```
26
17
 
27
- #### Type Parameters
18
+ #### assertInt
28
19
 
29
- **T**
20
+ <a id="api-assertint"></a>
30
21
 
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>`
44
-
45
- ***
46
-
47
- ### assertInt()
48
-
49
- ```ts
50
- function assertInt(value, messageOrError): asserts value is number
22
+ ```typescript
23
+ declare function assertInt(value: unknown, messageOrError: MessageOrError): asserts value is number;
51
24
  ```
52
25
 
53
- #### Parameters
54
-
55
- ##### value
56
-
57
- `unknown`
26
+ #### errorFromMessageOrError
58
27
 
59
- ##### messageOrError
28
+ <a id="api-errorfrommessageorerror"></a>
60
29
 
61
- [`MessageOrError`](README.md#messageorerror)
62
-
63
- #### Returns
64
-
65
- `asserts value is number`
66
-
67
- ***
68
-
69
- ### errorFromMessageOrError()
70
-
71
- ```ts
72
- function errorFromMessageOrError(messageOrError, defaultConstructor): Error
30
+ ```typescript
31
+ errorFromMessageOrError: (messageOrError: MessageOrError, defaultConstructor?: (new (message: string) => Error)) => Error
73
32
  ```
74
33
 
75
- Helper for guards which expect text or an error, or
76
- can generate one when needed.
77
-
78
- #### Parameters
79
-
80
- ##### messageOrError
81
-
82
- [`MessageOrError`](README.md#messageorerror)
83
-
84
- ##### defaultConstructor
85
-
86
- (`message`) => `Error`
34
+ Helper for guards which expect text or an error, or can generate one when needed.
87
35
 
88
- #### Returns
89
36
 
90
- `Error`
37
+ #### expectInt
91
38
 
92
- ***
93
-
94
- ### expectInt()
39
+ <a id="api-expectint"></a>
95
40
 
96
- ```ts
97
- function expectInt(obj, messageOrError): number
41
+ ```typescript
42
+ expectInt: (obj: unknown, messageOrError: MessageOrError) => number
98
43
  ```
99
44
 
100
- #### Parameters
101
-
102
- ##### obj
103
-
104
- `unknown`
105
-
106
- ##### messageOrError
107
-
108
- [`MessageOrError`](README.md#messageorerror)
109
-
110
- #### Returns
111
-
112
- `number`
45
+ #### hasArray
113
46
 
114
- ***
115
-
116
- ### hasArray()
117
-
118
- Guard for whether the given value is an object which has a property with its own array value.
119
-
120
- #### Call Signature
47
+ <a id="api-hasarray"></a>
121
48
 
122
- ```ts
123
- function hasArray<Name>(
124
- obj,
125
- name,
126
- predicate?): obj is { [K in string]: unknown[] }
49
+ ```typescript
50
+ declare function hasArray<Name extends string>(obj: unknown, name: Name, predicate?: undefined): obj is {
51
+ [K in Name]: unknown[];
52
+ };
127
53
  ```
128
54
 
129
- Guard for whether the given value is an object which has a property with its own array value.
130
- This variant does not exhaustively check the items of the array.
131
-
132
- ##### Type Parameters
133
-
134
- • **Name** *extends* `string`
135
-
136
- ##### Parameters
137
-
138
- ###### obj
55
+ Guard for whether the given value is an object which has a property with its own array value. This variant does not exhaustively check the items of the array.
139
56
 
140
- `unknown`
141
57
 
142
- ###### name
58
+ #### hasArray
143
59
 
144
- `Name`
60
+ <a id="api-hasarray"></a>
145
61
 
146
- ###### predicate?
147
-
148
- `undefined`
149
-
150
- ##### Returns
151
-
152
- `obj is { [K in string]: unknown[] }`
153
-
154
- #### Call Signature
155
-
156
- ```ts
157
- function hasArray<Name, Item>(
158
- obj,
159
- name,
160
- predicate?): obj is { [K in string]: Item[] }
62
+ ```typescript
63
+ declare function hasArray<Name extends string, Item>(obj: unknown, name: Name, predicate?: (item: unknown, index: number, items: unknown[]) => item is Item): obj is {
64
+ [K in Name]: Item[];
65
+ };
161
66
  ```
162
67
 
163
- Guard for whether the given value is an object which has a property with its own array value.
164
- This variant exhaustively checks the items of the array against the given predicate.
165
-
166
- ##### Type Parameters
167
-
168
- • **Name** *extends* `string`
169
-
170
- • **Item**
68
+ Guard for whether the given value is an object which has a property with its own array value. This variant exhaustively checks the items of the array against the given predicate.
171
69
 
172
- ##### Parameters
173
70
 
174
- ###### obj
71
+ #### hasNumber
175
72
 
176
- `unknown`
73
+ <a id="api-hasnumber"></a>
177
74
 
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
- ***
191
-
192
- ### hasNumber()
193
-
194
- ```ts
195
- function hasNumber<Name>(obj, name): obj is { [k in string]: string }
75
+ ```typescript
76
+ hasNumber: <Name extends string>(obj: unknown, name: Name) => obj is { [k in Name]: string; }
196
77
  ```
197
78
 
198
- #### Type Parameters
199
-
200
- • **Name** *extends* `string`
201
-
202
- #### Parameters
203
-
204
- ##### obj
205
-
206
- `unknown`
207
-
208
- ##### name
209
-
210
- `Name`
79
+ #### hasOwn
211
80
 
212
- #### Returns
81
+ <a id="api-hasown"></a>
213
82
 
214
- `obj is { [k in string]: string }`
215
-
216
- ***
217
-
218
- ### hasOwn()
219
-
220
- #### Call Signature
221
-
222
- ```ts
223
- function hasOwn<Name>(obj, name): obj is { [K in string]: unknown }
83
+ ```typescript
84
+ declare function hasOwn<Name extends string>(obj: unknown, name: Name): obj is {
85
+ [K in Name]: unknown;
86
+ };
224
87
  ```
225
88
 
226
- ##### Type Parameters
227
-
228
- • **Name** *extends* `string`
229
-
230
- ##### Parameters
231
-
232
- ###### obj
89
+ #### hasOwn
233
90
 
234
- `unknown`
91
+ <a id="api-hasown"></a>
235
92
 
236
- ###### name
237
-
238
- `Name`
239
-
240
- ##### Returns
241
-
242
- `obj is { [K in string]: unknown }`
243
-
244
- #### Call Signature
245
-
246
- ```ts
247
- function hasOwn<Name, T>(
248
- obj,
249
- name,
250
- predicate): obj is { [K in string]: T }
93
+ ```typescript
94
+ declare function hasOwn<Name extends string, T>(obj: unknown, name: Name, predicate: (value: unknown) => value is T): obj is {
95
+ [K in Name]: T;
96
+ };
251
97
  ```
252
98
 
253
- ##### Type Parameters
254
-
255
- • **Name** *extends* `string`
256
-
257
- • **T**
258
-
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
99
+ #### isInt
274
100
 
275
- `obj is { [K in string]: T }`
101
+ <a id="api-isint"></a>
276
102
 
277
- ***
278
-
279
- ### isInt()
280
-
281
- ```ts
282
- function isInt(obj): obj is number
103
+ ```typescript
104
+ isInt: (obj: unknown) => obj is number
283
105
  ```
284
106
 
285
- #### Parameters
286
-
287
- ##### obj
107
+ #### isListOf
288
108
 
289
- `unknown`
290
-
291
- #### Returns
292
-
293
- `obj is number`
294
-
295
- ***
109
+ <a id="api-islistof"></a>
296
110
 
297
- ### isListOf()
298
-
299
- ```ts
300
- function isListOf<T>(list, predicate): list is T[]
111
+ ```typescript
112
+ isListOf: <T>(list: unknown, predicate: (item: unknown, index: number, items: unknown[]) => item is T) => list is T[]
301
113
  ```
302
114
 
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`
115
+ #### isObject
316
116
 
317
- #### Returns
117
+ <a id="api-isobject"></a>
318
118
 
319
- `list is T[]`
320
-
321
- ***
322
-
323
- ### isObject()
324
-
325
- ```ts
326
- function isObject(obj): obj is object
119
+ ```typescript
120
+ isObject: (obj: unknown) => obj is NonNullable<object>
327
121
  ```
328
122
 
329
- #### Parameters
123
+ #### isPlainObject
330
124
 
331
- ##### obj
125
+ <a id="api-isplainobject"></a>
332
126
 
333
- `unknown`
334
-
335
- #### Returns
336
-
337
- `obj is object`
127
+ ```typescript
128
+ isPlainObject: (obj: unknown) => obj is Record<never, never>
129
+ ```
338
130
 
339
- ***
131
+ #### isUnaryPredicate
340
132
 
341
- ### isPlainObject()
133
+ <a id="api-isunarypredicate"></a>
342
134
 
343
- ```ts
344
- function isPlainObject(obj): obj is Record<never, never>
135
+ ```typescript
136
+ isUnaryPredicate: (obj: unknown) => obj is UnaryPredicate<unknown>
345
137
  ```
346
138
 
347
- #### Parameters
139
+ Tests whether the given object is a function and takes at least one parameter, and could maybe act as a unary predicate. Warning! Since no type information is available at runtime, it may not actually act as a predicate!
348
140
 
349
- ##### obj
350
141
 
351
- `unknown`
142
+ ### TypeAliases
352
143
 
353
- #### Returns
144
+ #### MessageOrError
354
145
 
355
- `obj is Record<never, never>`
146
+ <a id="api-messageorerror"></a>
356
147
 
357
- ***
358
-
359
- ### isUnaryPredicate()
360
-
361
- ```ts
362
- function isUnaryPredicate(obj): obj is UnaryPredicate<unknown>
148
+ ```typescript
149
+ type MessageOrError = string | Error | (() => (string | Error));
363
150
  ```
364
151
 
365
- Tests whether the given object is a function and takes at
366
- least one parameter, and could maybe act as a unary predicate.
367
- Warning! Since no type information is available at runtime,
368
- it may not actually act as a predicate!
369
-
370
- #### Parameters
371
-
372
- ##### obj
373
-
374
- `unknown`
152
+ A message, an error, or something which can generate one of those.
375
153
 
376
- #### Returns
377
154
 
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.29"
7
+ "@rickosborne/typical": "2024.12.32"
8
8
  },
9
9
  "description": "Rick Osborne's collection of type guards.",
10
10
  "engines": {
@@ -19,17 +19,26 @@
19
19
  }
20
20
  },
21
21
  "homepage": "https://github.com/rickosborne/es-js-ts",
22
+ "keywords": [
23
+ "typescript",
24
+ "guards",
25
+ "type guards"
26
+ ],
22
27
  "license": "CC-BY-NC-SA-4.0",
23
28
  "main": "cjs/index.js",
29
+ "module": "esm/index.js",
24
30
  "name": "@rickosborne/guard",
25
31
  "private": false,
26
32
  "publishConfig": {
27
33
  "access": "public"
28
34
  },
35
+ "readme": "README.md",
29
36
  "repository": {
30
37
  "directory": "guard",
31
38
  "type": "git",
32
39
  "url": "git+https://github.com/rickosborne/es-js-ts.git"
33
40
  },
34
- "version": "2024.12.29"
41
+ "types": "types/index.d.ts",
42
+ "typings": "types/index.d.ts",
43
+ "version": "2024.12.32"
35
44
  }
package/types/index.d.ts CHANGED
@@ -7,3 +7,4 @@ export * from "./ts/is-int.js";
7
7
  export * from "./ts/is-list-of.js";
8
8
  export * from "./ts/is-object.js";
9
9
  export * from "./ts/is-predicate.js";
10
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC"}
@@ -1,2 +1,3 @@
1
1
  import { type MessageOrError } from "./error-from-message.js";
2
2
  export declare function assertDefined<T>(value: T, messageOrError: MessageOrError): asserts value is NonNullable<T>;
3
+ //# sourceMappingURL=assert-defined.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assert-defined.d.ts","sourceRoot":"","sources":["../../../ts/assert-defined.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAA2B,MAAM,yBAAyB,CAAC;AAEvF,wBAAgB,aAAa,CAAC,CAAC,EAC9B,KAAK,EAAE,CAAC,EACR,cAAc,EAAE,cAAc,GAC5B,OAAO,CAAC,KAAK,IAAI,WAAW,CAAC,CAAC,CAAC,CAKjC"}
@@ -7,3 +7,4 @@ export type MessageOrError = string | Error | (() => (string | Error));
7
7
  * can generate one when needed.
8
8
  */
9
9
  export declare const errorFromMessageOrError: (messageOrError: MessageOrError, defaultConstructor?: (new (message: string) => Error)) => Error;
10
+ //# sourceMappingURL=error-from-message.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error-from-message.d.ts","sourceRoot":"","sources":["../../../ts/error-from-message.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,KAAK,GAAG,CAAC,MAAM,CAAC,MAAM,GAAG,KAAK,CAAC,CAAC,CAAC;AAEvE;;;GAGG;AACH,eAAO,MAAM,uBAAuB,mBACnB,cAAc,uBACV,CAAC,KAAK,OAAO,EAAE,MAAM,KAAK,KAAK,CAAC,KAClD,KAcF,CAAC"}
@@ -12,3 +12,4 @@ export declare function hasArray<Name extends string>(obj: unknown, name: Name,
12
12
  export declare function hasArray<Name extends string, Item>(obj: unknown, name: Name, predicate?: (item: unknown, index: number, items: unknown[]) => item is Item): obj is {
13
13
  [K in Name]: Item[];
14
14
  };
15
+ //# sourceMappingURL=has-array.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"has-array.d.ts","sourceRoot":"","sources":["../../../ts/has-array.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,IAAI,SAAS,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,CAAC,EAAE,SAAS,GAAG,GAAG,IAAI;KAAG,CAAC,IAAI,IAAI,GAAG,OAAO,EAAE;CAAE,CAAC;AAClI;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,IAAI,SAAS,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,IAAI,IAAI,IAAI,GAAG,GAAG,IAAI;KAAG,CAAC,IAAI,IAAI,GAAG,IAAI,EAAE;CAAE,CAAC"}
@@ -1 +1,2 @@
1
1
  export declare const hasNumber: <Name extends string>(obj: unknown, name: Name) => obj is { [k in Name]: string; };
2
+ //# sourceMappingURL=has-number.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"has-number.d.ts","sourceRoot":"","sources":["../../../ts/has-number.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,SAAS,GAAI,IAAI,SAAS,MAAM,OAAO,OAAO,QAAQ,IAAI,KAAG,GAAG,IAAI,GAAE,CAAC,IAAI,IAAI,GAAG,MAAM,GAEpG,CAAC"}
@@ -4,3 +4,4 @@ export declare function hasOwn<Name extends string>(obj: unknown, name: Name): o
4
4
  export declare function hasOwn<Name extends string, T>(obj: unknown, name: Name, predicate: (value: unknown) => value is T): obj is {
5
5
  [K in Name]: T;
6
6
  };
7
+ //# sourceMappingURL=has-own.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"has-own.d.ts","sourceRoot":"","sources":["../../../ts/has-own.ts"],"names":[],"mappings":"AAEA,wBAAgB,MAAM,CAAC,IAAI,SAAS,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,GAAG,GAAG,IAAI;KAAE,CAAC,IAAI,IAAI,GAAG,OAAO;CAAC,CAAC;AACrG,wBAAgB,MAAM,CAAC,IAAI,SAAS,MAAM,EAAE,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,KAAK,IAAI,CAAC,GAAG,GAAG,IAAI;KAAE,CAAC,IAAI,IAAI,GAAG,CAAC;CAAC,CAAC"}
@@ -2,3 +2,4 @@ import { type MessageOrError } from "./error-from-message.js";
2
2
  export declare const isInt: (obj: unknown) => obj is number;
3
3
  export declare function assertInt(value: unknown, messageOrError: MessageOrError): asserts value is number;
4
4
  export declare const expectInt: (obj: unknown, messageOrError: MessageOrError) => number;
5
+ //# sourceMappingURL=is-int.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-int.d.ts","sourceRoot":"","sources":["../../../ts/is-int.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAA2B,MAAM,yBAAyB,CAAC;AAEvF,eAAO,MAAM,KAAK,QAAS,OAAO,KAAG,GAAG,IAAI,MAAoH,CAAC;AAEjK,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,cAAc,GAAG,OAAO,CAAC,KAAK,IAAI,MAAM,CAIjG;AAED,eAAO,MAAM,SAAS,QAChB,OAAO,kBACI,cAAc,KAC5B,MAGF,CAAC"}
@@ -1 +1,2 @@
1
1
  export declare const isListOf: <T>(list: unknown, predicate: (item: unknown, index: number, items: unknown[]) => item is T) => list is T[];
2
+ //# sourceMappingURL=is-list-of.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-list-of.d.ts","sourceRoot":"","sources":["../../../ts/is-list-of.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,QAAQ,GAAI,CAAC,QACnB,OAAO,aACF,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,IAAI,IAAI,CAAC,KACtE,IAAI,IAAI,CAAC,EAEX,CAAC"}
@@ -1,2 +1,3 @@
1
1
  export declare const isObject: (obj: unknown) => obj is NonNullable<object>;
2
2
  export declare const isPlainObject: (obj: unknown) => obj is Record<never, never>;
3
+ //# sourceMappingURL=is-object.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-object.d.ts","sourceRoot":"","sources":["../../../ts/is-object.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,QAAQ,QAAS,OAAO,KAAG,GAAG,IAAI,WAAW,CAAC,MAAM,CAEhE,CAAC;AAEF,eAAO,MAAM,aAAa,QAAS,OAAO,KAAG,GAAG,IAAI,MAAM,CAAC,KAAK,EAAC,KAAK,CAMrE,CAAC"}
@@ -6,3 +6,4 @@ import { UnaryPredicate } from "@rickosborne/typical";
6
6
  * it may not actually act as a predicate!
7
7
  */
8
8
  export declare const isUnaryPredicate: (obj: unknown) => obj is UnaryPredicate<unknown>;
9
+ //# sourceMappingURL=is-predicate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-predicate.d.ts","sourceRoot":"","sources":["../../../ts/is-predicate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAEtD;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,QAAS,OAAO,KAAG,GAAG,IAAI,cAAc,CAAC,OAAO,CAE5E,CAAC"}
@@ -0,0 +1,11 @@
1
+ // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
+ // It should be published with your NPM package. It should not be tracked by Git.
3
+ {
4
+ "tsdocVersion": "0.12",
5
+ "toolPackages": [
6
+ {
7
+ "packageName": "@microsoft/api-extractor",
8
+ "packageVersion": "7.48.1"
9
+ }
10
+ ]
11
+ }