is-what 3.11.0 → 3.11.2
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/.eslintignore +1 -0
- package/README.md +7 -5
- package/{build/rollup.js → build.js} +1 -1
- package/dist/index.cjs.js +15 -15
- package/dist/index.esm.js +15 -15
- package/package.json +18 -18
- package/src/index.ts +25 -22
- package/test/ava.ts +31 -0
- package/types/index.d.ts +26 -28
package/.eslintignore
CHANGED
package/README.md
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
# is What? 🙉
|
2
2
|
|
3
|
+
Very simple & small JS type check functions. It's fully TypeScript supported!
|
4
|
+
|
3
5
|
```
|
4
6
|
npm i is-what
|
5
7
|
```
|
6
8
|
|
7
|
-
|
9
|
+
Or for deno available at: `"deno.land/x/is_what"`
|
8
10
|
|
9
11
|
## Motivation
|
10
12
|
|
@@ -110,7 +112,7 @@ getType(specialObject) // returns 'Object'
|
|
110
112
|
|
111
113
|
is-what makes TypeScript know the type during if statements. This means that a check returns the type of the payload for TypeScript users.
|
112
114
|
|
113
|
-
```
|
115
|
+
```ts
|
114
116
|
function isNumber (payload: any): payload is number {
|
115
117
|
// return boolean
|
116
118
|
}
|
@@ -127,7 +129,7 @@ function fn (payload: string | number): number {
|
|
127
129
|
|
128
130
|
`isPlainObject` and `isAnyObject` with TypeScript will declare the payload to be an object type with any props:
|
129
131
|
|
130
|
-
```
|
132
|
+
```ts
|
131
133
|
function isPlainObject (payload: any): payload is {[key: string]: any}
|
132
134
|
function isAnyObject (payload: any): payload is {[key: string]: any}
|
133
135
|
// The reason to return `{[key: string]: any}` is to be able to do
|
@@ -139,7 +141,7 @@ if (isPlainObject(payload) && payload.id) return payload.id
|
|
139
141
|
|
140
142
|
If you want more control over which kind of objects are allowed you can use `isObjectLike<T>`:
|
141
143
|
|
142
|
-
```
|
144
|
+
```ts
|
143
145
|
import { isObjectLike } from 'is-what'
|
144
146
|
// usage examples:
|
145
147
|
isObjectLike<{specificKey: string}>(payload)
|
@@ -149,7 +151,7 @@ isObjectLike<object>(payload)
|
|
149
151
|
|
150
152
|
`isObjectLike<T>` works like this under the hood:
|
151
153
|
|
152
|
-
```
|
154
|
+
```ts
|
153
155
|
function isObjectLike<T extends object> (payload: any): payload is T {
|
154
156
|
return isAnyObject(payload)
|
155
157
|
}
|
@@ -18,7 +18,7 @@ import typescript from 'rollup-plugin-typescript2'
|
|
18
18
|
// ------------------------------------------------------------------------------------------
|
19
19
|
// setup
|
20
20
|
// ------------------------------------------------------------------------------------------
|
21
|
-
const pkg = require('
|
21
|
+
const pkg = require('./package.json')
|
22
22
|
const name = pkg.name
|
23
23
|
const className = name.replace(/(^\w|-\w)/g, c => c.replace('-', '').toUpperCase())
|
24
24
|
const external = Object.keys(pkg.dependencies || [])
|
package/dist/index.cjs.js
CHANGED
@@ -33,7 +33,7 @@ function isNull(payload) {
|
|
33
33
|
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
|
34
34
|
*
|
35
35
|
* @param {*} payload
|
36
|
-
* @returns {payload is
|
36
|
+
* @returns {payload is Record<string, any>}
|
37
37
|
*/
|
38
38
|
function isPlainObject(payload) {
|
39
39
|
if (getType(payload) !== 'Object')
|
@@ -44,7 +44,7 @@ function isPlainObject(payload) {
|
|
44
44
|
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
|
45
45
|
*
|
46
46
|
* @param {*} payload
|
47
|
-
* @returns {payload is
|
47
|
+
* @returns {payload is Record<string, any>}
|
48
48
|
*/
|
49
49
|
function isObject(payload) {
|
50
50
|
return isPlainObject(payload);
|
@@ -53,7 +53,7 @@ function isObject(payload) {
|
|
53
53
|
* Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)
|
54
54
|
*
|
55
55
|
* @param {*} payload
|
56
|
-
* @returns {payload is {}}
|
56
|
+
* @returns {payload is { [K in any]: never }}
|
57
57
|
*/
|
58
58
|
function isEmptyObject(payload) {
|
59
59
|
return isPlainObject(payload) && Object.keys(payload).length === 0;
|
@@ -62,7 +62,7 @@ function isEmptyObject(payload) {
|
|
62
62
|
* Returns whether the payload is an any kind of object (including special classes or objects with different prototypes)
|
63
63
|
*
|
64
64
|
* @param {*} payload
|
65
|
-
* @returns {payload is
|
65
|
+
* @returns {payload is Record<string, any>}
|
66
66
|
*/
|
67
67
|
function isAnyObject(payload) {
|
68
68
|
return getType(payload) === 'Object';
|
@@ -83,7 +83,7 @@ function isObjectLike(payload) {
|
|
83
83
|
* Returns whether the payload is a function
|
84
84
|
*
|
85
85
|
* @param {*} payload
|
86
|
-
* @returns {payload is
|
86
|
+
* @returns {payload is AnyFunction}
|
87
87
|
*/
|
88
88
|
function isFunction(payload) {
|
89
89
|
return getType(payload) === 'Function';
|
@@ -91,8 +91,8 @@ function isFunction(payload) {
|
|
91
91
|
/**
|
92
92
|
* Returns whether the payload is an array
|
93
93
|
*
|
94
|
-
* @param {
|
95
|
-
* @returns {payload is
|
94
|
+
* @param {any} payload
|
95
|
+
* @returns {payload is any[]}
|
96
96
|
*/
|
97
97
|
function isArray(payload) {
|
98
98
|
return getType(payload) === 'Array';
|
@@ -134,9 +134,9 @@ function isEmptyString(payload) {
|
|
134
134
|
return payload === '';
|
135
135
|
}
|
136
136
|
/**
|
137
|
-
* Returns whether the payload is a number
|
137
|
+
* Returns whether the payload is a number (but not NaN)
|
138
138
|
*
|
139
|
-
* This will return false for NaN
|
139
|
+
* This will return `false` for `NaN`!!
|
140
140
|
*
|
141
141
|
* @param {*} payload
|
142
142
|
* @returns {payload is number}
|
@@ -166,7 +166,7 @@ function isRegExp(payload) {
|
|
166
166
|
* Returns whether the payload is a Map
|
167
167
|
*
|
168
168
|
* @param {*} payload
|
169
|
-
* @returns {payload is Map}
|
169
|
+
* @returns {payload is Map<any, any>}
|
170
170
|
*/
|
171
171
|
function isMap(payload) {
|
172
172
|
return getType(payload) === 'Map';
|
@@ -175,7 +175,7 @@ function isMap(payload) {
|
|
175
175
|
* Returns whether the payload is a WeakMap
|
176
176
|
*
|
177
177
|
* @param {*} payload
|
178
|
-
* @returns {payload is WeakMap}
|
178
|
+
* @returns {payload is WeakMap<any, any>}
|
179
179
|
*/
|
180
180
|
function isWeakMap(payload) {
|
181
181
|
return getType(payload) === 'WeakMap';
|
@@ -184,7 +184,7 @@ function isWeakMap(payload) {
|
|
184
184
|
* Returns whether the payload is a Set
|
185
185
|
*
|
186
186
|
* @param {*} payload
|
187
|
-
* @returns {payload is Set}
|
187
|
+
* @returns {payload is Set<any>}
|
188
188
|
*/
|
189
189
|
function isSet(payload) {
|
190
190
|
return getType(payload) === 'Set';
|
@@ -193,7 +193,7 @@ function isSet(payload) {
|
|
193
193
|
* Returns whether the payload is a WeakSet
|
194
194
|
*
|
195
195
|
* @param {*} payload
|
196
|
-
* @returns {payload is WeakSet}
|
196
|
+
* @returns {payload is WeakSet<any>}
|
197
197
|
*/
|
198
198
|
function isWeakSet(payload) {
|
199
199
|
return getType(payload) === 'WeakSet';
|
@@ -238,7 +238,7 @@ function isFile(payload) {
|
|
238
238
|
* Returns whether the payload is a Promise
|
239
239
|
*
|
240
240
|
* @param {*} payload
|
241
|
-
* @returns {payload is Promise}
|
241
|
+
* @returns {payload is Promise<any>}
|
242
242
|
*/
|
243
243
|
function isPromise(payload) {
|
244
244
|
return getType(payload) === 'Promise';
|
@@ -253,7 +253,7 @@ function isError(payload) {
|
|
253
253
|
return getType(payload) === 'Error';
|
254
254
|
}
|
255
255
|
/**
|
256
|
-
* Returns whether the payload is `NaN`
|
256
|
+
* Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`)
|
257
257
|
*
|
258
258
|
* @param {*} payload
|
259
259
|
* @returns {payload is typeof NaN}
|
package/dist/index.esm.js
CHANGED
@@ -29,7 +29,7 @@ function isNull(payload) {
|
|
29
29
|
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
|
30
30
|
*
|
31
31
|
* @param {*} payload
|
32
|
-
* @returns {payload is
|
32
|
+
* @returns {payload is Record<string, any>}
|
33
33
|
*/
|
34
34
|
function isPlainObject(payload) {
|
35
35
|
if (getType(payload) !== 'Object')
|
@@ -40,7 +40,7 @@ function isPlainObject(payload) {
|
|
40
40
|
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
|
41
41
|
*
|
42
42
|
* @param {*} payload
|
43
|
-
* @returns {payload is
|
43
|
+
* @returns {payload is Record<string, any>}
|
44
44
|
*/
|
45
45
|
function isObject(payload) {
|
46
46
|
return isPlainObject(payload);
|
@@ -49,7 +49,7 @@ function isObject(payload) {
|
|
49
49
|
* Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)
|
50
50
|
*
|
51
51
|
* @param {*} payload
|
52
|
-
* @returns {payload is {}}
|
52
|
+
* @returns {payload is { [K in any]: never }}
|
53
53
|
*/
|
54
54
|
function isEmptyObject(payload) {
|
55
55
|
return isPlainObject(payload) && Object.keys(payload).length === 0;
|
@@ -58,7 +58,7 @@ function isEmptyObject(payload) {
|
|
58
58
|
* Returns whether the payload is an any kind of object (including special classes or objects with different prototypes)
|
59
59
|
*
|
60
60
|
* @param {*} payload
|
61
|
-
* @returns {payload is
|
61
|
+
* @returns {payload is Record<string, any>}
|
62
62
|
*/
|
63
63
|
function isAnyObject(payload) {
|
64
64
|
return getType(payload) === 'Object';
|
@@ -79,7 +79,7 @@ function isObjectLike(payload) {
|
|
79
79
|
* Returns whether the payload is a function
|
80
80
|
*
|
81
81
|
* @param {*} payload
|
82
|
-
* @returns {payload is
|
82
|
+
* @returns {payload is AnyFunction}
|
83
83
|
*/
|
84
84
|
function isFunction(payload) {
|
85
85
|
return getType(payload) === 'Function';
|
@@ -87,8 +87,8 @@ function isFunction(payload) {
|
|
87
87
|
/**
|
88
88
|
* Returns whether the payload is an array
|
89
89
|
*
|
90
|
-
* @param {
|
91
|
-
* @returns {payload is
|
90
|
+
* @param {any} payload
|
91
|
+
* @returns {payload is any[]}
|
92
92
|
*/
|
93
93
|
function isArray(payload) {
|
94
94
|
return getType(payload) === 'Array';
|
@@ -130,9 +130,9 @@ function isEmptyString(payload) {
|
|
130
130
|
return payload === '';
|
131
131
|
}
|
132
132
|
/**
|
133
|
-
* Returns whether the payload is a number
|
133
|
+
* Returns whether the payload is a number (but not NaN)
|
134
134
|
*
|
135
|
-
* This will return false for NaN
|
135
|
+
* This will return `false` for `NaN`!!
|
136
136
|
*
|
137
137
|
* @param {*} payload
|
138
138
|
* @returns {payload is number}
|
@@ -162,7 +162,7 @@ function isRegExp(payload) {
|
|
162
162
|
* Returns whether the payload is a Map
|
163
163
|
*
|
164
164
|
* @param {*} payload
|
165
|
-
* @returns {payload is Map}
|
165
|
+
* @returns {payload is Map<any, any>}
|
166
166
|
*/
|
167
167
|
function isMap(payload) {
|
168
168
|
return getType(payload) === 'Map';
|
@@ -171,7 +171,7 @@ function isMap(payload) {
|
|
171
171
|
* Returns whether the payload is a WeakMap
|
172
172
|
*
|
173
173
|
* @param {*} payload
|
174
|
-
* @returns {payload is WeakMap}
|
174
|
+
* @returns {payload is WeakMap<any, any>}
|
175
175
|
*/
|
176
176
|
function isWeakMap(payload) {
|
177
177
|
return getType(payload) === 'WeakMap';
|
@@ -180,7 +180,7 @@ function isWeakMap(payload) {
|
|
180
180
|
* Returns whether the payload is a Set
|
181
181
|
*
|
182
182
|
* @param {*} payload
|
183
|
-
* @returns {payload is Set}
|
183
|
+
* @returns {payload is Set<any>}
|
184
184
|
*/
|
185
185
|
function isSet(payload) {
|
186
186
|
return getType(payload) === 'Set';
|
@@ -189,7 +189,7 @@ function isSet(payload) {
|
|
189
189
|
* Returns whether the payload is a WeakSet
|
190
190
|
*
|
191
191
|
* @param {*} payload
|
192
|
-
* @returns {payload is WeakSet}
|
192
|
+
* @returns {payload is WeakSet<any>}
|
193
193
|
*/
|
194
194
|
function isWeakSet(payload) {
|
195
195
|
return getType(payload) === 'WeakSet';
|
@@ -234,7 +234,7 @@ function isFile(payload) {
|
|
234
234
|
* Returns whether the payload is a Promise
|
235
235
|
*
|
236
236
|
* @param {*} payload
|
237
|
-
* @returns {payload is Promise}
|
237
|
+
* @returns {payload is Promise<any>}
|
238
238
|
*/
|
239
239
|
function isPromise(payload) {
|
240
240
|
return getType(payload) === 'Promise';
|
@@ -249,7 +249,7 @@ function isError(payload) {
|
|
249
249
|
return getType(payload) === 'Error';
|
250
250
|
}
|
251
251
|
/**
|
252
|
-
* Returns whether the payload is `NaN`
|
252
|
+
* Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`)
|
253
253
|
*
|
254
254
|
* @param {*} payload
|
255
255
|
* @returns {payload is typeof NaN}
|
package/package.json
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
{
|
2
2
|
"name": "is-what",
|
3
3
|
"sideEffects": false,
|
4
|
-
"version": "3.11.
|
4
|
+
"version": "3.11.2",
|
5
5
|
"description": "JS type check (TypeScript supported) functions like `isPlainObject() isArray()` etc. A simple & small integration.",
|
6
6
|
"main": "dist/index.cjs.js",
|
7
7
|
"module": "dist/index.esm.js",
|
8
8
|
"typings": "types/index.d.ts",
|
9
9
|
"scripts": {
|
10
|
-
"
|
11
|
-
"
|
12
|
-
"
|
10
|
+
"test": "ava",
|
11
|
+
"jest": "jest",
|
12
|
+
"jest-w": "jest --watchAll",
|
13
13
|
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
|
14
|
-
"rollup": "rollup -c ./build
|
15
|
-
"build": "npm run lint && npm run rollup && npm run test"
|
14
|
+
"rollup": "rollup -c ./build.js",
|
15
|
+
"build": "rm -rf types && rm -rf dist && npm run lint && npm run rollup && npm run test && npm run jest"
|
16
16
|
},
|
17
17
|
"repository": {
|
18
18
|
"type": "git",
|
@@ -47,25 +47,25 @@
|
|
47
47
|
"homepage": "https://github.com/mesqueeb/is-what#readme",
|
48
48
|
"dependencies": {},
|
49
49
|
"devDependencies": {
|
50
|
-
"@babel/core": "^7.11.
|
50
|
+
"@babel/core": "^7.11.5",
|
51
51
|
"@types/babel-core": "^6.25.6",
|
52
|
-
"@types/jest": "^
|
53
|
-
"@typescript-eslint/eslint-plugin": "^
|
54
|
-
"@typescript-eslint/parser": "^
|
55
|
-
"ava": "^3.
|
52
|
+
"@types/jest": "^26.0.12",
|
53
|
+
"@typescript-eslint/eslint-plugin": "^4.0.1",
|
54
|
+
"@typescript-eslint/parser": "^4.0.1",
|
55
|
+
"ava": "^3.12.1",
|
56
56
|
"babel-core": "^7.0.0-bridge.0",
|
57
|
-
"babel-jest": "^
|
57
|
+
"babel-jest": "^26.3.0",
|
58
58
|
"babel-preset-env": "^1.7.0",
|
59
|
-
"eslint": "^
|
59
|
+
"eslint": "^7.8.1",
|
60
60
|
"eslint-config-prettier": "^6.11.0",
|
61
61
|
"eslint-plugin-tree-shaking": "^1.8.0",
|
62
|
-
"jest": "^
|
62
|
+
"jest": "^26.4.2",
|
63
63
|
"regenerator-runtime": "^0.13.7",
|
64
|
-
"rollup": "^
|
65
|
-
"rollup-plugin-typescript2": "^0.27.
|
64
|
+
"rollup": "^2.26.9",
|
65
|
+
"rollup-plugin-typescript2": "^0.27.2",
|
66
66
|
"tsconfig-paths": "^3.9.0",
|
67
|
-
"ts-node": "^
|
68
|
-
"typescript": "^
|
67
|
+
"ts-node": "^9.0.0",
|
68
|
+
"typescript": "^4.0.2"
|
69
69
|
},
|
70
70
|
"ava": {
|
71
71
|
"extensions": [
|
package/src/index.ts
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
export type AnyFunction = (...args: any[]) => any
|
2
|
+
export type AnyClass = new (...args: any[]) => any
|
3
|
+
|
1
4
|
/**
|
2
5
|
* Returns the object type of the given payload
|
3
6
|
*
|
@@ -32,9 +35,9 @@ export function isNull (payload: any): payload is null {
|
|
32
35
|
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
|
33
36
|
*
|
34
37
|
* @param {*} payload
|
35
|
-
* @returns {payload is
|
38
|
+
* @returns {payload is Record<string, any>}
|
36
39
|
*/
|
37
|
-
export function isPlainObject (payload: any): payload is
|
40
|
+
export function isPlainObject (payload: any): payload is Record<string, any> {
|
38
41
|
if (getType(payload) !== 'Object') return false
|
39
42
|
return payload.constructor === Object && Object.getPrototypeOf(payload) === Object.prototype
|
40
43
|
}
|
@@ -43,9 +46,9 @@ export function isPlainObject (payload: any): payload is { [key: string]: any }
|
|
43
46
|
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
|
44
47
|
*
|
45
48
|
* @param {*} payload
|
46
|
-
* @returns {payload is
|
49
|
+
* @returns {payload is Record<string, any>}
|
47
50
|
*/
|
48
|
-
export function isObject (payload: any): payload is
|
51
|
+
export function isObject (payload: any): payload is Record<string, any> {
|
49
52
|
return isPlainObject(payload)
|
50
53
|
}
|
51
54
|
|
@@ -53,9 +56,9 @@ export function isObject (payload: any): payload is { [key: string]: any } {
|
|
53
56
|
* Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)
|
54
57
|
*
|
55
58
|
* @param {*} payload
|
56
|
-
* @returns {payload is {}}
|
59
|
+
* @returns {payload is { [K in any]: never }}
|
57
60
|
*/
|
58
|
-
export function isEmptyObject (payload: any): payload is {} {
|
61
|
+
export function isEmptyObject (payload: any): payload is { [K in any]: never } {
|
59
62
|
return isPlainObject(payload) && Object.keys(payload).length === 0
|
60
63
|
}
|
61
64
|
|
@@ -63,9 +66,9 @@ export function isEmptyObject (payload: any): payload is {} {
|
|
63
66
|
* Returns whether the payload is an any kind of object (including special classes or objects with different prototypes)
|
64
67
|
*
|
65
68
|
* @param {*} payload
|
66
|
-
* @returns {payload is
|
69
|
+
* @returns {payload is Record<string, any>}
|
67
70
|
*/
|
68
|
-
export function isAnyObject (payload: any): payload is
|
71
|
+
export function isAnyObject (payload: any): payload is Record<string, any> {
|
69
72
|
return getType(payload) === 'Object'
|
70
73
|
}
|
71
74
|
|
@@ -78,7 +81,7 @@ export function isAnyObject (payload: any): payload is { [key: string]: any } {
|
|
78
81
|
* @param {*} payload
|
79
82
|
* @returns {payload is T}
|
80
83
|
*/
|
81
|
-
export function isObjectLike<T extends
|
84
|
+
export function isObjectLike<T extends Record<string, any>> (payload: any): payload is T {
|
82
85
|
return isAnyObject(payload)
|
83
86
|
}
|
84
87
|
|
@@ -86,17 +89,17 @@ export function isObjectLike<T extends object> (payload: any): payload is T {
|
|
86
89
|
* Returns whether the payload is a function
|
87
90
|
*
|
88
91
|
* @param {*} payload
|
89
|
-
* @returns {payload is
|
92
|
+
* @returns {payload is AnyFunction}
|
90
93
|
*/
|
91
|
-
export function isFunction (payload: any): payload is
|
94
|
+
export function isFunction (payload: any): payload is AnyFunction {
|
92
95
|
return getType(payload) === 'Function'
|
93
96
|
}
|
94
97
|
|
95
98
|
/**
|
96
99
|
* Returns whether the payload is an array
|
97
100
|
*
|
98
|
-
* @param {
|
99
|
-
* @returns {payload is
|
101
|
+
* @param {any} payload
|
102
|
+
* @returns {payload is any[]}
|
100
103
|
*/
|
101
104
|
export function isArray (payload: any): payload is any[] {
|
102
105
|
return getType(payload) === 'Array'
|
@@ -143,9 +146,9 @@ export function isEmptyString (payload: any): payload is string {
|
|
143
146
|
}
|
144
147
|
|
145
148
|
/**
|
146
|
-
* Returns whether the payload is a number
|
149
|
+
* Returns whether the payload is a number (but not NaN)
|
147
150
|
*
|
148
|
-
* This will return false for NaN
|
151
|
+
* This will return `false` for `NaN`!!
|
149
152
|
*
|
150
153
|
* @param {*} payload
|
151
154
|
* @returns {payload is number}
|
@@ -178,7 +181,7 @@ export function isRegExp (payload: any): payload is RegExp {
|
|
178
181
|
* Returns whether the payload is a Map
|
179
182
|
*
|
180
183
|
* @param {*} payload
|
181
|
-
* @returns {payload is Map}
|
184
|
+
* @returns {payload is Map<any, any>}
|
182
185
|
*/
|
183
186
|
export function isMap (payload: any): payload is Map<any, any> {
|
184
187
|
return getType(payload) === 'Map'
|
@@ -188,7 +191,7 @@ export function isMap (payload: any): payload is Map<any, any> {
|
|
188
191
|
* Returns whether the payload is a WeakMap
|
189
192
|
*
|
190
193
|
* @param {*} payload
|
191
|
-
* @returns {payload is WeakMap}
|
194
|
+
* @returns {payload is WeakMap<any, any>}
|
192
195
|
*/
|
193
196
|
export function isWeakMap (payload: any): payload is WeakMap<any, any> {
|
194
197
|
return getType(payload) === 'WeakMap'
|
@@ -198,7 +201,7 @@ export function isWeakMap (payload: any): payload is WeakMap<any, any> {
|
|
198
201
|
* Returns whether the payload is a Set
|
199
202
|
*
|
200
203
|
* @param {*} payload
|
201
|
-
* @returns {payload is Set}
|
204
|
+
* @returns {payload is Set<any>}
|
202
205
|
*/
|
203
206
|
export function isSet (payload: any): payload is Set<any> {
|
204
207
|
return getType(payload) === 'Set'
|
@@ -208,7 +211,7 @@ export function isSet (payload: any): payload is Set<any> {
|
|
208
211
|
* Returns whether the payload is a WeakSet
|
209
212
|
*
|
210
213
|
* @param {*} payload
|
211
|
-
* @returns {payload is WeakSet}
|
214
|
+
* @returns {payload is WeakSet<any>}
|
212
215
|
*/
|
213
216
|
export function isWeakSet (payload: any): payload is WeakSet<any> {
|
214
217
|
return getType(payload) === 'WeakSet'
|
@@ -258,7 +261,7 @@ export function isFile (payload: any): payload is File {
|
|
258
261
|
* Returns whether the payload is a Promise
|
259
262
|
*
|
260
263
|
* @param {*} payload
|
261
|
-
* @returns {payload is Promise}
|
264
|
+
* @returns {payload is Promise<any>}
|
262
265
|
*/
|
263
266
|
export function isPromise (payload: any): payload is Promise<any> {
|
264
267
|
return getType(payload) === 'Promise'
|
@@ -275,7 +278,7 @@ export function isError (payload: any): payload is Error {
|
|
275
278
|
}
|
276
279
|
|
277
280
|
/**
|
278
|
-
* Returns whether the payload is `NaN`
|
281
|
+
* Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`)
|
279
282
|
*
|
280
283
|
* @param {*} payload
|
281
284
|
* @returns {payload is typeof NaN}
|
@@ -324,7 +327,7 @@ export function isNullOrUndefined (payload: any): payload is null | undefined {
|
|
324
327
|
* @throws {TypeError} Will throw type error if type is an invalid type
|
325
328
|
* @returns {payload is T}
|
326
329
|
*/
|
327
|
-
export function isType<T extends
|
330
|
+
export function isType<T extends AnyFunction | AnyClass> (payload: any, type: T): payload is T {
|
328
331
|
if (!(type instanceof Function)) {
|
329
332
|
throw new TypeError('Type must be a function')
|
330
333
|
}
|
package/test/ava.ts
CHANGED
@@ -276,3 +276,34 @@ test('isObject vs isAnyObject', t => {
|
|
276
276
|
t.is(isAnyObject(new Date('_')), false)
|
277
277
|
t.is(isAnyObject(new Date()), false)
|
278
278
|
})
|
279
|
+
|
280
|
+
test('type related tests', t => {
|
281
|
+
t.pass()
|
282
|
+
// const fn: string | ((k: number) => string) = (p) => 'a'
|
283
|
+
// if (!isFunction(fn)) {
|
284
|
+
// fn
|
285
|
+
// }
|
286
|
+
|
287
|
+
// const a: Record<string, number> = {}
|
288
|
+
|
289
|
+
// a[fn(1)] = fn(2)
|
290
|
+
|
291
|
+
// const myArray: string | string[] = ['a', 'b']
|
292
|
+
// if (!isArray(myArray)) {
|
293
|
+
// myArray
|
294
|
+
// }
|
295
|
+
|
296
|
+
// const a: Record<string, number> = {}
|
297
|
+
|
298
|
+
// a[myArray[1]] = myArray[0]
|
299
|
+
|
300
|
+
// const myArray: string | any[] = [1, 2, 'a', 'b']
|
301
|
+
// if (!isArray(myArray)) {
|
302
|
+
// myArray
|
303
|
+
// }
|
304
|
+
|
305
|
+
// const a: Record<string, number> = {}
|
306
|
+
|
307
|
+
// a[myArray[1]] = myArray[0]
|
308
|
+
|
309
|
+
})
|
package/types/index.d.ts
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
export declare type AnyFunction = (...args: any[]) => any;
|
2
|
+
export declare type AnyClass = new (...args: any[]) => any;
|
1
3
|
/**
|
2
4
|
* Returns the object type of the given payload
|
3
5
|
*
|
@@ -23,36 +25,32 @@ export declare function isNull(payload: any): payload is null;
|
|
23
25
|
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
|
24
26
|
*
|
25
27
|
* @param {*} payload
|
26
|
-
* @returns {payload is
|
28
|
+
* @returns {payload is Record<string, any>}
|
27
29
|
*/
|
28
|
-
export declare function isPlainObject(payload: any): payload is
|
29
|
-
[key: string]: any;
|
30
|
-
};
|
30
|
+
export declare function isPlainObject(payload: any): payload is Record<string, any>;
|
31
31
|
/**
|
32
32
|
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
|
33
33
|
*
|
34
34
|
* @param {*} payload
|
35
|
-
* @returns {payload is
|
35
|
+
* @returns {payload is Record<string, any>}
|
36
36
|
*/
|
37
|
-
export declare function isObject(payload: any): payload is
|
38
|
-
[key: string]: any;
|
39
|
-
};
|
37
|
+
export declare function isObject(payload: any): payload is Record<string, any>;
|
40
38
|
/**
|
41
39
|
* Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)
|
42
40
|
*
|
43
41
|
* @param {*} payload
|
44
|
-
* @returns {payload is {}}
|
42
|
+
* @returns {payload is { [K in any]: never }}
|
45
43
|
*/
|
46
|
-
export declare function isEmptyObject(payload: any): payload is {
|
44
|
+
export declare function isEmptyObject(payload: any): payload is {
|
45
|
+
[K in any]: never;
|
46
|
+
};
|
47
47
|
/**
|
48
48
|
* Returns whether the payload is an any kind of object (including special classes or objects with different prototypes)
|
49
49
|
*
|
50
50
|
* @param {*} payload
|
51
|
-
* @returns {payload is
|
51
|
+
* @returns {payload is Record<string, any>}
|
52
52
|
*/
|
53
|
-
export declare function isAnyObject(payload: any): payload is
|
54
|
-
[key: string]: any;
|
55
|
-
};
|
53
|
+
export declare function isAnyObject(payload: any): payload is Record<string, any>;
|
56
54
|
/**
|
57
55
|
* Returns whether the payload is an object like a type passed in < >
|
58
56
|
*
|
@@ -62,19 +60,19 @@ export declare function isAnyObject(payload: any): payload is {
|
|
62
60
|
* @param {*} payload
|
63
61
|
* @returns {payload is T}
|
64
62
|
*/
|
65
|
-
export declare function isObjectLike<T extends
|
63
|
+
export declare function isObjectLike<T extends Record<string, any>>(payload: any): payload is T;
|
66
64
|
/**
|
67
65
|
* Returns whether the payload is a function
|
68
66
|
*
|
69
67
|
* @param {*} payload
|
70
|
-
* @returns {payload is
|
68
|
+
* @returns {payload is AnyFunction}
|
71
69
|
*/
|
72
|
-
export declare function isFunction(payload: any): payload is
|
70
|
+
export declare function isFunction(payload: any): payload is AnyFunction;
|
73
71
|
/**
|
74
72
|
* Returns whether the payload is an array
|
75
73
|
*
|
76
|
-
* @param {
|
77
|
-
* @returns {payload is
|
74
|
+
* @param {any} payload
|
75
|
+
* @returns {payload is any[]}
|
78
76
|
*/
|
79
77
|
export declare function isArray(payload: any): payload is any[];
|
80
78
|
/**
|
@@ -106,9 +104,9 @@ export declare function isFullString(payload: any): payload is string;
|
|
106
104
|
*/
|
107
105
|
export declare function isEmptyString(payload: any): payload is string;
|
108
106
|
/**
|
109
|
-
* Returns whether the payload is a number
|
107
|
+
* Returns whether the payload is a number (but not NaN)
|
110
108
|
*
|
111
|
-
* This will return false for NaN
|
109
|
+
* This will return `false` for `NaN`!!
|
112
110
|
*
|
113
111
|
* @param {*} payload
|
114
112
|
* @returns {payload is number}
|
@@ -132,28 +130,28 @@ export declare function isRegExp(payload: any): payload is RegExp;
|
|
132
130
|
* Returns whether the payload is a Map
|
133
131
|
*
|
134
132
|
* @param {*} payload
|
135
|
-
* @returns {payload is Map}
|
133
|
+
* @returns {payload is Map<any, any>}
|
136
134
|
*/
|
137
135
|
export declare function isMap(payload: any): payload is Map<any, any>;
|
138
136
|
/**
|
139
137
|
* Returns whether the payload is a WeakMap
|
140
138
|
*
|
141
139
|
* @param {*} payload
|
142
|
-
* @returns {payload is WeakMap}
|
140
|
+
* @returns {payload is WeakMap<any, any>}
|
143
141
|
*/
|
144
142
|
export declare function isWeakMap(payload: any): payload is WeakMap<any, any>;
|
145
143
|
/**
|
146
144
|
* Returns whether the payload is a Set
|
147
145
|
*
|
148
146
|
* @param {*} payload
|
149
|
-
* @returns {payload is Set}
|
147
|
+
* @returns {payload is Set<any>}
|
150
148
|
*/
|
151
149
|
export declare function isSet(payload: any): payload is Set<any>;
|
152
150
|
/**
|
153
151
|
* Returns whether the payload is a WeakSet
|
154
152
|
*
|
155
153
|
* @param {*} payload
|
156
|
-
* @returns {payload is WeakSet}
|
154
|
+
* @returns {payload is WeakSet<any>}
|
157
155
|
*/
|
158
156
|
export declare function isWeakSet(payload: any): payload is WeakSet<any>;
|
159
157
|
/**
|
@@ -188,7 +186,7 @@ export declare function isFile(payload: any): payload is File;
|
|
188
186
|
* Returns whether the payload is a Promise
|
189
187
|
*
|
190
188
|
* @param {*} payload
|
191
|
-
* @returns {payload is Promise}
|
189
|
+
* @returns {payload is Promise<any>}
|
192
190
|
*/
|
193
191
|
export declare function isPromise(payload: any): payload is Promise<any>;
|
194
192
|
/**
|
@@ -199,7 +197,7 @@ export declare function isPromise(payload: any): payload is Promise<any>;
|
|
199
197
|
*/
|
200
198
|
export declare function isError(payload: any): payload is Error;
|
201
199
|
/**
|
202
|
-
* Returns whether the payload is `NaN`
|
200
|
+
* Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`)
|
203
201
|
*
|
204
202
|
* @param {*} payload
|
205
203
|
* @returns {payload is typeof NaN}
|
@@ -230,4 +228,4 @@ export declare function isNullOrUndefined(payload: any): payload is null | undef
|
|
230
228
|
* @throws {TypeError} Will throw type error if type is an invalid type
|
231
229
|
* @returns {payload is T}
|
232
230
|
*/
|
233
|
-
export declare function isType<T extends
|
231
|
+
export declare function isType<T extends AnyFunction | AnyClass>(payload: any, type: T): payload is T;
|