is-what 3.5.1 → 3.8.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/.eslintrc.js +1 -0
- package/dist/index.cjs.js +64 -4
- package/dist/index.esm.js +59 -5
- package/package.json +24 -13
- package/src/index.ts +64 -4
- package/test/ava.ts +207 -0
- package/test/index.test.js +3 -0
- package/types/index.d.ts +46 -4
package/.eslintrc.js
CHANGED
package/dist/index.cjs.js
CHANGED
@@ -136,7 +136,7 @@ function isBoolean(payload) {
|
|
136
136
|
return getType(payload) === 'Boolean';
|
137
137
|
}
|
138
138
|
/**
|
139
|
-
* Returns whether the payload is a regular expression
|
139
|
+
* Returns whether the payload is a regular expression (RegExp)
|
140
140
|
*
|
141
141
|
* @param {*} payload
|
142
142
|
* @returns {payload is RegExp}
|
@@ -144,6 +144,42 @@ function isBoolean(payload) {
|
|
144
144
|
function isRegExp(payload) {
|
145
145
|
return getType(payload) === 'RegExp';
|
146
146
|
}
|
147
|
+
/**
|
148
|
+
* Returns whether the payload is a Map
|
149
|
+
*
|
150
|
+
* @param {*} payload
|
151
|
+
* @returns {payload is Map}
|
152
|
+
*/
|
153
|
+
function isMap(payload) {
|
154
|
+
return getType(payload) === 'Map';
|
155
|
+
}
|
156
|
+
/**
|
157
|
+
* Returns whether the payload is a WeakMap
|
158
|
+
*
|
159
|
+
* @param {*} payload
|
160
|
+
* @returns {payload is WeakMap}
|
161
|
+
*/
|
162
|
+
function isWeakMap(payload) {
|
163
|
+
return getType(payload) === 'WeakMap';
|
164
|
+
}
|
165
|
+
/**
|
166
|
+
* Returns whether the payload is a Set
|
167
|
+
*
|
168
|
+
* @param {*} payload
|
169
|
+
* @returns {payload is Set}
|
170
|
+
*/
|
171
|
+
function isSet(payload) {
|
172
|
+
return getType(payload) === 'Set';
|
173
|
+
}
|
174
|
+
/**
|
175
|
+
* Returns whether the payload is a WeakSet
|
176
|
+
*
|
177
|
+
* @param {*} payload
|
178
|
+
* @returns {payload is WeakSet}
|
179
|
+
*/
|
180
|
+
function isWeakSet(payload) {
|
181
|
+
return getType(payload) === 'WeakSet';
|
182
|
+
}
|
147
183
|
/**
|
148
184
|
* Returns whether the payload is a Symbol
|
149
185
|
*
|
@@ -154,7 +190,7 @@ function isSymbol(payload) {
|
|
154
190
|
return getType(payload) === 'Symbol';
|
155
191
|
}
|
156
192
|
/**
|
157
|
-
* Returns whether the payload is a
|
193
|
+
* Returns whether the payload is a Date, and that the date is valid
|
158
194
|
*
|
159
195
|
* @param {*} payload
|
160
196
|
* @returns {payload is Date}
|
@@ -163,7 +199,7 @@ function isDate(payload) {
|
|
163
199
|
return getType(payload) === 'Date' && !isNaN(payload);
|
164
200
|
}
|
165
201
|
/**
|
166
|
-
* Returns whether the payload is a
|
202
|
+
* Returns whether the payload is a Blob
|
167
203
|
*
|
168
204
|
* @param {*} payload
|
169
205
|
* @returns {payload is Blob}
|
@@ -172,7 +208,7 @@ function isBlob(payload) {
|
|
172
208
|
return getType(payload) === 'Blob';
|
173
209
|
}
|
174
210
|
/**
|
175
|
-
* Returns whether the payload is a
|
211
|
+
* Returns whether the payload is a File
|
176
212
|
*
|
177
213
|
* @param {*} payload
|
178
214
|
* @returns {payload is File}
|
@@ -180,6 +216,24 @@ function isBlob(payload) {
|
|
180
216
|
function isFile(payload) {
|
181
217
|
return getType(payload) === 'File';
|
182
218
|
}
|
219
|
+
/**
|
220
|
+
* Returns whether the payload is a Promise
|
221
|
+
*
|
222
|
+
* @param {*} payload
|
223
|
+
* @returns {payload is Promise}
|
224
|
+
*/
|
225
|
+
function isPromise(payload) {
|
226
|
+
return getType(payload) === 'Promise';
|
227
|
+
}
|
228
|
+
/**
|
229
|
+
* Returns whether the payload is an Error
|
230
|
+
*
|
231
|
+
* @param {*} payload
|
232
|
+
* @returns {payload is Error}
|
233
|
+
*/
|
234
|
+
function isError(payload) {
|
235
|
+
return getType(payload) === 'Error';
|
236
|
+
}
|
183
237
|
/**
|
184
238
|
* Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol)
|
185
239
|
*
|
@@ -233,9 +287,11 @@ exports.isBlob = isBlob;
|
|
233
287
|
exports.isBoolean = isBoolean;
|
234
288
|
exports.isDate = isDate;
|
235
289
|
exports.isEmptyString = isEmptyString;
|
290
|
+
exports.isError = isError;
|
236
291
|
exports.isFile = isFile;
|
237
292
|
exports.isFullString = isFullString;
|
238
293
|
exports.isFunction = isFunction;
|
294
|
+
exports.isMap = isMap;
|
239
295
|
exports.isNull = isNull;
|
240
296
|
exports.isNullOrUndefined = isNullOrUndefined;
|
241
297
|
exports.isNumber = isNumber;
|
@@ -243,8 +299,12 @@ exports.isObject = isObject;
|
|
243
299
|
exports.isObjectLike = isObjectLike;
|
244
300
|
exports.isPlainObject = isPlainObject;
|
245
301
|
exports.isPrimitive = isPrimitive;
|
302
|
+
exports.isPromise = isPromise;
|
246
303
|
exports.isRegExp = isRegExp;
|
304
|
+
exports.isSet = isSet;
|
247
305
|
exports.isString = isString;
|
248
306
|
exports.isSymbol = isSymbol;
|
249
307
|
exports.isType = isType;
|
250
308
|
exports.isUndefined = isUndefined;
|
309
|
+
exports.isWeakMap = isWeakMap;
|
310
|
+
exports.isWeakSet = isWeakSet;
|
package/dist/index.esm.js
CHANGED
@@ -132,7 +132,7 @@ function isBoolean(payload) {
|
|
132
132
|
return getType(payload) === 'Boolean';
|
133
133
|
}
|
134
134
|
/**
|
135
|
-
* Returns whether the payload is a regular expression
|
135
|
+
* Returns whether the payload is a regular expression (RegExp)
|
136
136
|
*
|
137
137
|
* @param {*} payload
|
138
138
|
* @returns {payload is RegExp}
|
@@ -140,6 +140,42 @@ function isBoolean(payload) {
|
|
140
140
|
function isRegExp(payload) {
|
141
141
|
return getType(payload) === 'RegExp';
|
142
142
|
}
|
143
|
+
/**
|
144
|
+
* Returns whether the payload is a Map
|
145
|
+
*
|
146
|
+
* @param {*} payload
|
147
|
+
* @returns {payload is Map}
|
148
|
+
*/
|
149
|
+
function isMap(payload) {
|
150
|
+
return getType(payload) === 'Map';
|
151
|
+
}
|
152
|
+
/**
|
153
|
+
* Returns whether the payload is a WeakMap
|
154
|
+
*
|
155
|
+
* @param {*} payload
|
156
|
+
* @returns {payload is WeakMap}
|
157
|
+
*/
|
158
|
+
function isWeakMap(payload) {
|
159
|
+
return getType(payload) === 'WeakMap';
|
160
|
+
}
|
161
|
+
/**
|
162
|
+
* Returns whether the payload is a Set
|
163
|
+
*
|
164
|
+
* @param {*} payload
|
165
|
+
* @returns {payload is Set}
|
166
|
+
*/
|
167
|
+
function isSet(payload) {
|
168
|
+
return getType(payload) === 'Set';
|
169
|
+
}
|
170
|
+
/**
|
171
|
+
* Returns whether the payload is a WeakSet
|
172
|
+
*
|
173
|
+
* @param {*} payload
|
174
|
+
* @returns {payload is WeakSet}
|
175
|
+
*/
|
176
|
+
function isWeakSet(payload) {
|
177
|
+
return getType(payload) === 'WeakSet';
|
178
|
+
}
|
143
179
|
/**
|
144
180
|
* Returns whether the payload is a Symbol
|
145
181
|
*
|
@@ -150,7 +186,7 @@ function isSymbol(payload) {
|
|
150
186
|
return getType(payload) === 'Symbol';
|
151
187
|
}
|
152
188
|
/**
|
153
|
-
* Returns whether the payload is a
|
189
|
+
* Returns whether the payload is a Date, and that the date is valid
|
154
190
|
*
|
155
191
|
* @param {*} payload
|
156
192
|
* @returns {payload is Date}
|
@@ -159,7 +195,7 @@ function isDate(payload) {
|
|
159
195
|
return getType(payload) === 'Date' && !isNaN(payload);
|
160
196
|
}
|
161
197
|
/**
|
162
|
-
* Returns whether the payload is a
|
198
|
+
* Returns whether the payload is a Blob
|
163
199
|
*
|
164
200
|
* @param {*} payload
|
165
201
|
* @returns {payload is Blob}
|
@@ -168,7 +204,7 @@ function isBlob(payload) {
|
|
168
204
|
return getType(payload) === 'Blob';
|
169
205
|
}
|
170
206
|
/**
|
171
|
-
* Returns whether the payload is a
|
207
|
+
* Returns whether the payload is a File
|
172
208
|
*
|
173
209
|
* @param {*} payload
|
174
210
|
* @returns {payload is File}
|
@@ -176,6 +212,24 @@ function isBlob(payload) {
|
|
176
212
|
function isFile(payload) {
|
177
213
|
return getType(payload) === 'File';
|
178
214
|
}
|
215
|
+
/**
|
216
|
+
* Returns whether the payload is a Promise
|
217
|
+
*
|
218
|
+
* @param {*} payload
|
219
|
+
* @returns {payload is Promise}
|
220
|
+
*/
|
221
|
+
function isPromise(payload) {
|
222
|
+
return getType(payload) === 'Promise';
|
223
|
+
}
|
224
|
+
/**
|
225
|
+
* Returns whether the payload is an Error
|
226
|
+
*
|
227
|
+
* @param {*} payload
|
228
|
+
* @returns {payload is Error}
|
229
|
+
*/
|
230
|
+
function isError(payload) {
|
231
|
+
return getType(payload) === 'Error';
|
232
|
+
}
|
179
233
|
/**
|
180
234
|
* Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol)
|
181
235
|
*
|
@@ -222,4 +276,4 @@ function isType(payload, type) {
|
|
222
276
|
return getType(payload) === name || Boolean(payload && payload.constructor === type);
|
223
277
|
}
|
224
278
|
|
225
|
-
export { getType, isAnyObject, isArray, isBlob, isBoolean, isDate, isEmptyString, isFile, isFullString, isFunction, isNull, isNullOrUndefined, isNumber, isObject, isObjectLike, isPlainObject, isPrimitive, isRegExp, isString, isSymbol, isType, isUndefined };
|
279
|
+
export { getType, isAnyObject, isArray, isBlob, isBoolean, isDate, isEmptyString, isError, isFile, isFullString, isFunction, isMap, isNull, isNullOrUndefined, isNumber, isObject, isObjectLike, isPlainObject, isPrimitive, isPromise, isRegExp, isSet, isString, isSymbol, isType, isUndefined, isWeakMap, isWeakSet };
|
package/package.json
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
{
|
2
2
|
"name": "is-what",
|
3
3
|
"sideEffects": false,
|
4
|
-
"version": "3.
|
4
|
+
"version": "3.8.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
|
+
"ava": "ava",
|
10
11
|
"test": "jest",
|
11
12
|
"test-w": "jest --watchAll",
|
12
13
|
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
|
@@ -44,23 +45,33 @@
|
|
44
45
|
"url": "https://github.com/mesqueeb/is-what/issues"
|
45
46
|
},
|
46
47
|
"homepage": "https://github.com/mesqueeb/is-what#readme",
|
48
|
+
"dependencies": {},
|
47
49
|
"devDependencies": {
|
48
|
-
"@babel/core": "^7.
|
50
|
+
"@babel/core": "^7.9.0",
|
49
51
|
"@types/babel-core": "^6.25.6",
|
50
|
-
"@types/jest": "^
|
51
|
-
"@typescript-eslint/eslint-plugin": "^2.
|
52
|
-
"@typescript-eslint/parser": "^2.
|
52
|
+
"@types/jest": "^25.2.1",
|
53
|
+
"@typescript-eslint/eslint-plugin": "^2.27.0",
|
54
|
+
"@typescript-eslint/parser": "^2.27.0",
|
55
|
+
"ava": "^3.6.0",
|
53
56
|
"babel-core": "^7.0.0-bridge.0",
|
54
|
-
"babel-jest": "^
|
57
|
+
"babel-jest": "^25.3.0",
|
55
58
|
"babel-preset-env": "^1.7.0",
|
56
59
|
"eslint": "^6.8.0",
|
57
|
-
"eslint-config-prettier": "^6.
|
60
|
+
"eslint-config-prettier": "^6.10.1",
|
58
61
|
"eslint-plugin-tree-shaking": "^1.8.0",
|
59
|
-
"jest": "^
|
60
|
-
"regenerator-runtime": "^0.13.
|
61
|
-
"rollup": "^1.
|
62
|
-
"rollup-plugin-typescript2": "^0.
|
63
|
-
"
|
62
|
+
"jest": "^25.3.0",
|
63
|
+
"regenerator-runtime": "^0.13.5",
|
64
|
+
"rollup": "^1.32.1",
|
65
|
+
"rollup-plugin-typescript2": "^0.26.0",
|
66
|
+
"ts-node": "^8.8.2",
|
67
|
+
"typescript": "^3.8.3"
|
64
68
|
},
|
65
|
-
"
|
69
|
+
"ava": {
|
70
|
+
"extensions": [
|
71
|
+
"ts"
|
72
|
+
],
|
73
|
+
"require": [
|
74
|
+
"ts-node/register"
|
75
|
+
]
|
76
|
+
}
|
66
77
|
}
|
package/src/index.ts
CHANGED
@@ -145,7 +145,7 @@ export function isBoolean (payload: any): payload is boolean {
|
|
145
145
|
}
|
146
146
|
|
147
147
|
/**
|
148
|
-
* Returns whether the payload is a regular expression
|
148
|
+
* Returns whether the payload is a regular expression (RegExp)
|
149
149
|
*
|
150
150
|
* @param {*} payload
|
151
151
|
* @returns {payload is RegExp}
|
@@ -154,6 +154,46 @@ export function isRegExp (payload: any): payload is RegExp {
|
|
154
154
|
return getType(payload) === 'RegExp'
|
155
155
|
}
|
156
156
|
|
157
|
+
/**
|
158
|
+
* Returns whether the payload is a Map
|
159
|
+
*
|
160
|
+
* @param {*} payload
|
161
|
+
* @returns {payload is Map}
|
162
|
+
*/
|
163
|
+
export function isMap (payload: any): payload is Map<any, any> {
|
164
|
+
return getType(payload) === 'Map'
|
165
|
+
}
|
166
|
+
|
167
|
+
/**
|
168
|
+
* Returns whether the payload is a WeakMap
|
169
|
+
*
|
170
|
+
* @param {*} payload
|
171
|
+
* @returns {payload is WeakMap}
|
172
|
+
*/
|
173
|
+
export function isWeakMap (payload: any): payload is WeakMap<any, any> {
|
174
|
+
return getType(payload) === 'WeakMap'
|
175
|
+
}
|
176
|
+
|
177
|
+
/**
|
178
|
+
* Returns whether the payload is a Set
|
179
|
+
*
|
180
|
+
* @param {*} payload
|
181
|
+
* @returns {payload is Set}
|
182
|
+
*/
|
183
|
+
export function isSet (payload: any): payload is Set<any> {
|
184
|
+
return getType(payload) === 'Set'
|
185
|
+
}
|
186
|
+
|
187
|
+
/**
|
188
|
+
* Returns whether the payload is a WeakSet
|
189
|
+
*
|
190
|
+
* @param {*} payload
|
191
|
+
* @returns {payload is WeakSet}
|
192
|
+
*/
|
193
|
+
export function isWeakSet (payload: any): payload is WeakSet<any> {
|
194
|
+
return getType(payload) === 'WeakSet'
|
195
|
+
}
|
196
|
+
|
157
197
|
/**
|
158
198
|
* Returns whether the payload is a Symbol
|
159
199
|
*
|
@@ -165,7 +205,7 @@ export function isSymbol (payload: any): payload is symbol {
|
|
165
205
|
}
|
166
206
|
|
167
207
|
/**
|
168
|
-
* Returns whether the payload is a
|
208
|
+
* Returns whether the payload is a Date, and that the date is valid
|
169
209
|
*
|
170
210
|
* @param {*} payload
|
171
211
|
* @returns {payload is Date}
|
@@ -175,7 +215,7 @@ export function isDate (payload: any): payload is Date {
|
|
175
215
|
}
|
176
216
|
|
177
217
|
/**
|
178
|
-
* Returns whether the payload is a
|
218
|
+
* Returns whether the payload is a Blob
|
179
219
|
*
|
180
220
|
* @param {*} payload
|
181
221
|
* @returns {payload is Blob}
|
@@ -185,7 +225,7 @@ export function isBlob (payload: any): payload is Blob {
|
|
185
225
|
}
|
186
226
|
|
187
227
|
/**
|
188
|
-
* Returns whether the payload is a
|
228
|
+
* Returns whether the payload is a File
|
189
229
|
*
|
190
230
|
* @param {*} payload
|
191
231
|
* @returns {payload is File}
|
@@ -194,6 +234,26 @@ export function isFile (payload: any): payload is File {
|
|
194
234
|
return getType(payload) === 'File'
|
195
235
|
}
|
196
236
|
|
237
|
+
/**
|
238
|
+
* Returns whether the payload is a Promise
|
239
|
+
*
|
240
|
+
* @param {*} payload
|
241
|
+
* @returns {payload is Promise}
|
242
|
+
*/
|
243
|
+
export function isPromise (payload: any): payload is Promise<any> {
|
244
|
+
return getType(payload) === 'Promise'
|
245
|
+
}
|
246
|
+
|
247
|
+
/**
|
248
|
+
* Returns whether the payload is an Error
|
249
|
+
*
|
250
|
+
* @param {*} payload
|
251
|
+
* @returns {payload is Error}
|
252
|
+
*/
|
253
|
+
export function isError (payload: any): payload is Error {
|
254
|
+
return getType(payload) === 'Error'
|
255
|
+
}
|
256
|
+
|
197
257
|
/**
|
198
258
|
* Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol)
|
199
259
|
*
|
package/test/ava.ts
ADDED
@@ -0,0 +1,207 @@
|
|
1
|
+
import test from 'ava'
|
2
|
+
import {
|
3
|
+
isError,
|
4
|
+
isObject,
|
5
|
+
isPlainObject,
|
6
|
+
isAnyObject,
|
7
|
+
isUndefined,
|
8
|
+
isNull,
|
9
|
+
isNullOrUndefined,
|
10
|
+
isFunction,
|
11
|
+
isArray,
|
12
|
+
isString,
|
13
|
+
isEmptyString,
|
14
|
+
isFullString,
|
15
|
+
isBoolean,
|
16
|
+
isRegExp,
|
17
|
+
isNumber,
|
18
|
+
isDate,
|
19
|
+
isSymbol,
|
20
|
+
isPrimitive,
|
21
|
+
isType,
|
22
|
+
isMap,
|
23
|
+
isWeakMap,
|
24
|
+
isSet,
|
25
|
+
isWeakSet,
|
26
|
+
// isBlob,
|
27
|
+
// isFile,
|
28
|
+
isPromise,
|
29
|
+
} from '../src/index'
|
30
|
+
|
31
|
+
// const blob = Buffer.from([])
|
32
|
+
|
33
|
+
test('Basic true tests', t => {
|
34
|
+
t.is(isError(new Error('')), true)
|
35
|
+
t.is(isUndefined(undefined), true)
|
36
|
+
t.is(isNull(null), true)
|
37
|
+
t.is(isNullOrUndefined(null), true)
|
38
|
+
t.is(isNullOrUndefined(undefined), true)
|
39
|
+
t.is(isObject({}), true)
|
40
|
+
t.is(isObject(new Object()), true)
|
41
|
+
t.is(
|
42
|
+
isFunction(_ => {}),
|
43
|
+
true
|
44
|
+
)
|
45
|
+
t.is(isArray([]), true)
|
46
|
+
t.is(isArray(new Array()), true)
|
47
|
+
t.is(isString(''), true)
|
48
|
+
t.is(isString('_'), true)
|
49
|
+
t.is(isEmptyString(''), true)
|
50
|
+
t.is(isFullString(' '), true)
|
51
|
+
t.is(isBoolean(true), true)
|
52
|
+
t.is(isBoolean(false), true)
|
53
|
+
t.is(isRegExp(/./), true)
|
54
|
+
t.is(isRegExp(/./gi), true)
|
55
|
+
t.is(isNumber(0), true)
|
56
|
+
t.is(isNumber(1), true)
|
57
|
+
t.is(isDate(new Date()), true)
|
58
|
+
t.is(isSymbol(Symbol()), true)
|
59
|
+
t.is(isMap(new Map()), true)
|
60
|
+
t.is(isWeakMap(new WeakMap()), true)
|
61
|
+
t.is(isSet(new Set()), true)
|
62
|
+
t.is(isWeakSet(new WeakSet()), true)
|
63
|
+
// t.is(isBlob(blob), true)
|
64
|
+
// t.is(isFile(new File([''], '', { type: 'text/html' })), true)
|
65
|
+
t.is(isPromise(new Promise((resolve, reject) => {})), true)
|
66
|
+
})
|
67
|
+
|
68
|
+
test('Basic false tests', t => {
|
69
|
+
t.is(isError({}), false)
|
70
|
+
t.is(isNumber(NaN), false)
|
71
|
+
t.is(isDate(new Date('_')), false)
|
72
|
+
t.is(isDate(NaN), false)
|
73
|
+
t.is(isUndefined(NaN), false)
|
74
|
+
t.is(isNull(NaN), false)
|
75
|
+
t.is(isObject(NaN), false)
|
76
|
+
t.is(isFunction(NaN), false)
|
77
|
+
t.is(isArray(NaN), false)
|
78
|
+
t.is(isString(NaN), false)
|
79
|
+
t.is(isEmptyString(' '), false)
|
80
|
+
t.is(isFullString(''), false)
|
81
|
+
t.is(isBoolean(NaN), false)
|
82
|
+
t.is(isRegExp(NaN), false)
|
83
|
+
t.is(isSymbol(NaN), false)
|
84
|
+
t.is(isMap(new WeakMap()), false)
|
85
|
+
t.is(isWeakMap(new Map()), false)
|
86
|
+
t.is(isSet(new WeakSet()), false)
|
87
|
+
t.is(isWeakSet(new Set()), false)
|
88
|
+
t.is(isNullOrUndefined(NaN), false)
|
89
|
+
})
|
90
|
+
|
91
|
+
test('Primitive tests', t => {
|
92
|
+
// true
|
93
|
+
t.is(isPrimitive(0), true)
|
94
|
+
t.is(isPrimitive(''), true)
|
95
|
+
t.is(isPrimitive('str'), true)
|
96
|
+
t.is(isPrimitive(Symbol()), true)
|
97
|
+
t.is(isPrimitive(true), true)
|
98
|
+
t.is(isPrimitive(false), true)
|
99
|
+
t.is(isPrimitive(null), true)
|
100
|
+
t.is(isPrimitive(undefined), true)
|
101
|
+
// false
|
102
|
+
t.is(isPrimitive(NaN), false)
|
103
|
+
t.is(isPrimitive([]), false)
|
104
|
+
t.is(isPrimitive(new Array()), false)
|
105
|
+
t.is(isPrimitive({}), false)
|
106
|
+
t.is(isPrimitive(new Object()), false)
|
107
|
+
t.is(isPrimitive(new Date()), false)
|
108
|
+
t.is(
|
109
|
+
isPrimitive(_ => {}),
|
110
|
+
false
|
111
|
+
)
|
112
|
+
})
|
113
|
+
|
114
|
+
test('Date exception', t => {
|
115
|
+
t.is(isDate(new Date('_')), false)
|
116
|
+
})
|
117
|
+
|
118
|
+
test('Generic isType', t => {
|
119
|
+
function MyClass () {}
|
120
|
+
// This is correct old fashion syntax for classes, if this is missing
|
121
|
+
MyClass.prototype.constructor = MyClass
|
122
|
+
class MyOtherClass {}
|
123
|
+
const myClass = new MyClass()
|
124
|
+
// this is expected behaviour
|
125
|
+
t.is(isType('', String), true)
|
126
|
+
t.is(isType('_', String), true)
|
127
|
+
t.is(isType('Hello World', String), true)
|
128
|
+
t.is(isType(NaN, Number), true)
|
129
|
+
t.is(isType(0, Number), true)
|
130
|
+
t.is(isType(1, Number), true)
|
131
|
+
t.is(isType({}, Object), true)
|
132
|
+
t.is(isType(new Object(), Object), true)
|
133
|
+
t.is(isType([], Array), true)
|
134
|
+
t.is(isType(new Array(), Array), true)
|
135
|
+
t.is(
|
136
|
+
isType(_ => {}, Function),
|
137
|
+
true
|
138
|
+
)
|
139
|
+
t.is(isType(true, Boolean), true)
|
140
|
+
t.is(isType(false, Boolean), true)
|
141
|
+
t.is(isType(new Date('_'), Date), true)
|
142
|
+
t.is(isType(new Date(), Date), true)
|
143
|
+
t.is(isType(/./, RegExp), true)
|
144
|
+
t.is(isType(/./gi, RegExp), true)
|
145
|
+
t.is(isType(myClass, MyClass), true)
|
146
|
+
t.is(isType(new MyOtherClass(), MyOtherClass), true)
|
147
|
+
t.is(isType(myClass, MyOtherClass), false)
|
148
|
+
t.is(isType(Symbol(), Symbol), true)
|
149
|
+
// t.is(isType(null, Null), true)
|
150
|
+
// t.is(isType(undefined, Undefined), true)
|
151
|
+
// It SHOULD fail
|
152
|
+
t.is(isType(5, String), false)
|
153
|
+
t.is(isType(null, Object), false)
|
154
|
+
// Not sure if this would be the expected behaviour but everything is an object
|
155
|
+
// so I would say so
|
156
|
+
t.is(isType(myClass, Object), true)
|
157
|
+
})
|
158
|
+
|
159
|
+
test('isObject vs isAnyObject', t => {
|
160
|
+
function MyClass () {}
|
161
|
+
// This is correct old fashion syntax for classes, if this is missing
|
162
|
+
MyClass.prototype.constructor = MyClass
|
163
|
+
const myClass = new MyClass()
|
164
|
+
class MyClass2 {}
|
165
|
+
const myClass2 = new MyClass()
|
166
|
+
const mySpecialObject = {}
|
167
|
+
Object.setPrototypeOf(mySpecialObject, {
|
168
|
+
toDate: function () {
|
169
|
+
return new Date()
|
170
|
+
},
|
171
|
+
})
|
172
|
+
// IS OBJECT
|
173
|
+
// plain object
|
174
|
+
t.is(isObject({}), true)
|
175
|
+
t.is(isObject(new Object()), true)
|
176
|
+
t.is(isPlainObject({}), true)
|
177
|
+
t.is(isPlainObject(new Object()), true)
|
178
|
+
// classes & prototypes
|
179
|
+
t.is(isObject(myClass), false)
|
180
|
+
t.is(isObject(myClass2), false)
|
181
|
+
t.is(isObject(mySpecialObject), false)
|
182
|
+
t.is(isPlainObject(myClass), false)
|
183
|
+
t.is(isPlainObject(myClass2), false)
|
184
|
+
t.is(isPlainObject(mySpecialObject), false)
|
185
|
+
// arrays and dates
|
186
|
+
t.is(isObject([]), false)
|
187
|
+
t.is(isObject(new Array()), false)
|
188
|
+
t.is(isObject(new Date('_')), false)
|
189
|
+
t.is(isObject(new Date()), false)
|
190
|
+
t.is(isPlainObject([]), false)
|
191
|
+
t.is(isPlainObject(new Array()), false)
|
192
|
+
t.is(isPlainObject(new Date('_')), false)
|
193
|
+
t.is(isPlainObject(new Date()), false)
|
194
|
+
// IS ANY OBJECT
|
195
|
+
// plain object
|
196
|
+
t.is(isAnyObject({}), true)
|
197
|
+
t.is(isAnyObject(new Object()), true)
|
198
|
+
// classes & prototypes
|
199
|
+
t.is(isAnyObject(myClass), true)
|
200
|
+
t.is(isAnyObject(myClass2), true)
|
201
|
+
t.is(isAnyObject(mySpecialObject), true)
|
202
|
+
// arrays and dates
|
203
|
+
t.is(isAnyObject([]), false)
|
204
|
+
t.is(isAnyObject(new Array()), false)
|
205
|
+
t.is(isAnyObject(new Date('_')), false)
|
206
|
+
t.is(isAnyObject(new Date()), false)
|
207
|
+
})
|
package/test/index.test.js
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
// @ts-check
|
1
2
|
import {
|
2
3
|
isObject,
|
3
4
|
isPlainObject,
|
@@ -19,6 +20,7 @@ import {
|
|
19
20
|
isType,
|
20
21
|
isBlob,
|
21
22
|
isFile,
|
23
|
+
isPromise,
|
22
24
|
} from '../dist/index.cjs'
|
23
25
|
|
24
26
|
test('Basic true tests', () => {
|
@@ -45,6 +47,7 @@ test('Basic true tests', () => {
|
|
45
47
|
expect(isSymbol(Symbol())).toBe(true)
|
46
48
|
expect(isBlob(new Blob())).toBe(true)
|
47
49
|
expect(isFile(new File([''], '', { type: 'text/html' }))).toBe(true)
|
50
|
+
expect(isPromise(new Promise((resolve, reject) => {}))).toBe(true)
|
48
51
|
})
|
49
52
|
|
50
53
|
test('Basic false tests', () => {
|
package/types/index.d.ts
CHANGED
@@ -108,12 +108,40 @@ export declare function isNumber(payload: any): payload is number;
|
|
108
108
|
*/
|
109
109
|
export declare function isBoolean(payload: any): payload is boolean;
|
110
110
|
/**
|
111
|
-
* Returns whether the payload is a regular expression
|
111
|
+
* Returns whether the payload is a regular expression (RegExp)
|
112
112
|
*
|
113
113
|
* @param {*} payload
|
114
114
|
* @returns {payload is RegExp}
|
115
115
|
*/
|
116
116
|
export declare function isRegExp(payload: any): payload is RegExp;
|
117
|
+
/**
|
118
|
+
* Returns whether the payload is a Map
|
119
|
+
*
|
120
|
+
* @param {*} payload
|
121
|
+
* @returns {payload is Map}
|
122
|
+
*/
|
123
|
+
export declare function isMap(payload: any): payload is Map<any, any>;
|
124
|
+
/**
|
125
|
+
* Returns whether the payload is a WeakMap
|
126
|
+
*
|
127
|
+
* @param {*} payload
|
128
|
+
* @returns {payload is WeakMap}
|
129
|
+
*/
|
130
|
+
export declare function isWeakMap(payload: any): payload is WeakMap<any, any>;
|
131
|
+
/**
|
132
|
+
* Returns whether the payload is a Set
|
133
|
+
*
|
134
|
+
* @param {*} payload
|
135
|
+
* @returns {payload is Set}
|
136
|
+
*/
|
137
|
+
export declare function isSet(payload: any): payload is Set<any>;
|
138
|
+
/**
|
139
|
+
* Returns whether the payload is a WeakSet
|
140
|
+
*
|
141
|
+
* @param {*} payload
|
142
|
+
* @returns {payload is WeakSet}
|
143
|
+
*/
|
144
|
+
export declare function isWeakSet(payload: any): payload is WeakSet<any>;
|
117
145
|
/**
|
118
146
|
* Returns whether the payload is a Symbol
|
119
147
|
*
|
@@ -122,26 +150,40 @@ export declare function isRegExp(payload: any): payload is RegExp;
|
|
122
150
|
*/
|
123
151
|
export declare function isSymbol(payload: any): payload is symbol;
|
124
152
|
/**
|
125
|
-
* Returns whether the payload is a
|
153
|
+
* Returns whether the payload is a Date, and that the date is valid
|
126
154
|
*
|
127
155
|
* @param {*} payload
|
128
156
|
* @returns {payload is Date}
|
129
157
|
*/
|
130
158
|
export declare function isDate(payload: any): payload is Date;
|
131
159
|
/**
|
132
|
-
* Returns whether the payload is a
|
160
|
+
* Returns whether the payload is a Blob
|
133
161
|
*
|
134
162
|
* @param {*} payload
|
135
163
|
* @returns {payload is Blob}
|
136
164
|
*/
|
137
165
|
export declare function isBlob(payload: any): payload is Blob;
|
138
166
|
/**
|
139
|
-
* Returns whether the payload is a
|
167
|
+
* Returns whether the payload is a File
|
140
168
|
*
|
141
169
|
* @param {*} payload
|
142
170
|
* @returns {payload is File}
|
143
171
|
*/
|
144
172
|
export declare function isFile(payload: any): payload is File;
|
173
|
+
/**
|
174
|
+
* Returns whether the payload is a Promise
|
175
|
+
*
|
176
|
+
* @param {*} payload
|
177
|
+
* @returns {payload is Promise}
|
178
|
+
*/
|
179
|
+
export declare function isPromise(payload: any): payload is Promise<any>;
|
180
|
+
/**
|
181
|
+
* Returns whether the payload is an Error
|
182
|
+
*
|
183
|
+
* @param {*} payload
|
184
|
+
* @returns {payload is Error}
|
185
|
+
*/
|
186
|
+
export declare function isError(payload: any): payload is Error;
|
145
187
|
/**
|
146
188
|
* Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol)
|
147
189
|
*
|