is-what 3.11.0 → 3.12.0
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/.eslintrc.js +1 -0
- package/README.md +7 -5
- package/{build/rollup.js → build.js} +1 -1
- package/dist/index.cjs.js +27 -17
- package/dist/index.esm.js +27 -18
- package/package.json +19 -19
- package/src/index.ts +38 -24
- package/test/ava.ts +80 -13
- package/test/index.test.js +5 -171
- package/tsconfig.json +1 -0
- package/types/index.d.ts +35 -29
package/.eslintignore
CHANGED
package/.eslintrc.js
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';
|
@@ -80,23 +80,32 @@ function isObjectLike(payload) {
|
|
80
80
|
return isAnyObject(payload);
|
81
81
|
}
|
82
82
|
/**
|
83
|
-
* Returns whether the payload is a function
|
83
|
+
* Returns whether the payload is a function (regular or async)
|
84
84
|
*
|
85
85
|
* @param {*} payload
|
86
|
-
* @returns {payload is
|
86
|
+
* @returns {payload is AnyFunction}
|
87
87
|
*/
|
88
88
|
function isFunction(payload) {
|
89
|
-
return
|
89
|
+
return typeof payload === "function";
|
90
90
|
}
|
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';
|
99
99
|
}
|
100
|
+
/**
|
101
|
+
* Returns whether the payload is a an array with at least 1 item
|
102
|
+
*
|
103
|
+
* @param {*} payload
|
104
|
+
* @returns {payload is any[]}
|
105
|
+
*/
|
106
|
+
function isFullArray(payload) {
|
107
|
+
return isArray(payload) && payload.length > 0;
|
108
|
+
}
|
100
109
|
/**
|
101
110
|
* Returns whether the payload is a an empty array
|
102
111
|
*
|
@@ -134,9 +143,9 @@ function isEmptyString(payload) {
|
|
134
143
|
return payload === '';
|
135
144
|
}
|
136
145
|
/**
|
137
|
-
* Returns whether the payload is a number
|
146
|
+
* Returns whether the payload is a number (but not NaN)
|
138
147
|
*
|
139
|
-
* This will return false for NaN
|
148
|
+
* This will return `false` for `NaN`!!
|
140
149
|
*
|
141
150
|
* @param {*} payload
|
142
151
|
* @returns {payload is number}
|
@@ -166,7 +175,7 @@ function isRegExp(payload) {
|
|
166
175
|
* Returns whether the payload is a Map
|
167
176
|
*
|
168
177
|
* @param {*} payload
|
169
|
-
* @returns {payload is Map}
|
178
|
+
* @returns {payload is Map<any, any>}
|
170
179
|
*/
|
171
180
|
function isMap(payload) {
|
172
181
|
return getType(payload) === 'Map';
|
@@ -175,7 +184,7 @@ function isMap(payload) {
|
|
175
184
|
* Returns whether the payload is a WeakMap
|
176
185
|
*
|
177
186
|
* @param {*} payload
|
178
|
-
* @returns {payload is WeakMap}
|
187
|
+
* @returns {payload is WeakMap<any, any>}
|
179
188
|
*/
|
180
189
|
function isWeakMap(payload) {
|
181
190
|
return getType(payload) === 'WeakMap';
|
@@ -184,7 +193,7 @@ function isWeakMap(payload) {
|
|
184
193
|
* Returns whether the payload is a Set
|
185
194
|
*
|
186
195
|
* @param {*} payload
|
187
|
-
* @returns {payload is Set}
|
196
|
+
* @returns {payload is Set<any>}
|
188
197
|
*/
|
189
198
|
function isSet(payload) {
|
190
199
|
return getType(payload) === 'Set';
|
@@ -193,7 +202,7 @@ function isSet(payload) {
|
|
193
202
|
* Returns whether the payload is a WeakSet
|
194
203
|
*
|
195
204
|
* @param {*} payload
|
196
|
-
* @returns {payload is WeakSet}
|
205
|
+
* @returns {payload is WeakSet<any>}
|
197
206
|
*/
|
198
207
|
function isWeakSet(payload) {
|
199
208
|
return getType(payload) === 'WeakSet';
|
@@ -238,7 +247,7 @@ function isFile(payload) {
|
|
238
247
|
* Returns whether the payload is a Promise
|
239
248
|
*
|
240
249
|
* @param {*} payload
|
241
|
-
* @returns {payload is Promise}
|
250
|
+
* @returns {payload is Promise<any>}
|
242
251
|
*/
|
243
252
|
function isPromise(payload) {
|
244
253
|
return getType(payload) === 'Promise';
|
@@ -253,7 +262,7 @@ function isError(payload) {
|
|
253
262
|
return getType(payload) === 'Error';
|
254
263
|
}
|
255
264
|
/**
|
256
|
-
* Returns whether the payload is `NaN`
|
265
|
+
* Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`)
|
257
266
|
*
|
258
267
|
* @param {*} payload
|
259
268
|
* @returns {payload is typeof NaN}
|
@@ -318,6 +327,7 @@ exports.isEmptyObject = isEmptyObject;
|
|
318
327
|
exports.isEmptyString = isEmptyString;
|
319
328
|
exports.isError = isError;
|
320
329
|
exports.isFile = isFile;
|
330
|
+
exports.isFullArray = isFullArray;
|
321
331
|
exports.isFullString = isFullString;
|
322
332
|
exports.isFunction = isFunction;
|
323
333
|
exports.isMap = isMap;
|
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';
|
@@ -76,23 +76,32 @@ function isObjectLike(payload) {
|
|
76
76
|
return isAnyObject(payload);
|
77
77
|
}
|
78
78
|
/**
|
79
|
-
* Returns whether the payload is a function
|
79
|
+
* Returns whether the payload is a function (regular or async)
|
80
80
|
*
|
81
81
|
* @param {*} payload
|
82
|
-
* @returns {payload is
|
82
|
+
* @returns {payload is AnyFunction}
|
83
83
|
*/
|
84
84
|
function isFunction(payload) {
|
85
|
-
return
|
85
|
+
return typeof payload === "function";
|
86
86
|
}
|
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';
|
95
95
|
}
|
96
|
+
/**
|
97
|
+
* Returns whether the payload is a an array with at least 1 item
|
98
|
+
*
|
99
|
+
* @param {*} payload
|
100
|
+
* @returns {payload is any[]}
|
101
|
+
*/
|
102
|
+
function isFullArray(payload) {
|
103
|
+
return isArray(payload) && payload.length > 0;
|
104
|
+
}
|
96
105
|
/**
|
97
106
|
* Returns whether the payload is a an empty array
|
98
107
|
*
|
@@ -130,9 +139,9 @@ function isEmptyString(payload) {
|
|
130
139
|
return payload === '';
|
131
140
|
}
|
132
141
|
/**
|
133
|
-
* Returns whether the payload is a number
|
142
|
+
* Returns whether the payload is a number (but not NaN)
|
134
143
|
*
|
135
|
-
* This will return false for NaN
|
144
|
+
* This will return `false` for `NaN`!!
|
136
145
|
*
|
137
146
|
* @param {*} payload
|
138
147
|
* @returns {payload is number}
|
@@ -162,7 +171,7 @@ function isRegExp(payload) {
|
|
162
171
|
* Returns whether the payload is a Map
|
163
172
|
*
|
164
173
|
* @param {*} payload
|
165
|
-
* @returns {payload is Map}
|
174
|
+
* @returns {payload is Map<any, any>}
|
166
175
|
*/
|
167
176
|
function isMap(payload) {
|
168
177
|
return getType(payload) === 'Map';
|
@@ -171,7 +180,7 @@ function isMap(payload) {
|
|
171
180
|
* Returns whether the payload is a WeakMap
|
172
181
|
*
|
173
182
|
* @param {*} payload
|
174
|
-
* @returns {payload is WeakMap}
|
183
|
+
* @returns {payload is WeakMap<any, any>}
|
175
184
|
*/
|
176
185
|
function isWeakMap(payload) {
|
177
186
|
return getType(payload) === 'WeakMap';
|
@@ -180,7 +189,7 @@ function isWeakMap(payload) {
|
|
180
189
|
* Returns whether the payload is a Set
|
181
190
|
*
|
182
191
|
* @param {*} payload
|
183
|
-
* @returns {payload is Set}
|
192
|
+
* @returns {payload is Set<any>}
|
184
193
|
*/
|
185
194
|
function isSet(payload) {
|
186
195
|
return getType(payload) === 'Set';
|
@@ -189,7 +198,7 @@ function isSet(payload) {
|
|
189
198
|
* Returns whether the payload is a WeakSet
|
190
199
|
*
|
191
200
|
* @param {*} payload
|
192
|
-
* @returns {payload is WeakSet}
|
201
|
+
* @returns {payload is WeakSet<any>}
|
193
202
|
*/
|
194
203
|
function isWeakSet(payload) {
|
195
204
|
return getType(payload) === 'WeakSet';
|
@@ -234,7 +243,7 @@ function isFile(payload) {
|
|
234
243
|
* Returns whether the payload is a Promise
|
235
244
|
*
|
236
245
|
* @param {*} payload
|
237
|
-
* @returns {payload is Promise}
|
246
|
+
* @returns {payload is Promise<any>}
|
238
247
|
*/
|
239
248
|
function isPromise(payload) {
|
240
249
|
return getType(payload) === 'Promise';
|
@@ -249,7 +258,7 @@ function isError(payload) {
|
|
249
258
|
return getType(payload) === 'Error';
|
250
259
|
}
|
251
260
|
/**
|
252
|
-
* Returns whether the payload is `NaN`
|
261
|
+
* Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`)
|
253
262
|
*
|
254
263
|
* @param {*} payload
|
255
264
|
* @returns {payload is typeof NaN}
|
@@ -303,4 +312,4 @@ function isType(payload, type) {
|
|
303
312
|
return getType(payload) === name || Boolean(payload && payload.constructor === type);
|
304
313
|
}
|
305
314
|
|
306
|
-
export { getType, isAnyObject, isArray, isBlob, isBoolean, isDate, isEmptyArray, isEmptyObject, isEmptyString, isError, isFile, isFullString, isFunction, isMap, isNaNValue, isNull, isNullOrUndefined, isNumber, isObject, isObjectLike, isPlainObject, isPrimitive, isPromise, isRegExp, isSet, isString, isSymbol, isType, isUndefined, isWeakMap, isWeakSet };
|
315
|
+
export { getType, isAnyObject, isArray, isBlob, isBoolean, isDate, isEmptyArray, isEmptyObject, isEmptyString, isError, isFile, isFullArray, isFullString, isFunction, isMap, isNaNValue, isNull, isNullOrUndefined, isNumber, isObject, isObjectLike, isPlainObject, isPrimitive, isPromise, isRegExp, isSet, isString, isSymbol, isType, isUndefined, isWeakMap, isWeakSet };
|
package/package.json
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
{
|
2
2
|
"name": "is-what",
|
3
3
|
"sideEffects": false,
|
4
|
-
"version": "3.
|
4
|
+
"version": "3.12.0",
|
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.
|
50
|
+
"@babel/core": "^7.12.3",
|
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.15",
|
53
|
+
"@typescript-eslint/eslint-plugin": "^4.7.0",
|
54
|
+
"@typescript-eslint/parser": "^4.7.0",
|
55
|
+
"ava": "^3.13.0",
|
56
56
|
"babel-core": "^7.0.0-bridge.0",
|
57
|
-
"babel-jest": "^
|
57
|
+
"babel-jest": "^26.6.3",
|
58
58
|
"babel-preset-env": "^1.7.0",
|
59
|
-
"eslint": "^
|
60
|
-
"eslint-config-prettier": "^6.
|
59
|
+
"eslint": "^7.13.0",
|
60
|
+
"eslint-config-prettier": "^6.15.0",
|
61
61
|
"eslint-plugin-tree-shaking": "^1.8.0",
|
62
|
-
"jest": "^
|
62
|
+
"jest": "^26.6.3",
|
63
63
|
"regenerator-runtime": "^0.13.7",
|
64
|
-
"rollup": "^
|
65
|
-
"rollup-plugin-typescript2": "^0.
|
64
|
+
"rollup": "^2.33.1",
|
65
|
+
"rollup-plugin-typescript2": "^0.29.0",
|
66
66
|
"tsconfig-paths": "^3.9.0",
|
67
|
-
"ts-node": "^
|
68
|
-
"typescript": "^
|
67
|
+
"ts-node": "^9.0.0",
|
68
|
+
"typescript": "^4.0.5"
|
69
69
|
},
|
70
70
|
"ava": {
|
71
71
|
"extensions": [
|
package/src/index.ts
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
export type AnyFunction = (...args: any[]) => any
|
2
|
+
export type AnyAsyncFunction = (...args: any[]) => Promise<any>
|
3
|
+
export type AnyClass = new (...args: any[]) => any
|
4
|
+
|
1
5
|
/**
|
2
6
|
* Returns the object type of the given payload
|
3
7
|
*
|
@@ -32,9 +36,9 @@ export function isNull (payload: any): payload is null {
|
|
32
36
|
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
|
33
37
|
*
|
34
38
|
* @param {*} payload
|
35
|
-
* @returns {payload is
|
39
|
+
* @returns {payload is Record<string, any>}
|
36
40
|
*/
|
37
|
-
export function isPlainObject (payload: any): payload is
|
41
|
+
export function isPlainObject (payload: any): payload is Record<string, any> {
|
38
42
|
if (getType(payload) !== 'Object') return false
|
39
43
|
return payload.constructor === Object && Object.getPrototypeOf(payload) === Object.prototype
|
40
44
|
}
|
@@ -43,9 +47,9 @@ export function isPlainObject (payload: any): payload is { [key: string]: any }
|
|
43
47
|
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
|
44
48
|
*
|
45
49
|
* @param {*} payload
|
46
|
-
* @returns {payload is
|
50
|
+
* @returns {payload is Record<string, any>}
|
47
51
|
*/
|
48
|
-
export function isObject (payload: any): payload is
|
52
|
+
export function isObject (payload: any): payload is Record<string, any> {
|
49
53
|
return isPlainObject(payload)
|
50
54
|
}
|
51
55
|
|
@@ -53,9 +57,9 @@ export function isObject (payload: any): payload is { [key: string]: any } {
|
|
53
57
|
* Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)
|
54
58
|
*
|
55
59
|
* @param {*} payload
|
56
|
-
* @returns {payload is {}}
|
60
|
+
* @returns {payload is { [K in any]: never }}
|
57
61
|
*/
|
58
|
-
export function isEmptyObject (payload: any): payload is {} {
|
62
|
+
export function isEmptyObject (payload: any): payload is { [K in any]: never } {
|
59
63
|
return isPlainObject(payload) && Object.keys(payload).length === 0
|
60
64
|
}
|
61
65
|
|
@@ -63,9 +67,9 @@ export function isEmptyObject (payload: any): payload is {} {
|
|
63
67
|
* Returns whether the payload is an any kind of object (including special classes or objects with different prototypes)
|
64
68
|
*
|
65
69
|
* @param {*} payload
|
66
|
-
* @returns {payload is
|
70
|
+
* @returns {payload is Record<string, any>}
|
67
71
|
*/
|
68
|
-
export function isAnyObject (payload: any): payload is
|
72
|
+
export function isAnyObject (payload: any): payload is Record<string, any> {
|
69
73
|
return getType(payload) === 'Object'
|
70
74
|
}
|
71
75
|
|
@@ -78,30 +82,40 @@ export function isAnyObject (payload: any): payload is { [key: string]: any } {
|
|
78
82
|
* @param {*} payload
|
79
83
|
* @returns {payload is T}
|
80
84
|
*/
|
81
|
-
export function isObjectLike<T extends
|
85
|
+
export function isObjectLike<T extends Record<string, any>> (payload: any): payload is T {
|
82
86
|
return isAnyObject(payload)
|
83
87
|
}
|
84
88
|
|
85
89
|
/**
|
86
|
-
* Returns whether the payload is a function
|
90
|
+
* Returns whether the payload is a function (regular or async)
|
87
91
|
*
|
88
92
|
* @param {*} payload
|
89
|
-
* @returns {payload is
|
93
|
+
* @returns {payload is AnyFunction}
|
90
94
|
*/
|
91
|
-
export function isFunction (payload: any): payload is
|
92
|
-
return
|
95
|
+
export function isFunction (payload: any): payload is AnyFunction {
|
96
|
+
return typeof payload === "function"
|
93
97
|
}
|
94
98
|
|
95
99
|
/**
|
96
100
|
* Returns whether the payload is an array
|
97
101
|
*
|
98
|
-
* @param {
|
99
|
-
* @returns {payload is
|
102
|
+
* @param {any} payload
|
103
|
+
* @returns {payload is any[]}
|
100
104
|
*/
|
101
105
|
export function isArray (payload: any): payload is any[] {
|
102
106
|
return getType(payload) === 'Array'
|
103
107
|
}
|
104
108
|
|
109
|
+
/**
|
110
|
+
* Returns whether the payload is a an array with at least 1 item
|
111
|
+
*
|
112
|
+
* @param {*} payload
|
113
|
+
* @returns {payload is any[]}
|
114
|
+
*/
|
115
|
+
export function isFullArray (payload: any): payload is any[] {
|
116
|
+
return isArray(payload) && payload.length > 0
|
117
|
+
}
|
118
|
+
|
105
119
|
/**
|
106
120
|
* Returns whether the payload is a an empty array
|
107
121
|
*
|
@@ -143,9 +157,9 @@ export function isEmptyString (payload: any): payload is string {
|
|
143
157
|
}
|
144
158
|
|
145
159
|
/**
|
146
|
-
* Returns whether the payload is a number
|
160
|
+
* Returns whether the payload is a number (but not NaN)
|
147
161
|
*
|
148
|
-
* This will return false for NaN
|
162
|
+
* This will return `false` for `NaN`!!
|
149
163
|
*
|
150
164
|
* @param {*} payload
|
151
165
|
* @returns {payload is number}
|
@@ -178,7 +192,7 @@ export function isRegExp (payload: any): payload is RegExp {
|
|
178
192
|
* Returns whether the payload is a Map
|
179
193
|
*
|
180
194
|
* @param {*} payload
|
181
|
-
* @returns {payload is Map}
|
195
|
+
* @returns {payload is Map<any, any>}
|
182
196
|
*/
|
183
197
|
export function isMap (payload: any): payload is Map<any, any> {
|
184
198
|
return getType(payload) === 'Map'
|
@@ -188,7 +202,7 @@ export function isMap (payload: any): payload is Map<any, any> {
|
|
188
202
|
* Returns whether the payload is a WeakMap
|
189
203
|
*
|
190
204
|
* @param {*} payload
|
191
|
-
* @returns {payload is WeakMap}
|
205
|
+
* @returns {payload is WeakMap<any, any>}
|
192
206
|
*/
|
193
207
|
export function isWeakMap (payload: any): payload is WeakMap<any, any> {
|
194
208
|
return getType(payload) === 'WeakMap'
|
@@ -198,7 +212,7 @@ export function isWeakMap (payload: any): payload is WeakMap<any, any> {
|
|
198
212
|
* Returns whether the payload is a Set
|
199
213
|
*
|
200
214
|
* @param {*} payload
|
201
|
-
* @returns {payload is Set}
|
215
|
+
* @returns {payload is Set<any>}
|
202
216
|
*/
|
203
217
|
export function isSet (payload: any): payload is Set<any> {
|
204
218
|
return getType(payload) === 'Set'
|
@@ -208,7 +222,7 @@ export function isSet (payload: any): payload is Set<any> {
|
|
208
222
|
* Returns whether the payload is a WeakSet
|
209
223
|
*
|
210
224
|
* @param {*} payload
|
211
|
-
* @returns {payload is WeakSet}
|
225
|
+
* @returns {payload is WeakSet<any>}
|
212
226
|
*/
|
213
227
|
export function isWeakSet (payload: any): payload is WeakSet<any> {
|
214
228
|
return getType(payload) === 'WeakSet'
|
@@ -258,7 +272,7 @@ export function isFile (payload: any): payload is File {
|
|
258
272
|
* Returns whether the payload is a Promise
|
259
273
|
*
|
260
274
|
* @param {*} payload
|
261
|
-
* @returns {payload is Promise}
|
275
|
+
* @returns {payload is Promise<any>}
|
262
276
|
*/
|
263
277
|
export function isPromise (payload: any): payload is Promise<any> {
|
264
278
|
return getType(payload) === 'Promise'
|
@@ -275,7 +289,7 @@ export function isError (payload: any): payload is Error {
|
|
275
289
|
}
|
276
290
|
|
277
291
|
/**
|
278
|
-
* Returns whether the payload is `NaN`
|
292
|
+
* Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`)
|
279
293
|
*
|
280
294
|
* @param {*} payload
|
281
295
|
* @returns {payload is typeof NaN}
|
@@ -324,7 +338,7 @@ export function isNullOrUndefined (payload: any): payload is null | undefined {
|
|
324
338
|
* @throws {TypeError} Will throw type error if type is an invalid type
|
325
339
|
* @returns {payload is T}
|
326
340
|
*/
|
327
|
-
export function isType<T extends
|
341
|
+
export function isType<T extends AnyFunction | AnyClass> (payload: any, type: T): payload is T {
|
328
342
|
if (!(type instanceof Function)) {
|
329
343
|
throw new TypeError('Type must be a function')
|
330
344
|
}
|
package/test/ava.ts
CHANGED
@@ -24,6 +24,7 @@ import {
|
|
24
24
|
isWeakMap,
|
25
25
|
isSet,
|
26
26
|
isWeakSet,
|
27
|
+
isFullArray,
|
27
28
|
// isBlob,
|
28
29
|
// isFile,
|
29
30
|
isPromise,
|
@@ -41,10 +42,6 @@ test('Basic true tests', t => {
|
|
41
42
|
t.is(isNullOrUndefined(undefined), true)
|
42
43
|
t.is(isObject({}), true)
|
43
44
|
t.is(isObject(new Object()), true)
|
44
|
-
t.is(
|
45
|
-
isFunction(_ => {}),
|
46
|
-
true
|
47
|
-
)
|
48
45
|
t.is(isArray([]), true)
|
49
46
|
t.is(isArray(new Array()), true)
|
50
47
|
t.is(isString(''), true)
|
@@ -76,7 +73,6 @@ test('Basic false tests', t => {
|
|
76
73
|
t.is(isUndefined(NaN), false)
|
77
74
|
t.is(isNull(NaN), false)
|
78
75
|
t.is(isObject(NaN), false)
|
79
|
-
t.is(isFunction(NaN), false)
|
80
76
|
t.is(isArray(NaN), false)
|
81
77
|
t.is(isString(NaN), false)
|
82
78
|
t.is(isEmptyString(' '), false)
|
@@ -91,6 +87,19 @@ test('Basic false tests', t => {
|
|
91
87
|
t.is(isNullOrUndefined(NaN), false)
|
92
88
|
})
|
93
89
|
|
90
|
+
test('isFunction', t => {
|
91
|
+
t.is(isFunction(NaN), false)
|
92
|
+
t.is(isFunction(() => {}), true)
|
93
|
+
t.is(isFunction(function () {}), true)
|
94
|
+
t.is(isFunction(async () => {}), true)
|
95
|
+
t.is(isFunction(async function () {}), true)
|
96
|
+
t.is(isFunction(function * () {}), true)
|
97
|
+
t.is(isFunction(async function * () {}), true)
|
98
|
+
const _ = { fn: () => {}, method () {} }
|
99
|
+
t.is(isFunction(_.fn), true)
|
100
|
+
t.is(isFunction(_.method), true)
|
101
|
+
})
|
102
|
+
|
94
103
|
test('isEmptyObject', t => {
|
95
104
|
t.is(isEmptyObject({}), true)
|
96
105
|
t.is(isEmptyObject(new Object()), true)
|
@@ -126,6 +135,27 @@ test('isEmptyArray', t => {
|
|
126
135
|
t.is(isEmptyArray(new WeakSet()), false)
|
127
136
|
})
|
128
137
|
|
138
|
+
test('isFullArray', t => {
|
139
|
+
t.is(isFullArray(new Array(1)), true)
|
140
|
+
t.is(isFullArray([undefined]), true)
|
141
|
+
t.is(isFullArray([null]), true)
|
142
|
+
t.is(isFullArray(['']), true)
|
143
|
+
|
144
|
+
t.is(isFullArray([]), false)
|
145
|
+
t.is(isFullArray(new Array()), false)
|
146
|
+
t.is(isFullArray(new Array(0)), false)
|
147
|
+
|
148
|
+
t.is(isFullArray(null), false)
|
149
|
+
t.is(isFullArray(new Date()), false)
|
150
|
+
t.is(isFullArray(new Error('')), false)
|
151
|
+
t.is(isFullArray(new Date()), false)
|
152
|
+
t.is(isFullArray(Symbol()), false)
|
153
|
+
t.is(isFullArray(new Map()), false)
|
154
|
+
t.is(isFullArray(new WeakMap()), false)
|
155
|
+
t.is(isFullArray(new Set()), false)
|
156
|
+
t.is(isFullArray(new WeakSet()), false)
|
157
|
+
})
|
158
|
+
|
129
159
|
test('NaN tests', t => {
|
130
160
|
t.is(isNaNValue(NaN), true)
|
131
161
|
t.is(isNaNValue(new Error('')), false)
|
@@ -135,7 +165,7 @@ test('NaN tests', t => {
|
|
135
165
|
t.is(isNaNValue({}), false)
|
136
166
|
t.is(isNaNValue(new Object()), false)
|
137
167
|
t.is(
|
138
|
-
isNaNValue(
|
168
|
+
isNaNValue(() => {}),
|
139
169
|
false
|
140
170
|
)
|
141
171
|
t.is(isNaNValue([]), false)
|
@@ -177,7 +207,7 @@ test('Primitive tests', t => {
|
|
177
207
|
t.is(isPrimitive(new Object()), false)
|
178
208
|
t.is(isPrimitive(new Date()), false)
|
179
209
|
t.is(
|
180
|
-
isPrimitive(
|
210
|
+
isPrimitive(() => {}),
|
181
211
|
false
|
182
212
|
)
|
183
213
|
})
|
@@ -187,11 +217,14 @@ test('Date exception', t => {
|
|
187
217
|
})
|
188
218
|
|
189
219
|
test('Generic isType', t => {
|
190
|
-
|
220
|
+
// -----------------------------
|
191
221
|
// This is correct old fashion syntax for classes, if this is missing
|
222
|
+
function MyClass () {}
|
192
223
|
MyClass.prototype.constructor = MyClass
|
193
|
-
|
224
|
+
// @ts-ignore
|
194
225
|
const myClass = new MyClass()
|
226
|
+
// -----------------------------
|
227
|
+
class MyOtherClass { constructor() {} }
|
195
228
|
// this is expected behaviour
|
196
229
|
t.is(isType('', String), true)
|
197
230
|
t.is(isType('_', String), true)
|
@@ -204,7 +237,7 @@ test('Generic isType', t => {
|
|
204
237
|
t.is(isType([], Array), true)
|
205
238
|
t.is(isType(new Array(), Array), true)
|
206
239
|
t.is(
|
207
|
-
isType(
|
240
|
+
isType(() => {}, Function),
|
208
241
|
true
|
209
242
|
)
|
210
243
|
t.is(isType(true, Boolean), true)
|
@@ -228,12 +261,15 @@ test('Generic isType', t => {
|
|
228
261
|
})
|
229
262
|
|
230
263
|
test('isObject vs isAnyObject', t => {
|
231
|
-
|
264
|
+
// -----------------------------
|
232
265
|
// This is correct old fashion syntax for classes, if this is missing
|
266
|
+
function MyClass () {}
|
233
267
|
MyClass.prototype.constructor = MyClass
|
268
|
+
// @ts-ignore
|
234
269
|
const myClass = new MyClass()
|
235
|
-
|
236
|
-
|
270
|
+
// -----------------------------
|
271
|
+
class MyClass2 { constructor() {} }
|
272
|
+
const myClass2 = new MyClass2()
|
237
273
|
const mySpecialObject = {}
|
238
274
|
Object.setPrototypeOf(mySpecialObject, {
|
239
275
|
toDate: function () {
|
@@ -276,3 +312,34 @@ test('isObject vs isAnyObject', t => {
|
|
276
312
|
t.is(isAnyObject(new Date('_')), false)
|
277
313
|
t.is(isAnyObject(new Date()), false)
|
278
314
|
})
|
315
|
+
|
316
|
+
test('type related tests', t => {
|
317
|
+
t.pass()
|
318
|
+
// const fn: string | ((k: number) => string) = (p) => 'a'
|
319
|
+
// if (!isFunction(fn)) {
|
320
|
+
// fn
|
321
|
+
// }
|
322
|
+
|
323
|
+
// const a: Record<string, number> = {}
|
324
|
+
|
325
|
+
// a[fn(1)] = fn(2)
|
326
|
+
|
327
|
+
// const myArray: string | string[] = ['a', 'b']
|
328
|
+
// if (!isArray(myArray)) {
|
329
|
+
// myArray
|
330
|
+
// }
|
331
|
+
|
332
|
+
// const a: Record<string, number> = {}
|
333
|
+
|
334
|
+
// a[myArray[1]] = myArray[0]
|
335
|
+
|
336
|
+
// const myArray: string | any[] = [1, 2, 'a', 'b']
|
337
|
+
// if (!isArray(myArray)) {
|
338
|
+
// myArray
|
339
|
+
// }
|
340
|
+
|
341
|
+
// const a: Record<string, number> = {}
|
342
|
+
|
343
|
+
// a[myArray[1]] = myArray[0]
|
344
|
+
|
345
|
+
})
|
package/test/index.test.js
CHANGED
@@ -1,181 +1,15 @@
|
|
1
1
|
// @ts-check
|
2
2
|
import {
|
3
|
-
isObject,
|
4
|
-
isPlainObject,
|
5
|
-
isAnyObject,
|
6
|
-
isUndefined,
|
7
|
-
isNull,
|
8
|
-
isNullOrUndefined,
|
9
|
-
isFunction,
|
10
|
-
isArray,
|
11
|
-
isString,
|
12
|
-
isEmptyString,
|
13
|
-
isFullString,
|
14
|
-
isBoolean,
|
15
|
-
isRegExp,
|
16
|
-
isNumber,
|
17
|
-
isDate,
|
18
|
-
isSymbol,
|
19
|
-
isPrimitive,
|
20
|
-
isType,
|
21
3
|
isBlob,
|
22
4
|
isFile,
|
23
|
-
isPromise,
|
24
5
|
} from '../dist/index.cjs'
|
25
6
|
|
26
|
-
test('
|
27
|
-
expect(
|
28
|
-
expect(isNull(null)).toBe(true)
|
29
|
-
expect(isNullOrUndefined(null)).toBe(true)
|
30
|
-
expect(isNullOrUndefined(undefined)).toBe(true)
|
31
|
-
expect(isObject({})).toBe(true)
|
32
|
-
expect(isObject(new Object())).toBe(true)
|
33
|
-
expect(isFunction(_ => {})).toBe(true)
|
34
|
-
expect(isArray([])).toBe(true)
|
35
|
-
expect(isArray(new Array())).toBe(true)
|
36
|
-
expect(isString('')).toBe(true)
|
37
|
-
expect(isString('_')).toBe(true)
|
38
|
-
expect(isEmptyString('')).toBe(true)
|
39
|
-
expect(isFullString(' ')).toBe(true)
|
40
|
-
expect(isBoolean(true)).toBe(true)
|
41
|
-
expect(isBoolean(false)).toBe(true)
|
42
|
-
expect(isRegExp(/./)).toBe(true)
|
43
|
-
expect(isRegExp(/./gi)).toBe(true)
|
44
|
-
expect(isNumber(0)).toBe(true)
|
45
|
-
expect(isNumber(1)).toBe(true)
|
46
|
-
expect(isDate(new Date())).toBe(true)
|
47
|
-
expect(isSymbol(Symbol())).toBe(true)
|
7
|
+
test('isBlob', () => {
|
8
|
+
expect(isBlob(Blob)).toBe(false)
|
48
9
|
expect(isBlob(new Blob())).toBe(true)
|
49
|
-
expect(isFile(new File([''], '', { type: 'text/html' }))).toBe(true)
|
50
|
-
expect(isPromise(new Promise((resolve, reject) => {}))).toBe(true)
|
51
|
-
})
|
52
|
-
|
53
|
-
test('Basic false tests', () => {
|
54
|
-
expect(isNumber(NaN)).toBe(false)
|
55
|
-
expect(isDate(new Date('_'))).toBe(false)
|
56
|
-
expect(isDate(NaN)).toBe(false)
|
57
|
-
expect(isUndefined(NaN)).toBe(false)
|
58
|
-
expect(isNull(NaN)).toBe(false)
|
59
|
-
expect(isObject(NaN)).toBe(false)
|
60
|
-
expect(isFunction(NaN)).toBe(false)
|
61
|
-
expect(isArray(NaN)).toBe(false)
|
62
|
-
expect(isString(NaN)).toBe(false)
|
63
|
-
expect(isEmptyString(' ')).toBe(false)
|
64
|
-
expect(isFullString('')).toBe(false)
|
65
|
-
expect(isBoolean(NaN)).toBe(false)
|
66
|
-
expect(isRegExp(NaN)).toBe(false)
|
67
|
-
expect(isSymbol(NaN)).toBe(false)
|
68
|
-
expect(isNullOrUndefined(NaN)).toBe(false)
|
69
|
-
})
|
70
|
-
|
71
|
-
test('Primitive tests', () => {
|
72
|
-
// true
|
73
|
-
expect(isPrimitive(0)).toBe(true)
|
74
|
-
expect(isPrimitive('')).toBe(true)
|
75
|
-
expect(isPrimitive('str')).toBe(true)
|
76
|
-
expect(isPrimitive(Symbol())).toBe(true)
|
77
|
-
expect(isPrimitive(true)).toBe(true)
|
78
|
-
expect(isPrimitive(false)).toBe(true)
|
79
|
-
expect(isPrimitive(null)).toBe(true)
|
80
|
-
expect(isPrimitive(undefined)).toBe(true)
|
81
|
-
// false
|
82
|
-
expect(isPrimitive(NaN)).toBe(false)
|
83
|
-
expect(isPrimitive([])).toBe(false)
|
84
|
-
expect(isPrimitive(new Array())).toBe(false)
|
85
|
-
expect(isPrimitive({})).toBe(false)
|
86
|
-
expect(isPrimitive(new Object())).toBe(false)
|
87
|
-
expect(isPrimitive(new Date())).toBe(false)
|
88
|
-
expect(isPrimitive(_ => {})).toBe(false)
|
89
10
|
})
|
90
11
|
|
91
|
-
test('
|
92
|
-
expect(
|
93
|
-
})
|
94
|
-
|
95
|
-
test('Generic isType', () => {
|
96
|
-
function MyClass () {}
|
97
|
-
// This is correct old fashion syntax for classes, if this is missing
|
98
|
-
MyClass.prototype.constructor = MyClass
|
99
|
-
class MyOtherClass {}
|
100
|
-
const myClass = new MyClass()
|
101
|
-
// this is expected behaviour
|
102
|
-
expect(isType('', String)).toBe(true)
|
103
|
-
expect(isType('_', String)).toBe(true)
|
104
|
-
expect(isType('Hello World', String)).toBe(true)
|
105
|
-
expect(isType(NaN, Number)).toBe(true)
|
106
|
-
expect(isType(0, Number)).toBe(true)
|
107
|
-
expect(isType(1, Number)).toBe(true)
|
108
|
-
expect(isType({}, Object)).toBe(true)
|
109
|
-
expect(isType(new Object(), Object)).toBe(true)
|
110
|
-
expect(isType([], Array)).toBe(true)
|
111
|
-
expect(isType(new Array(), Array)).toBe(true)
|
112
|
-
expect(isType(_ => {}, Function)).toBe(true)
|
113
|
-
expect(isType(true, Boolean)).toBe(true)
|
114
|
-
expect(isType(false, Boolean)).toBe(true)
|
115
|
-
expect(isType(new Date('_'), Date)).toBe(true)
|
116
|
-
expect(isType(new Date(), Date)).toBe(true)
|
117
|
-
expect(isType(/./, RegExp)).toBe(true)
|
118
|
-
expect(isType(/./gi, RegExp)).toBe(true)
|
119
|
-
expect(isType(myClass, MyClass)).toBe(true)
|
120
|
-
expect(isType(new MyOtherClass(), MyOtherClass)).toBe(true)
|
121
|
-
expect(isType(myClass, MyOtherClass)).toBe(false)
|
122
|
-
expect(isType(Symbol(), Symbol)).toBe(true)
|
123
|
-
// expect(isType(null, Null)).toBe(true)
|
124
|
-
// expect(isType(undefined, Undefined)).toBe(true)
|
125
|
-
// It SHOULD fail
|
126
|
-
expect(isType(5, String)).toBe(false)
|
127
|
-
expect(isType(null, Object)).toBe(false)
|
128
|
-
// Not sure if this would be the expected behaviour but everything is an object
|
129
|
-
// so I would say so
|
130
|
-
expect(isType(myClass, Object)).toBe(true)
|
131
|
-
})
|
132
|
-
|
133
|
-
test('isObject vs isAnyObject', () => {
|
134
|
-
function MyClass () {}
|
135
|
-
// This is correct old fashion syntax for classes, if this is missing
|
136
|
-
MyClass.prototype.constructor = MyClass
|
137
|
-
const myClass = new MyClass()
|
138
|
-
class MyClass2 {}
|
139
|
-
const myClass2 = new MyClass()
|
140
|
-
const mySpecialObject = {}
|
141
|
-
Object.setPrototypeOf(mySpecialObject, {
|
142
|
-
toDate: function () {
|
143
|
-
return new Date()
|
144
|
-
},
|
145
|
-
})
|
146
|
-
// IS OBJECT
|
147
|
-
// plain object
|
148
|
-
expect(isObject({})).toBe(true)
|
149
|
-
expect(isObject(new Object())).toBe(true)
|
150
|
-
expect(isPlainObject({})).toBe(true)
|
151
|
-
expect(isPlainObject(new Object())).toBe(true)
|
152
|
-
// classes & prototypes
|
153
|
-
expect(isObject(myClass)).toBe(false)
|
154
|
-
expect(isObject(myClass2)).toBe(false)
|
155
|
-
expect(isObject(mySpecialObject)).toBe(false)
|
156
|
-
expect(isPlainObject(myClass)).toBe(false)
|
157
|
-
expect(isPlainObject(myClass2)).toBe(false)
|
158
|
-
expect(isPlainObject(mySpecialObject)).toBe(false)
|
159
|
-
// arrays and dates
|
160
|
-
expect(isObject([])).toBe(false)
|
161
|
-
expect(isObject(new Array())).toBe(false)
|
162
|
-
expect(isObject(new Date('_'))).toBe(false)
|
163
|
-
expect(isObject(new Date())).toBe(false)
|
164
|
-
expect(isPlainObject([])).toBe(false)
|
165
|
-
expect(isPlainObject(new Array())).toBe(false)
|
166
|
-
expect(isPlainObject(new Date('_'))).toBe(false)
|
167
|
-
expect(isPlainObject(new Date())).toBe(false)
|
168
|
-
// IS ANY OBJECT
|
169
|
-
// plain object
|
170
|
-
expect(isAnyObject({})).toBe(true)
|
171
|
-
expect(isAnyObject(new Object())).toBe(true)
|
172
|
-
// classes & prototypes
|
173
|
-
expect(isAnyObject(myClass)).toBe(true)
|
174
|
-
expect(isAnyObject(myClass2)).toBe(true)
|
175
|
-
expect(isAnyObject(mySpecialObject)).toBe(true)
|
176
|
-
// arrays and dates
|
177
|
-
expect(isAnyObject([])).toBe(false)
|
178
|
-
expect(isAnyObject(new Array())).toBe(false)
|
179
|
-
expect(isAnyObject(new Date('_'))).toBe(false)
|
180
|
-
expect(isAnyObject(new Date())).toBe(false)
|
12
|
+
test('isFile', () => {
|
13
|
+
expect(isFile(File)).toBe(false)
|
14
|
+
expect(isFile(new File([''], '', { type: 'text/html' }))).toBe(true)
|
181
15
|
})
|
package/tsconfig.json
CHANGED
package/types/index.d.ts
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
export declare type AnyFunction = (...args: any[]) => any;
|
2
|
+
export declare type AnyAsyncFunction = (...args: any[]) => Promise<any>;
|
3
|
+
export declare type AnyClass = new (...args: any[]) => any;
|
1
4
|
/**
|
2
5
|
* Returns the object type of the given payload
|
3
6
|
*
|
@@ -23,36 +26,32 @@ export declare function isNull(payload: any): payload is null;
|
|
23
26
|
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
|
24
27
|
*
|
25
28
|
* @param {*} payload
|
26
|
-
* @returns {payload is
|
29
|
+
* @returns {payload is Record<string, any>}
|
27
30
|
*/
|
28
|
-
export declare function isPlainObject(payload: any): payload is
|
29
|
-
[key: string]: any;
|
30
|
-
};
|
31
|
+
export declare function isPlainObject(payload: any): payload is Record<string, any>;
|
31
32
|
/**
|
32
33
|
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
|
33
34
|
*
|
34
35
|
* @param {*} payload
|
35
|
-
* @returns {payload is
|
36
|
+
* @returns {payload is Record<string, any>}
|
36
37
|
*/
|
37
|
-
export declare function isObject(payload: any): payload is
|
38
|
-
[key: string]: any;
|
39
|
-
};
|
38
|
+
export declare function isObject(payload: any): payload is Record<string, any>;
|
40
39
|
/**
|
41
40
|
* Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)
|
42
41
|
*
|
43
42
|
* @param {*} payload
|
44
|
-
* @returns {payload is {}}
|
43
|
+
* @returns {payload is { [K in any]: never }}
|
45
44
|
*/
|
46
|
-
export declare function isEmptyObject(payload: any): payload is {
|
45
|
+
export declare function isEmptyObject(payload: any): payload is {
|
46
|
+
[K in any]: never;
|
47
|
+
};
|
47
48
|
/**
|
48
49
|
* Returns whether the payload is an any kind of object (including special classes or objects with different prototypes)
|
49
50
|
*
|
50
51
|
* @param {*} payload
|
51
|
-
* @returns {payload is
|
52
|
+
* @returns {payload is Record<string, any>}
|
52
53
|
*/
|
53
|
-
export declare function isAnyObject(payload: any): payload is
|
54
|
-
[key: string]: any;
|
55
|
-
};
|
54
|
+
export declare function isAnyObject(payload: any): payload is Record<string, any>;
|
56
55
|
/**
|
57
56
|
* Returns whether the payload is an object like a type passed in < >
|
58
57
|
*
|
@@ -62,21 +61,28 @@ export declare function isAnyObject(payload: any): payload is {
|
|
62
61
|
* @param {*} payload
|
63
62
|
* @returns {payload is T}
|
64
63
|
*/
|
65
|
-
export declare function isObjectLike<T extends
|
64
|
+
export declare function isObjectLike<T extends Record<string, any>>(payload: any): payload is T;
|
66
65
|
/**
|
67
|
-
* Returns whether the payload is a function
|
66
|
+
* Returns whether the payload is a function (regular or async)
|
68
67
|
*
|
69
68
|
* @param {*} payload
|
70
|
-
* @returns {payload is
|
69
|
+
* @returns {payload is AnyFunction}
|
71
70
|
*/
|
72
|
-
export declare function isFunction(payload: any): payload is
|
71
|
+
export declare function isFunction(payload: any): payload is AnyFunction;
|
73
72
|
/**
|
74
73
|
* Returns whether the payload is an array
|
75
74
|
*
|
76
|
-
* @param {
|
77
|
-
* @returns {payload is
|
75
|
+
* @param {any} payload
|
76
|
+
* @returns {payload is any[]}
|
78
77
|
*/
|
79
78
|
export declare function isArray(payload: any): payload is any[];
|
79
|
+
/**
|
80
|
+
* Returns whether the payload is a an array with at least 1 item
|
81
|
+
*
|
82
|
+
* @param {*} payload
|
83
|
+
* @returns {payload is any[]}
|
84
|
+
*/
|
85
|
+
export declare function isFullArray(payload: any): payload is any[];
|
80
86
|
/**
|
81
87
|
* Returns whether the payload is a an empty array
|
82
88
|
*
|
@@ -106,9 +112,9 @@ export declare function isFullString(payload: any): payload is string;
|
|
106
112
|
*/
|
107
113
|
export declare function isEmptyString(payload: any): payload is string;
|
108
114
|
/**
|
109
|
-
* Returns whether the payload is a number
|
115
|
+
* Returns whether the payload is a number (but not NaN)
|
110
116
|
*
|
111
|
-
* This will return false for NaN
|
117
|
+
* This will return `false` for `NaN`!!
|
112
118
|
*
|
113
119
|
* @param {*} payload
|
114
120
|
* @returns {payload is number}
|
@@ -132,28 +138,28 @@ export declare function isRegExp(payload: any): payload is RegExp;
|
|
132
138
|
* Returns whether the payload is a Map
|
133
139
|
*
|
134
140
|
* @param {*} payload
|
135
|
-
* @returns {payload is Map}
|
141
|
+
* @returns {payload is Map<any, any>}
|
136
142
|
*/
|
137
143
|
export declare function isMap(payload: any): payload is Map<any, any>;
|
138
144
|
/**
|
139
145
|
* Returns whether the payload is a WeakMap
|
140
146
|
*
|
141
147
|
* @param {*} payload
|
142
|
-
* @returns {payload is WeakMap}
|
148
|
+
* @returns {payload is WeakMap<any, any>}
|
143
149
|
*/
|
144
150
|
export declare function isWeakMap(payload: any): payload is WeakMap<any, any>;
|
145
151
|
/**
|
146
152
|
* Returns whether the payload is a Set
|
147
153
|
*
|
148
154
|
* @param {*} payload
|
149
|
-
* @returns {payload is Set}
|
155
|
+
* @returns {payload is Set<any>}
|
150
156
|
*/
|
151
157
|
export declare function isSet(payload: any): payload is Set<any>;
|
152
158
|
/**
|
153
159
|
* Returns whether the payload is a WeakSet
|
154
160
|
*
|
155
161
|
* @param {*} payload
|
156
|
-
* @returns {payload is WeakSet}
|
162
|
+
* @returns {payload is WeakSet<any>}
|
157
163
|
*/
|
158
164
|
export declare function isWeakSet(payload: any): payload is WeakSet<any>;
|
159
165
|
/**
|
@@ -188,7 +194,7 @@ export declare function isFile(payload: any): payload is File;
|
|
188
194
|
* Returns whether the payload is a Promise
|
189
195
|
*
|
190
196
|
* @param {*} payload
|
191
|
-
* @returns {payload is Promise}
|
197
|
+
* @returns {payload is Promise<any>}
|
192
198
|
*/
|
193
199
|
export declare function isPromise(payload: any): payload is Promise<any>;
|
194
200
|
/**
|
@@ -199,7 +205,7 @@ export declare function isPromise(payload: any): payload is Promise<any>;
|
|
199
205
|
*/
|
200
206
|
export declare function isError(payload: any): payload is Error;
|
201
207
|
/**
|
202
|
-
* Returns whether the payload is `NaN`
|
208
|
+
* Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`)
|
203
209
|
*
|
204
210
|
* @param {*} payload
|
205
211
|
* @returns {payload is typeof NaN}
|
@@ -230,4 +236,4 @@ export declare function isNullOrUndefined(payload: any): payload is null | undef
|
|
230
236
|
* @throws {TypeError} Will throw type error if type is an invalid type
|
231
237
|
* @returns {payload is T}
|
232
238
|
*/
|
233
|
-
export declare function isType<T extends
|
239
|
+
export declare function isType<T extends AnyFunction | AnyClass>(payload: any, type: T): payload is T;
|