[6a3a178] | 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 boolean
|
---|
| 163 | *
|
---|
| 164 | * @param {*} payload
|
---|
| 165 | * @returns {payload is boolean}
|
---|
| 166 | */
|
---|
| 167 | function isBoolean(payload) {
|
---|
| 168 | return getType(payload) === 'Boolean';
|
---|
| 169 | }
|
---|
| 170 | /**
|
---|
| 171 | * Returns whether the payload is a regular expression (RegExp)
|
---|
| 172 | *
|
---|
| 173 | * @param {*} payload
|
---|
| 174 | * @returns {payload is RegExp}
|
---|
| 175 | */
|
---|
| 176 | function isRegExp(payload) {
|
---|
| 177 | return getType(payload) === 'RegExp';
|
---|
| 178 | }
|
---|
| 179 | /**
|
---|
| 180 | * Returns whether the payload is a Map
|
---|
| 181 | *
|
---|
| 182 | * @param {*} payload
|
---|
| 183 | * @returns {payload is Map<any, any>}
|
---|
| 184 | */
|
---|
| 185 | function isMap(payload) {
|
---|
| 186 | return getType(payload) === 'Map';
|
---|
| 187 | }
|
---|
| 188 | /**
|
---|
| 189 | * Returns whether the payload is a WeakMap
|
---|
| 190 | *
|
---|
| 191 | * @param {*} payload
|
---|
| 192 | * @returns {payload is WeakMap<any, any>}
|
---|
| 193 | */
|
---|
| 194 | function isWeakMap(payload) {
|
---|
| 195 | return getType(payload) === 'WeakMap';
|
---|
| 196 | }
|
---|
| 197 | /**
|
---|
| 198 | * Returns whether the payload is a Set
|
---|
| 199 | *
|
---|
| 200 | * @param {*} payload
|
---|
| 201 | * @returns {payload is Set<any>}
|
---|
| 202 | */
|
---|
| 203 | function isSet(payload) {
|
---|
| 204 | return getType(payload) === 'Set';
|
---|
| 205 | }
|
---|
| 206 | /**
|
---|
| 207 | * Returns whether the payload is a WeakSet
|
---|
| 208 | *
|
---|
| 209 | * @param {*} payload
|
---|
| 210 | * @returns {payload is WeakSet<any>}
|
---|
| 211 | */
|
---|
| 212 | function isWeakSet(payload) {
|
---|
| 213 | return getType(payload) === 'WeakSet';
|
---|
| 214 | }
|
---|
| 215 | /**
|
---|
| 216 | * Returns whether the payload is a Symbol
|
---|
| 217 | *
|
---|
| 218 | * @param {*} payload
|
---|
| 219 | * @returns {payload is symbol}
|
---|
| 220 | */
|
---|
| 221 | function isSymbol(payload) {
|
---|
| 222 | return getType(payload) === 'Symbol';
|
---|
| 223 | }
|
---|
| 224 | /**
|
---|
| 225 | * Returns whether the payload is a Date, and that the date is valid
|
---|
| 226 | *
|
---|
| 227 | * @param {*} payload
|
---|
| 228 | * @returns {payload is Date}
|
---|
| 229 | */
|
---|
| 230 | function isDate(payload) {
|
---|
| 231 | return getType(payload) === 'Date' && !isNaN(payload);
|
---|
| 232 | }
|
---|
| 233 | /**
|
---|
| 234 | * Returns whether the payload is a Blob
|
---|
| 235 | *
|
---|
| 236 | * @param {*} payload
|
---|
| 237 | * @returns {payload is Blob}
|
---|
| 238 | */
|
---|
| 239 | function isBlob(payload) {
|
---|
| 240 | return getType(payload) === 'Blob';
|
---|
| 241 | }
|
---|
| 242 | /**
|
---|
| 243 | * Returns whether the payload is a File
|
---|
| 244 | *
|
---|
| 245 | * @param {*} payload
|
---|
| 246 | * @returns {payload is File}
|
---|
| 247 | */
|
---|
| 248 | function isFile(payload) {
|
---|
| 249 | return getType(payload) === 'File';
|
---|
| 250 | }
|
---|
| 251 | /**
|
---|
| 252 | * Returns whether the payload is a Promise
|
---|
| 253 | *
|
---|
| 254 | * @param {*} payload
|
---|
| 255 | * @returns {payload is Promise<any>}
|
---|
| 256 | */
|
---|
| 257 | function isPromise(payload) {
|
---|
| 258 | return getType(payload) === 'Promise';
|
---|
| 259 | }
|
---|
| 260 | /**
|
---|
| 261 | * Returns whether the payload is an Error
|
---|
| 262 | *
|
---|
| 263 | * @param {*} payload
|
---|
| 264 | * @returns {payload is Error}
|
---|
| 265 | */
|
---|
| 266 | function isError(payload) {
|
---|
| 267 | return getType(payload) === 'Error';
|
---|
| 268 | }
|
---|
| 269 | /**
|
---|
| 270 | * Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`)
|
---|
| 271 | *
|
---|
| 272 | * @param {*} payload
|
---|
| 273 | * @returns {payload is typeof NaN}
|
---|
| 274 | */
|
---|
| 275 | function isNaNValue(payload) {
|
---|
| 276 | return getType(payload) === 'Number' && isNaN(payload);
|
---|
| 277 | }
|
---|
| 278 | /**
|
---|
| 279 | * Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol)
|
---|
| 280 | *
|
---|
| 281 | * @param {*} payload
|
---|
| 282 | * @returns {(payload is boolean | null | undefined | number | string | symbol)}
|
---|
| 283 | */
|
---|
| 284 | function isPrimitive(payload) {
|
---|
| 285 | return (isBoolean(payload) ||
|
---|
| 286 | isNull(payload) ||
|
---|
| 287 | isUndefined(payload) ||
|
---|
| 288 | isNumber(payload) ||
|
---|
| 289 | isString(payload) ||
|
---|
| 290 | isSymbol(payload));
|
---|
| 291 | }
|
---|
| 292 | /**
|
---|
| 293 | * Returns true whether the payload is null or undefined
|
---|
| 294 | *
|
---|
| 295 | * @param {*} payload
|
---|
| 296 | * @returns {(payload is null | undefined)}
|
---|
| 297 | */
|
---|
| 298 | var isNullOrUndefined = isOneOf(isNull, isUndefined);
|
---|
| 299 | function isOneOf(a, b, c, d, e) {
|
---|
| 300 | return function (value) {
|
---|
| 301 | return a(value) || b(value) || (!!c && c(value)) || (!!d && d(value)) || (!!e && e(value));
|
---|
| 302 | };
|
---|
| 303 | }
|
---|
| 304 | /**
|
---|
| 305 | * Does a generic check to check that the given payload is of a given type.
|
---|
| 306 | * In cases like Number, it will return true for NaN as NaN is a Number (thanks javascript!);
|
---|
| 307 | * It will, however, differentiate between object and null
|
---|
| 308 | *
|
---|
| 309 | * @template T
|
---|
| 310 | * @param {*} payload
|
---|
| 311 | * @param {T} type
|
---|
| 312 | * @throws {TypeError} Will throw type error if type is an invalid type
|
---|
| 313 | * @returns {payload is T}
|
---|
| 314 | */
|
---|
| 315 | function isType(payload, type) {
|
---|
| 316 | if (!(type instanceof Function)) {
|
---|
| 317 | throw new TypeError('Type must be a function');
|
---|
| 318 | }
|
---|
| 319 | if (!Object.prototype.hasOwnProperty.call(type, 'prototype')) {
|
---|
| 320 | throw new TypeError('Type is not a class');
|
---|
| 321 | }
|
---|
| 322 | // Classes usually have names (as functions usually have names)
|
---|
| 323 | var name = type.name;
|
---|
| 324 | return getType(payload) === name || Boolean(payload && payload.constructor === type);
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | export { getType, isAnyObject, isArray, isBlob, isBoolean, isDate, isEmptyArray, isEmptyObject, isEmptyString, isError, isFile, isFullArray, isFullObject, isFullString, isFunction, isMap, isNaNValue, isNull, isNullOrUndefined, isNumber, isObject, isObjectLike, isOneOf, isPlainObject, isPrimitive, isPromise, isRegExp, isSet, isString, isSymbol, isType, isUndefined, isWeakMap, isWeakSet };
|
---|