is-what 4.1.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/.babelrc +3 -0
- package/.eslintignore +9 -0
- package/.github/FUNDING.yml +12 -0
- package/.prettierrc +9 -0
- package/.vscode/settings.json +9 -0
- package/LICENSE +21 -0
- package/README.md +272 -0
- package/build.js +40 -0
- package/dist/index.js +343 -0
- package/package.json +105 -0
- package/src/index.ts +415 -0
- package/test/ava.ts +419 -0
- package/tsconfig.json +15 -0
- package/types/index.d.ts +267 -0
package/dist/index.js
ADDED
@@ -0,0 +1,343 @@
|
|
1
|
+
/**
|
2
|
+
* Returns the object type of the given payload
|
3
|
+
*
|
4
|
+
* @param {*} payload
|
5
|
+
* @returns {string}
|
6
|
+
*/
|
7
|
+
function getType(payload) {
|
8
|
+
return Object.prototype.toString.call(payload).slice(8, -1);
|
9
|
+
}
|
10
|
+
/**
|
11
|
+
* Returns whether the payload is undefined
|
12
|
+
*
|
13
|
+
* @param {*} payload
|
14
|
+
* @returns {payload is undefined}
|
15
|
+
*/
|
16
|
+
function isUndefined(payload) {
|
17
|
+
return getType(payload) === 'Undefined';
|
18
|
+
}
|
19
|
+
/**
|
20
|
+
* Returns whether the payload is null
|
21
|
+
*
|
22
|
+
* @param {*} payload
|
23
|
+
* @returns {payload is null}
|
24
|
+
*/
|
25
|
+
function isNull(payload) {
|
26
|
+
return getType(payload) === 'Null';
|
27
|
+
}
|
28
|
+
/**
|
29
|
+
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
|
30
|
+
*
|
31
|
+
* @param {*} payload
|
32
|
+
* @returns {payload is PlainObject}
|
33
|
+
*/
|
34
|
+
function isPlainObject(payload) {
|
35
|
+
if (getType(payload) !== 'Object')
|
36
|
+
return false;
|
37
|
+
return payload.constructor === Object && Object.getPrototypeOf(payload) === Object.prototype;
|
38
|
+
}
|
39
|
+
/**
|
40
|
+
* Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
|
41
|
+
*
|
42
|
+
* @param {*} payload
|
43
|
+
* @returns {payload is PlainObject}
|
44
|
+
*/
|
45
|
+
function isObject(payload) {
|
46
|
+
return isPlainObject(payload);
|
47
|
+
}
|
48
|
+
/**
|
49
|
+
* Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)
|
50
|
+
*
|
51
|
+
* @param {*} payload
|
52
|
+
* @returns {payload is { [K in any]: never }}
|
53
|
+
*/
|
54
|
+
function isEmptyObject(payload) {
|
55
|
+
return isPlainObject(payload) && Object.keys(payload).length === 0;
|
56
|
+
}
|
57
|
+
/**
|
58
|
+
* Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)
|
59
|
+
*
|
60
|
+
* @param {*} payload
|
61
|
+
* @returns {payload is PlainObject}
|
62
|
+
*/
|
63
|
+
function isFullObject(payload) {
|
64
|
+
return isPlainObject(payload) && Object.keys(payload).length > 0;
|
65
|
+
}
|
66
|
+
/**
|
67
|
+
* Returns whether the payload is an any kind of object (including special classes or objects with different prototypes)
|
68
|
+
*
|
69
|
+
* @param {*} payload
|
70
|
+
* @returns {payload is PlainObject}
|
71
|
+
*/
|
72
|
+
function isAnyObject(payload) {
|
73
|
+
return getType(payload) === 'Object';
|
74
|
+
}
|
75
|
+
/**
|
76
|
+
* Returns whether the payload is an object like a type passed in < >
|
77
|
+
*
|
78
|
+
* Usage: isObjectLike<{id: any}>(payload) // will make sure it's an object and has an `id` prop.
|
79
|
+
*
|
80
|
+
* @template T this must be passed in < >
|
81
|
+
* @param {*} payload
|
82
|
+
* @returns {payload is T}
|
83
|
+
*/
|
84
|
+
function isObjectLike(payload) {
|
85
|
+
return isAnyObject(payload);
|
86
|
+
}
|
87
|
+
/**
|
88
|
+
* Returns whether the payload is a function (regular or async)
|
89
|
+
*
|
90
|
+
* @param {*} payload
|
91
|
+
* @returns {payload is AnyFunction}
|
92
|
+
*/
|
93
|
+
function isFunction(payload) {
|
94
|
+
return typeof payload === 'function';
|
95
|
+
}
|
96
|
+
/**
|
97
|
+
* Returns whether the payload is an array
|
98
|
+
*
|
99
|
+
* @param {any} payload
|
100
|
+
* @returns {payload is any[]}
|
101
|
+
*/
|
102
|
+
function isArray(payload) {
|
103
|
+
return getType(payload) === 'Array';
|
104
|
+
}
|
105
|
+
/**
|
106
|
+
* Returns whether the payload is a an array with at least 1 item
|
107
|
+
*
|
108
|
+
* @param {*} payload
|
109
|
+
* @returns {payload is any[]}
|
110
|
+
*/
|
111
|
+
function isFullArray(payload) {
|
112
|
+
return isArray(payload) && payload.length > 0;
|
113
|
+
}
|
114
|
+
/**
|
115
|
+
* Returns whether the payload is a an empty array
|
116
|
+
*
|
117
|
+
* @param {*} payload
|
118
|
+
* @returns {payload is []}
|
119
|
+
*/
|
120
|
+
function isEmptyArray(payload) {
|
121
|
+
return isArray(payload) && payload.length === 0;
|
122
|
+
}
|
123
|
+
/**
|
124
|
+
* Returns whether the payload is a string
|
125
|
+
*
|
126
|
+
* @param {*} payload
|
127
|
+
* @returns {payload is string}
|
128
|
+
*/
|
129
|
+
function isString(payload) {
|
130
|
+
return getType(payload) === 'String';
|
131
|
+
}
|
132
|
+
/**
|
133
|
+
* Returns whether the payload is a string, BUT returns false for ''
|
134
|
+
*
|
135
|
+
* @param {*} payload
|
136
|
+
* @returns {payload is string}
|
137
|
+
*/
|
138
|
+
function isFullString(payload) {
|
139
|
+
return isString(payload) && payload !== '';
|
140
|
+
}
|
141
|
+
/**
|
142
|
+
* Returns whether the payload is ''
|
143
|
+
*
|
144
|
+
* @param {*} payload
|
145
|
+
* @returns {payload is string}
|
146
|
+
*/
|
147
|
+
function isEmptyString(payload) {
|
148
|
+
return payload === '';
|
149
|
+
}
|
150
|
+
/**
|
151
|
+
* Returns whether the payload is a number (but not NaN)
|
152
|
+
*
|
153
|
+
* This will return `false` for `NaN`!!
|
154
|
+
*
|
155
|
+
* @param {*} payload
|
156
|
+
* @returns {payload is number}
|
157
|
+
*/
|
158
|
+
function isNumber(payload) {
|
159
|
+
return getType(payload) === 'Number' && !isNaN(payload);
|
160
|
+
}
|
161
|
+
/**
|
162
|
+
* Returns whether the payload is a positive number (but not 0)
|
163
|
+
*
|
164
|
+
* @param {*} payload
|
165
|
+
* @returns {payload is number}
|
166
|
+
*/
|
167
|
+
function isPositiveNumber(payload) {
|
168
|
+
return isNumber(payload) && payload > 0;
|
169
|
+
}
|
170
|
+
/**
|
171
|
+
* Returns whether the payload is a negative number (but not 0)
|
172
|
+
*
|
173
|
+
* @param {*} payload
|
174
|
+
* @returns {payload is number}
|
175
|
+
*/
|
176
|
+
function isNegativeNumber(payload) {
|
177
|
+
return isNumber(payload) && payload < 0;
|
178
|
+
}
|
179
|
+
/**
|
180
|
+
* Returns whether the payload is a boolean
|
181
|
+
*
|
182
|
+
* @param {*} payload
|
183
|
+
* @returns {payload is boolean}
|
184
|
+
*/
|
185
|
+
function isBoolean(payload) {
|
186
|
+
return getType(payload) === 'Boolean';
|
187
|
+
}
|
188
|
+
/**
|
189
|
+
* Returns whether the payload is a regular expression (RegExp)
|
190
|
+
*
|
191
|
+
* @param {*} payload
|
192
|
+
* @returns {payload is RegExp}
|
193
|
+
*/
|
194
|
+
function isRegExp(payload) {
|
195
|
+
return getType(payload) === 'RegExp';
|
196
|
+
}
|
197
|
+
/**
|
198
|
+
* Returns whether the payload is a Map
|
199
|
+
*
|
200
|
+
* @param {*} payload
|
201
|
+
* @returns {payload is Map<any, any>}
|
202
|
+
*/
|
203
|
+
function isMap(payload) {
|
204
|
+
return getType(payload) === 'Map';
|
205
|
+
}
|
206
|
+
/**
|
207
|
+
* Returns whether the payload is a WeakMap
|
208
|
+
*
|
209
|
+
* @param {*} payload
|
210
|
+
* @returns {payload is WeakMap<any, any>}
|
211
|
+
*/
|
212
|
+
function isWeakMap(payload) {
|
213
|
+
return getType(payload) === 'WeakMap';
|
214
|
+
}
|
215
|
+
/**
|
216
|
+
* Returns whether the payload is a Set
|
217
|
+
*
|
218
|
+
* @param {*} payload
|
219
|
+
* @returns {payload is Set<any>}
|
220
|
+
*/
|
221
|
+
function isSet(payload) {
|
222
|
+
return getType(payload) === 'Set';
|
223
|
+
}
|
224
|
+
/**
|
225
|
+
* Returns whether the payload is a WeakSet
|
226
|
+
*
|
227
|
+
* @param {*} payload
|
228
|
+
* @returns {payload is WeakSet<any>}
|
229
|
+
*/
|
230
|
+
function isWeakSet(payload) {
|
231
|
+
return getType(payload) === 'WeakSet';
|
232
|
+
}
|
233
|
+
/**
|
234
|
+
* Returns whether the payload is a Symbol
|
235
|
+
*
|
236
|
+
* @param {*} payload
|
237
|
+
* @returns {payload is symbol}
|
238
|
+
*/
|
239
|
+
function isSymbol(payload) {
|
240
|
+
return getType(payload) === 'Symbol';
|
241
|
+
}
|
242
|
+
/**
|
243
|
+
* Returns whether the payload is a Date, and that the date is valid
|
244
|
+
*
|
245
|
+
* @param {*} payload
|
246
|
+
* @returns {payload is Date}
|
247
|
+
*/
|
248
|
+
function isDate(payload) {
|
249
|
+
return getType(payload) === 'Date' && !isNaN(payload);
|
250
|
+
}
|
251
|
+
/**
|
252
|
+
* Returns whether the payload is a Blob
|
253
|
+
*
|
254
|
+
* @param {*} payload
|
255
|
+
* @returns {payload is Blob}
|
256
|
+
*/
|
257
|
+
function isBlob(payload) {
|
258
|
+
return getType(payload) === 'Blob';
|
259
|
+
}
|
260
|
+
/**
|
261
|
+
* Returns whether the payload is a File
|
262
|
+
*
|
263
|
+
* @param {*} payload
|
264
|
+
* @returns {payload is File}
|
265
|
+
*/
|
266
|
+
function isFile(payload) {
|
267
|
+
return getType(payload) === 'File';
|
268
|
+
}
|
269
|
+
/**
|
270
|
+
* Returns whether the payload is a Promise
|
271
|
+
*
|
272
|
+
* @param {*} payload
|
273
|
+
* @returns {payload is Promise<any>}
|
274
|
+
*/
|
275
|
+
function isPromise(payload) {
|
276
|
+
return getType(payload) === 'Promise';
|
277
|
+
}
|
278
|
+
/**
|
279
|
+
* Returns whether the payload is an Error
|
280
|
+
*
|
281
|
+
* @param {*} payload
|
282
|
+
* @returns {payload is Error}
|
283
|
+
*/
|
284
|
+
function isError(payload) {
|
285
|
+
return getType(payload) === 'Error';
|
286
|
+
}
|
287
|
+
/**
|
288
|
+
* Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`)
|
289
|
+
*
|
290
|
+
* @param {*} payload
|
291
|
+
* @returns {payload is typeof NaN}
|
292
|
+
*/
|
293
|
+
function isNaNValue(payload) {
|
294
|
+
return getType(payload) === 'Number' && isNaN(payload);
|
295
|
+
}
|
296
|
+
/**
|
297
|
+
* Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol)
|
298
|
+
*
|
299
|
+
* @param {*} payload
|
300
|
+
* @returns {(payload is boolean | null | undefined | number | string | symbol)}
|
301
|
+
*/
|
302
|
+
function isPrimitive(payload) {
|
303
|
+
return (isBoolean(payload) ||
|
304
|
+
isNull(payload) ||
|
305
|
+
isUndefined(payload) ||
|
306
|
+
isNumber(payload) ||
|
307
|
+
isString(payload) ||
|
308
|
+
isSymbol(payload));
|
309
|
+
}
|
310
|
+
/**
|
311
|
+
* Returns true whether the payload is null or undefined
|
312
|
+
*
|
313
|
+
* @param {*} payload
|
314
|
+
* @returns {(payload is null | undefined)}
|
315
|
+
*/
|
316
|
+
const isNullOrUndefined = isOneOf(isNull, isUndefined);
|
317
|
+
function isOneOf(a, b, c, d, e) {
|
318
|
+
return (value) => a(value) || b(value) || (!!c && c(value)) || (!!d && d(value)) || (!!e && e(value));
|
319
|
+
}
|
320
|
+
/**
|
321
|
+
* Does a generic check to check that the given payload is of a given type.
|
322
|
+
* In cases like Number, it will return true for NaN as NaN is a Number (thanks javascript!);
|
323
|
+
* It will, however, differentiate between object and null
|
324
|
+
*
|
325
|
+
* @template T
|
326
|
+
* @param {*} payload
|
327
|
+
* @param {T} type
|
328
|
+
* @throws {TypeError} Will throw type error if type is an invalid type
|
329
|
+
* @returns {payload is T}
|
330
|
+
*/
|
331
|
+
function isType(payload, type) {
|
332
|
+
if (!(type instanceof Function)) {
|
333
|
+
throw new TypeError('Type must be a function');
|
334
|
+
}
|
335
|
+
if (!Object.prototype.hasOwnProperty.call(type, 'prototype')) {
|
336
|
+
throw new TypeError('Type is not a class');
|
337
|
+
}
|
338
|
+
// Classes usually have names (as functions usually have names)
|
339
|
+
const name = type.name;
|
340
|
+
return getType(payload) === name || Boolean(payload && payload.constructor === type);
|
341
|
+
}
|
342
|
+
|
343
|
+
export { getType, isAnyObject, isArray, isBlob, isBoolean, isDate, isEmptyArray, isEmptyObject, isEmptyString, isError, isFile, isFullArray, isFullObject, isFullString, isFunction, isMap, isNaNValue, isNegativeNumber, isNull, isNullOrUndefined, isNumber, isObject, isObjectLike, isOneOf, isPlainObject, isPositiveNumber, isPrimitive, isPromise, isRegExp, isSet, isString, isSymbol, isType, isUndefined, isWeakMap, isWeakSet };
|
package/package.json
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
{
|
2
|
+
"name": "is-what",
|
3
|
+
"sideEffects": false,
|
4
|
+
"version": "4.1.0",
|
5
|
+
"description": "JS type check (TypeScript supported) functions like `isPlainObject() isArray()` etc. A simple & small integration.",
|
6
|
+
"type": "module",
|
7
|
+
"exports": "./dist/index.js",
|
8
|
+
"types": "./types/index.d.ts",
|
9
|
+
"scripts": {
|
10
|
+
"test": "ava",
|
11
|
+
"lint": "tsc --noEmit src/index.ts && eslint . --ext .js,.jsx,.ts,.tsx",
|
12
|
+
"rollup": "rollup -c ./build.js",
|
13
|
+
"build": "rimraf types && rimraf dist && npm run lint && npm run rollup && npm run test",
|
14
|
+
"release": "npm run build && np"
|
15
|
+
},
|
16
|
+
"repository": {
|
17
|
+
"type": "git",
|
18
|
+
"url": "git+https://github.com/mesqueeb/is-what.git"
|
19
|
+
},
|
20
|
+
"keywords": [
|
21
|
+
"javascript",
|
22
|
+
"typescript",
|
23
|
+
"typechecker",
|
24
|
+
"check-type",
|
25
|
+
"javascript-type",
|
26
|
+
"primitive-types",
|
27
|
+
"plain-object",
|
28
|
+
"plain-objects",
|
29
|
+
"class-instance",
|
30
|
+
"class-identifier",
|
31
|
+
"type-checking",
|
32
|
+
"type-checker",
|
33
|
+
"type-check",
|
34
|
+
"define-type",
|
35
|
+
"get-type",
|
36
|
+
"what-type",
|
37
|
+
"is-object",
|
38
|
+
"is-plain-obj",
|
39
|
+
"is-plain-object"
|
40
|
+
],
|
41
|
+
"author": "Luca Ban - Mesqueeb",
|
42
|
+
"license": "MIT",
|
43
|
+
"bugs": {
|
44
|
+
"url": "https://github.com/mesqueeb/is-what/issues"
|
45
|
+
},
|
46
|
+
"homepage": "https://github.com/mesqueeb/is-what#readme",
|
47
|
+
"devDependencies": {
|
48
|
+
"@babel/core": "^7.15.8",
|
49
|
+
"@types/babel-core": "^6.25.7",
|
50
|
+
"@typescript-eslint/eslint-plugin": "^5.0.0",
|
51
|
+
"@typescript-eslint/parser": "^5.0.0",
|
52
|
+
"ava": "^3.15.0",
|
53
|
+
"babel-core": "^7.0.0-bridge.0",
|
54
|
+
"babel-preset-env": "^1.7.0",
|
55
|
+
"eslint": "^8.0.1",
|
56
|
+
"eslint-config-prettier": "^8.3.0",
|
57
|
+
"eslint-plugin-tree-shaking": "^1.9.2",
|
58
|
+
"np": "^7.5.0",
|
59
|
+
"prettier": "^2.4.1",
|
60
|
+
"rimraf": "^3.0.2",
|
61
|
+
"rollup": "^2.58.0",
|
62
|
+
"rollup-plugin-typescript2": "^0.30.0",
|
63
|
+
"ts-node": "^10.3.0",
|
64
|
+
"typescript": "^4.4.4"
|
65
|
+
},
|
66
|
+
"engines": {
|
67
|
+
"node": ">=12.13",
|
68
|
+
"npm": ">=7"
|
69
|
+
},
|
70
|
+
"ava": {
|
71
|
+
"extensions": {
|
72
|
+
"ts": "module"
|
73
|
+
},
|
74
|
+
"nonSemVerExperiments": {
|
75
|
+
"configurableModuleFormat": true
|
76
|
+
},
|
77
|
+
"nodeArguments": [
|
78
|
+
"--loader=ts-node/esm"
|
79
|
+
]
|
80
|
+
},
|
81
|
+
"np": {
|
82
|
+
"yarn": false,
|
83
|
+
"branch": "production"
|
84
|
+
},
|
85
|
+
"eslintConfig": {
|
86
|
+
"root": true,
|
87
|
+
"parser": "@typescript-eslint/parser",
|
88
|
+
"plugins": [
|
89
|
+
"@typescript-eslint",
|
90
|
+
"tree-shaking"
|
91
|
+
],
|
92
|
+
"extends": [
|
93
|
+
"eslint:recommended",
|
94
|
+
"plugin:@typescript-eslint/eslint-recommended",
|
95
|
+
"plugin:@typescript-eslint/recommended",
|
96
|
+
"prettier"
|
97
|
+
],
|
98
|
+
"rules": {
|
99
|
+
"@typescript-eslint/no-explicit-any": "off",
|
100
|
+
"@typescript-eslint/ban-ts-ignore": "off",
|
101
|
+
"tree-shaking/no-side-effects-in-initialization": "error",
|
102
|
+
"@typescript-eslint/explicit-module-boundary-types": "off"
|
103
|
+
}
|
104
|
+
}
|
105
|
+
}
|