@rickosborne/guard 2024.12.30 → 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 +96 -62
- package/package.json +11 -2
- package/types/index.d.ts +1 -0
- package/types/index.d.ts.map +1 -0
- package/types/ts/assert-defined.d.ts +1 -0
- package/types/ts/assert-defined.d.ts.map +1 -0
- package/types/ts/error-from-message.d.ts +1 -0
- package/types/ts/error-from-message.d.ts.map +1 -0
- package/types/ts/has-array.d.ts +1 -0
- package/types/ts/has-array.d.ts.map +1 -0
- package/types/ts/has-number.d.ts +1 -0
- package/types/ts/has-number.d.ts.map +1 -0
- package/types/ts/has-own.d.ts +1 -0
- package/types/ts/has-own.d.ts.map +1 -0
- package/types/ts/is-int.d.ts +1 -0
- package/types/ts/is-int.d.ts.map +1 -0
- package/types/ts/is-list-of.d.ts +1 -0
- package/types/ts/is-list-of.d.ts.map +1 -0
- package/types/ts/is-object.d.ts +1 -0
- package/types/ts/is-object.d.ts.map +1 -0
- package/types/ts/is-predicate.d.ts +1 -0
- package/types/ts/is-predicate.d.ts.map +1 -0
- package/types/tsdoc-metadata.json +11 -0
package/README.md
CHANGED
|
@@ -1,120 +1,154 @@
|
|
|
1
|
-
|
|
1
|
+
# @rickosborne/guard
|
|
2
2
|
|
|
3
|
+
Basic type guards building on [@rickosborne/typical](https://www.npmjs.com/package/@rickosborne/typical).
|
|
3
4
|
***
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
## API
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
### Functions
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
#### assertDefined
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
<a id="api-assertdefined"></a>
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
```typescript
|
|
15
|
+
declare function assertDefined<T>(value: T, messageOrError: MessageOrError): asserts value is NonNullable<T>;
|
|
16
|
+
```
|
|
14
17
|
|
|
15
|
-
|
|
18
|
+
#### assertInt
|
|
16
19
|
|
|
17
|
-
|
|
20
|
+
<a id="api-assertint"></a>
|
|
18
21
|
|
|
19
|
-
|
|
22
|
+
```typescript
|
|
23
|
+
declare function assertInt(value: unknown, messageOrError: MessageOrError): asserts value is number;
|
|
24
|
+
```
|
|
20
25
|
|
|
21
|
-
|
|
26
|
+
#### errorFromMessageOrError
|
|
22
27
|
|
|
23
|
-
>
|
|
28
|
+
<a id="api-errorfrommessageorerror"></a>
|
|
24
29
|
|
|
25
|
-
|
|
30
|
+
```typescript
|
|
31
|
+
errorFromMessageOrError: (messageOrError: MessageOrError, defaultConstructor?: (new (message: string) => Error)) => Error
|
|
32
|
+
```
|
|
26
33
|
|
|
27
|
-
|
|
34
|
+
Helper for guards which expect text or an error, or can generate one when needed.
|
|
28
35
|
|
|
29
|
-
Helper for guards which expect text or an error, or
|
|
30
|
-
can generate one when needed.
|
|
31
36
|
|
|
32
|
-
|
|
37
|
+
#### expectInt
|
|
33
38
|
|
|
34
|
-
>
|
|
39
|
+
<a id="api-expectint"></a>
|
|
35
40
|
|
|
36
|
-
|
|
41
|
+
```typescript
|
|
42
|
+
expectInt: (obj: unknown, messageOrError: MessageOrError) => number
|
|
43
|
+
```
|
|
37
44
|
|
|
38
|
-
|
|
45
|
+
#### hasArray
|
|
39
46
|
|
|
40
|
-
|
|
47
|
+
<a id="api-hasarray"></a>
|
|
41
48
|
|
|
42
|
-
|
|
49
|
+
```typescript
|
|
50
|
+
declare function hasArray<Name extends string>(obj: unknown, name: Name, predicate?: undefined): obj is {
|
|
51
|
+
[K in Name]: unknown[];
|
|
52
|
+
};
|
|
53
|
+
```
|
|
43
54
|
|
|
44
|
-
|
|
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.
|
|
45
56
|
|
|
46
|
-
Guard for whether the given value is an object which has a property with its own array value.
|
|
47
|
-
This variant does not exhaustively check the items of the array.
|
|
48
57
|
|
|
49
|
-
|
|
58
|
+
#### hasArray
|
|
50
59
|
|
|
51
|
-
|
|
60
|
+
<a id="api-hasarray"></a>
|
|
52
61
|
|
|
53
|
-
|
|
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
|
+
};
|
|
66
|
+
```
|
|
54
67
|
|
|
55
|
-
|
|
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.
|
|
56
69
|
|
|
57
|
-
Guard for whether the given value is an object which has a property with its own array value.
|
|
58
|
-
This variant exhaustively checks the items of the array against the given predicate.
|
|
59
70
|
|
|
60
|
-
|
|
71
|
+
#### hasNumber
|
|
61
72
|
|
|
62
|
-
|
|
73
|
+
<a id="api-hasnumber"></a>
|
|
63
74
|
|
|
64
|
-
|
|
75
|
+
```typescript
|
|
76
|
+
hasNumber: <Name extends string>(obj: unknown, name: Name) => obj is { [k in Name]: string; }
|
|
77
|
+
```
|
|
65
78
|
|
|
66
|
-
|
|
79
|
+
#### hasOwn
|
|
67
80
|
|
|
68
|
-
>
|
|
81
|
+
<a id="api-hasown"></a>
|
|
69
82
|
|
|
70
|
-
|
|
83
|
+
```typescript
|
|
84
|
+
declare function hasOwn<Name extends string>(obj: unknown, name: Name): obj is {
|
|
85
|
+
[K in Name]: unknown;
|
|
86
|
+
};
|
|
87
|
+
```
|
|
71
88
|
|
|
72
|
-
|
|
89
|
+
#### hasOwn
|
|
73
90
|
|
|
74
|
-
|
|
91
|
+
<a id="api-hasown"></a>
|
|
75
92
|
|
|
76
|
-
|
|
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
|
+
};
|
|
97
|
+
```
|
|
77
98
|
|
|
78
|
-
####
|
|
99
|
+
#### isInt
|
|
79
100
|
|
|
80
|
-
>
|
|
101
|
+
<a id="api-isint"></a>
|
|
81
102
|
|
|
82
|
-
|
|
103
|
+
```typescript
|
|
104
|
+
isInt: (obj: unknown) => obj is number
|
|
105
|
+
```
|
|
83
106
|
|
|
84
|
-
|
|
107
|
+
#### isListOf
|
|
85
108
|
|
|
86
|
-
|
|
109
|
+
<a id="api-islistof"></a>
|
|
87
110
|
|
|
88
|
-
|
|
111
|
+
```typescript
|
|
112
|
+
isListOf: <T>(list: unknown, predicate: (item: unknown, index: number, items: unknown[]) => item is T) => list is T[]
|
|
113
|
+
```
|
|
89
114
|
|
|
90
|
-
|
|
115
|
+
#### isObject
|
|
91
116
|
|
|
92
|
-
|
|
117
|
+
<a id="api-isobject"></a>
|
|
93
118
|
|
|
94
|
-
|
|
119
|
+
```typescript
|
|
120
|
+
isObject: (obj: unknown) => obj is NonNullable<object>
|
|
121
|
+
```
|
|
95
122
|
|
|
96
|
-
|
|
123
|
+
#### isPlainObject
|
|
97
124
|
|
|
98
|
-
>
|
|
125
|
+
<a id="api-isplainobject"></a>
|
|
99
126
|
|
|
100
|
-
|
|
127
|
+
```typescript
|
|
128
|
+
isPlainObject: (obj: unknown) => obj is Record<never, never>
|
|
129
|
+
```
|
|
101
130
|
|
|
102
|
-
|
|
131
|
+
#### isUnaryPredicate
|
|
103
132
|
|
|
104
|
-
|
|
133
|
+
<a id="api-isunarypredicate"></a>
|
|
105
134
|
|
|
106
|
-
|
|
135
|
+
```typescript
|
|
136
|
+
isUnaryPredicate: (obj: unknown) => obj is UnaryPredicate<unknown>
|
|
137
|
+
```
|
|
107
138
|
|
|
108
|
-
|
|
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!
|
|
109
140
|
|
|
110
|
-
> **isPlainObject**(obj): obj is Record<never, never>
|
|
111
141
|
|
|
112
|
-
|
|
142
|
+
### TypeAliases
|
|
143
|
+
|
|
144
|
+
#### MessageOrError
|
|
145
|
+
|
|
146
|
+
<a id="api-messageorerror"></a>
|
|
113
147
|
|
|
114
|
-
|
|
148
|
+
```typescript
|
|
149
|
+
type MessageOrError = string | Error | (() => (string | Error));
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
A message, an error, or something which can generate one of those.
|
|
115
153
|
|
|
116
|
-
Tests whether the given object is a function and takes at
|
|
117
|
-
least one parameter, and could maybe act as a unary predicate.
|
|
118
|
-
Warning! Since no type information is available at runtime,
|
|
119
|
-
it may not actually act as a predicate!
|
|
120
154
|
|
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.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
|
-
"
|
|
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
|
@@ -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"}
|
|
@@ -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"}
|
package/types/ts/has-array.d.ts
CHANGED
|
@@ -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"}
|
package/types/ts/has-number.d.ts
CHANGED
|
@@ -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"}
|
package/types/ts/has-own.d.ts
CHANGED
|
@@ -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"}
|
package/types/ts/is-int.d.ts
CHANGED
|
@@ -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"}
|
package/types/ts/is-list-of.d.ts
CHANGED
|
@@ -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"}
|
package/types/ts/is-object.d.ts
CHANGED
|
@@ -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"}
|
|
@@ -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
|
+
}
|