[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, '__esModule', { value: true });
|
---|
| 4 |
|
---|
| 5 | /**
|
---|
| 6 | * Returns the object type of the given payload
|
---|
| 7 | *
|
---|
| 8 | * @param {*} payload
|
---|
| 9 | * @returns {string}
|
---|
| 10 | */
|
---|
| 11 | function getType(payload) {
|
---|
| 12 | return Object.prototype.toString.call(payload).slice(8, -1);
|
---|
| 13 | }
|
---|
| 14 | /**
|
---|
| 15 | * Returns whether the payload is undefined
|
---|
| 16 | *
|
---|
| 17 | * @param {*} payload
|
---|
| 18 | * @returns {payload is undefined}
|
---|
| 19 | */
|
---|
| 20 | function isUndefined(payload) {
|
---|
| 21 | return getType(payload) === 'Undefined';
|
---|
| 22 | }
|
---|
| 23 | /**
|
---|
| 24 | * Returns whether the payload is null
|
---|
| 25 | *
|
---|
| 26 | * @param {*} payload
|
---|
| 27 | * @returns {payload is null}
|
---|
| 28 | */
|
---|
| 29 | function isNull(payload) {
|
---|
| 30 | return getType(payload) === 'Null';
|
---|
| 31 | }
|
---|
| 32 | /**
|
---|
| 33 | * Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
|
---|
| 34 | *
|
---|
| 35 | * @param {*} payload
|
---|
| 36 | * @returns {payload is PlainObject}
|
---|
| 37 | */
|
---|
| 38 | function isPlainObject(payload) {
|
---|
| 39 | if (getType(payload) !== 'Object')
|
---|
| 40 | return false;
|
---|
| 41 | return payload.constructor === Object && Object.getPrototypeOf(payload) === Object.prototype;
|
---|
| 42 | }
|
---|
| 43 | /**
|
---|
| 44 | * Returns whether the payload is a plain JavaScript object (excluding special classes or objects with other prototypes)
|
---|
| 45 | *
|
---|
| 46 | * @param {*} payload
|
---|
| 47 | * @returns {payload is PlainObject}
|
---|
| 48 | */
|
---|
| 49 | function isObject(payload) {
|
---|
| 50 | return isPlainObject(payload);
|
---|
| 51 | }
|
---|
| 52 | /**
|
---|
| 53 | * Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)
|
---|
| 54 | *
|
---|
| 55 | * @param {*} payload
|
---|
| 56 | * @returns {payload is { [K in any]: never }}
|
---|
| 57 | */
|
---|
| 58 | function isEmptyObject(payload) {
|
---|
| 59 | return isPlainObject(payload) && Object.keys(payload).length === 0;
|
---|
| 60 | }
|
---|
| 61 | /**
|
---|
| 62 | * Returns whether the payload is a an empty object (excluding special classes or objects with other prototypes)
|
---|
| 63 | *
|
---|
| 64 | * @param {*} payload
|
---|
| 65 | * @returns {payload is PlainObject}
|
---|
| 66 | */
|
---|
| 67 | function isFullObject(payload) {
|
---|
| 68 | return isPlainObject(payload) && Object.keys(payload).length > 0;
|
---|
| 69 | }
|
---|
| 70 | /**
|
---|
| 71 | * Returns whether the payload is an any kind of object (including special classes or objects with different prototypes)
|
---|
| 72 | *
|
---|
| 73 | * @param {*} payload
|
---|
| 74 | * @returns {payload is PlainObject}
|
---|
| 75 | */
|
---|
| 76 | function isAnyObject(payload) {
|
---|
| 77 | return getType(payload) === 'Object';
|
---|
| 78 | }
|
---|
| 79 | /**
|
---|
| 80 | * Returns whether the payload is an object like a type passed in < >
|
---|
| 81 | *
|
---|
| 82 | * Usage: isObjectLike<{id: any}>(payload) // will make sure it's an object and has an `id` prop.
|
---|
| 83 | *
|
---|
| 84 | * @template T this must be passed in < >
|
---|
| 85 | * @param {*} payload
|
---|
| 86 | * @returns {payload is T}
|
---|
| 87 | */
|
---|
| 88 | function isObjectLike(payload) {
|
---|
| 89 | return isAnyObject(payload);
|
---|
| 90 | }
|
---|
| 91 | /**
|
---|
| 92 | * Returns whether the payload is a function (regular or async)
|
---|
| 93 | *
|
---|
| 94 | * @param {*} payload
|
---|
| 95 | * @returns {payload is AnyFunction}
|
---|
| 96 | */
|
---|
| 97 | function isFunction(payload) {
|
---|
| 98 | return typeof payload === 'function';
|
---|
| 99 | }
|
---|
| 100 | /**
|
---|
| 101 | * Returns whether the payload is an array
|
---|
| 102 | *
|
---|
| 103 | * @param {any} payload
|
---|
| 104 | * @returns {payload is any[]}
|
---|
| 105 | */
|
---|
| 106 | function isArray(payload) {
|
---|
| 107 | return getType(payload) === 'Array';
|
---|
| 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 | function isFullArray(payload) {
|
---|
| 116 | return isArray(payload) && payload.length > 0;
|
---|
| 117 | }
|
---|
| 118 | /**
|
---|
| 119 | * Returns whether the payload is a an empty array
|
---|
| 120 | *
|
---|
| 121 | * @param {*} payload
|
---|
| 122 | * @returns {payload is []}
|
---|
| 123 | */
|
---|
| 124 | function isEmptyArray(payload) {
|
---|
| 125 | return isArray(payload) && payload.length === 0;
|
---|
| 126 | }
|
---|
| 127 | /**
|
---|
| 128 | * Returns whether the payload is a string
|
---|
| 129 | *
|
---|
| 130 | * @param {*} payload
|
---|
| 131 | * @returns {payload is string}
|
---|
| 132 | */
|
---|
| 133 | function isString(payload) {
|
---|
| 134 | return getType(payload) === 'String';
|
---|
| 135 | }
|
---|
| 136 | /**
|
---|
| 137 | * Returns whether the payload is a string, BUT returns false for ''
|
---|
| 138 | *
|
---|
| 139 | * @param {*} payload
|
---|
| 140 | * @returns {payload is string}
|
---|
| 141 | */
|
---|
| 142 | function isFullString(payload) {
|
---|
| 143 | return isString(payload) && payload !== '';
|
---|
| 144 | }
|
---|
| 145 | /**
|
---|
| 146 | * Returns whether the payload is ''
|
---|
| 147 | *
|
---|
| 148 | * @param {*} payload
|
---|
| 149 | * @returns {payload is string}
|
---|
| 150 | */
|
---|
| 151 | function isEmptyString(payload) {
|
---|
| 152 | return payload === '';
|
---|
| 153 | }
|
---|
| 154 | /**
|
---|
| 155 | * Returns whether the payload is a number (but not NaN)
|
---|
| 156 | *
|
---|
| 157 | * This will return `false` for `NaN`!!
|
---|
| 158 | *
|
---|
| 159 | * @param {*} payload
|
---|
| 160 | * @returns {payload is number}
|
---|
| 161 | */
|
---|
| 162 | function isNumber(payload) {
|
---|
| 163 | return getType(payload) === 'Number' && !isNaN(payload);
|
---|
| 164 | }
|
---|
| 165 | /**
|
---|
| 166 | * Returns whether the payload is a boolean
|
---|
| 167 | *
|
---|
| 168 | * @param {*} payload
|
---|
| 169 | * @returns {payload is boolean}
|
---|
| 170 | */
|
---|
| 171 | function isBoolean(payload) {
|
---|
| 172 | return getType(payload) === 'Boolean';
|
---|
| 173 | }
|
---|
| 174 | /**
|
---|
| 175 | * Returns whether the payload is a regular expression (RegExp)
|
---|
| 176 | *
|
---|
| 177 | * @param {*} payload
|
---|
| 178 | * @returns {payload is RegExp}
|
---|
| 179 | */
|
---|
| 180 | function isRegExp(payload) {
|
---|
| 181 | return getType(payload) === 'RegExp';
|
---|
| 182 | }
|
---|
| 183 | /**
|
---|
| 184 | * Returns whether the payload is a Map
|
---|
| 185 | *
|
---|
| 186 | * @param {*} payload
|
---|
| 187 | * @returns {payload is Map<any, any>}
|
---|
| 188 | */
|
---|
| 189 | function isMap(payload) {
|
---|
| 190 | return getType(payload) === 'Map';
|
---|
| 191 | }
|
---|
| 192 | /**
|
---|
| 193 | * Returns whether the payload is a WeakMap
|
---|
| 194 | *
|
---|
| 195 | * @param {*} payload
|
---|
| 196 | * @returns {payload is WeakMap<any, any>}
|
---|
| 197 | */
|
---|
| 198 | function isWeakMap(payload) {
|
---|
| 199 | return getType(payload) === 'WeakMap';
|
---|
| 200 | }
|
---|
| 201 | /**
|
---|
| 202 | * Returns whether the payload is a Set
|
---|
| 203 | *
|
---|
| 204 | * @param {*} payload
|
---|
| 205 | * @returns {payload is Set<any>}
|
---|
| 206 | */
|
---|
| 207 | function isSet(payload) {
|
---|
| 208 | return getType(payload) === 'Set';
|
---|
| 209 | }
|
---|
| 210 | /**
|
---|
| 211 | * Returns whether the payload is a WeakSet
|
---|
| 212 | *
|
---|
| 213 | * @param {*} payload
|
---|
| 214 | * @returns {payload is WeakSet<any>}
|
---|
| 215 | */
|
---|
| 216 | function isWeakSet(payload) {
|
---|
| 217 | return getType(payload) === 'WeakSet';
|
---|
| 218 | }
|
---|
| 219 | /**
|
---|
| 220 | * Returns whether the payload is a Symbol
|
---|
| 221 | *
|
---|
| 222 | * @param {*} payload
|
---|
| 223 | * @returns {payload is symbol}
|
---|
| 224 | */
|
---|
| 225 | function isSymbol(payload) {
|
---|
| 226 | return getType(payload) === 'Symbol';
|
---|
| 227 | }
|
---|
| 228 | /**
|
---|
| 229 | * Returns whether the payload is a Date, and that the date is valid
|
---|
| 230 | *
|
---|
| 231 | * @param {*} payload
|
---|
| 232 | * @returns {payload is Date}
|
---|
| 233 | */
|
---|
| 234 | function isDate(payload) {
|
---|
| 235 | return getType(payload) === 'Date' && !isNaN(payload);
|
---|
| 236 | }
|
---|
| 237 | /**
|
---|
| 238 | * Returns whether the payload is a Blob
|
---|
| 239 | *
|
---|
| 240 | * @param {*} payload
|
---|
| 241 | * @returns {payload is Blob}
|
---|
| 242 | */
|
---|
| 243 | function isBlob(payload) {
|
---|
| 244 | return getType(payload) === 'Blob';
|
---|
| 245 | }
|
---|
| 246 | /**
|
---|
| 247 | * Returns whether the payload is a File
|
---|
| 248 | *
|
---|
| 249 | * @param {*} payload
|
---|
| 250 | * @returns {payload is File}
|
---|
| 251 | */
|
---|
| 252 | function isFile(payload) {
|
---|
| 253 | return getType(payload) === 'File';
|
---|
| 254 | }
|
---|
| 255 | /**
|
---|
| 256 | * Returns whether the payload is a Promise
|
---|
| 257 | *
|
---|
| 258 | * @param {*} payload
|
---|
| 259 | * @returns {payload is Promise<any>}
|
---|
| 260 | */
|
---|
| 261 | function isPromise(payload) {
|
---|
| 262 | return getType(payload) === 'Promise';
|
---|
| 263 | }
|
---|
| 264 | /**
|
---|
| 265 | * Returns whether the payload is an Error
|
---|
| 266 | *
|
---|
| 267 | * @param {*} payload
|
---|
| 268 | * @returns {payload is Error}
|
---|
| 269 | */
|
---|
| 270 | function isError(payload) {
|
---|
| 271 | return getType(payload) === 'Error';
|
---|
| 272 | }
|
---|
| 273 | /**
|
---|
| 274 | * Returns whether the payload is literally the value `NaN` (it's `NaN` and also a `number`)
|
---|
| 275 | *
|
---|
| 276 | * @param {*} payload
|
---|
| 277 | * @returns {payload is typeof NaN}
|
---|
| 278 | */
|
---|
| 279 | function isNaNValue(payload) {
|
---|
| 280 | return getType(payload) === 'Number' && isNaN(payload);
|
---|
| 281 | }
|
---|
| 282 | /**
|
---|
| 283 | * Returns whether the payload is a primitive type (eg. Boolean | Null | Undefined | Number | String | Symbol)
|
---|
| 284 | *
|
---|
| 285 | * @param {*} payload
|
---|
| 286 | * @returns {(payload is boolean | null | undefined | number | string | symbol)}
|
---|
| 287 | */
|
---|
| 288 | function isPrimitive(payload) {
|
---|
| 289 | return (isBoolean(payload) ||
|
---|
| 290 | isNull(payload) ||
|
---|
| 291 | isUndefined(payload) ||
|
---|
| 292 | isNumber(payload) ||
|
---|
| 293 | isString(payload) ||
|
---|
| 294 | isSymbol(payload));
|
---|
| 295 | }
|
---|
| 296 | /**
|
---|
| 297 | * Returns true whether the payload is null or undefined
|
---|
| 298 | *
|
---|
| 299 | * @param {*} payload
|
---|
| 300 | * @returns {(payload is null | undefined)}
|
---|
| 301 | */
|
---|
| 302 | var isNullOrUndefined = isOneOf(isNull, isUndefined);
|
---|
| 303 | function isOneOf(a, b, c, d, e) {
|
---|
| 304 | return function (value) {
|
---|
| 305 | return a(value) || b(value) || (!!c && c(value)) || (!!d && d(value)) || (!!e && e(value));
|
---|
| 306 | };
|
---|
| 307 | }
|
---|
| 308 | /**
|
---|
| 309 | * Does a generic check to check that the given payload is of a given type.
|
---|
| 310 | * In cases like Number, it will return true for NaN as NaN is a Number (thanks javascript!);
|
---|
| 311 | * It will, however, differentiate between object and null
|
---|
| 312 | *
|
---|
| 313 | * @template T
|
---|
| 314 | * @param {*} payload
|
---|
| 315 | * @param {T} type
|
---|
| 316 | * @throws {TypeError} Will throw type error if type is an invalid type
|
---|
| 317 | * @returns {payload is T}
|
---|
| 318 | */
|
---|
| 319 | function isType(payload, type) {
|
---|
| 320 | if (!(type instanceof Function)) {
|
---|
| 321 | throw new TypeError('Type must be a function');
|
---|
| 322 | }
|
---|
| 323 | if (!Object.prototype.hasOwnProperty.call(type, 'prototype')) {
|
---|
| 324 | throw new TypeError('Type is not a class');
|
---|
| 325 | }
|
---|
| 326 | // Classes usually have names (as functions usually have names)
|
---|
| 327 | var name = type.name;
|
---|
| 328 | return getType(payload) === name || Boolean(payload && payload.constructor === type);
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | exports.getType = getType;
|
---|
| 332 | exports.isAnyObject = isAnyObject;
|
---|
| 333 | exports.isArray = isArray;
|
---|
| 334 | exports.isBlob = isBlob;
|
---|
| 335 | exports.isBoolean = isBoolean;
|
---|
| 336 | exports.isDate = isDate;
|
---|
| 337 | exports.isEmptyArray = isEmptyArray;
|
---|
| 338 | exports.isEmptyObject = isEmptyObject;
|
---|
| 339 | exports.isEmptyString = isEmptyString;
|
---|
| 340 | exports.isError = isError;
|
---|
| 341 | exports.isFile = isFile;
|
---|
| 342 | exports.isFullArray = isFullArray;
|
---|
| 343 | exports.isFullObject = isFullObject;
|
---|
| 344 | exports.isFullString = isFullString;
|
---|
| 345 | exports.isFunction = isFunction;
|
---|
| 346 | exports.isMap = isMap;
|
---|
| 347 | exports.isNaNValue = isNaNValue;
|
---|
| 348 | exports.isNull = isNull;
|
---|
| 349 | exports.isNullOrUndefined = isNullOrUndefined;
|
---|
| 350 | exports.isNumber = isNumber;
|
---|
| 351 | exports.isObject = isObject;
|
---|
| 352 | exports.isObjectLike = isObjectLike;
|
---|
| 353 | exports.isOneOf = isOneOf;
|
---|
| 354 | exports.isPlainObject = isPlainObject;
|
---|
| 355 | exports.isPrimitive = isPrimitive;
|
---|
| 356 | exports.isPromise = isPromise;
|
---|
| 357 | exports.isRegExp = isRegExp;
|
---|
| 358 | exports.isSet = isSet;
|
---|
| 359 | exports.isString = isString;
|
---|
| 360 | exports.isSymbol = isSymbol;
|
---|
| 361 | exports.isType = isType;
|
---|
| 362 | exports.isUndefined = isUndefined;
|
---|
| 363 | exports.isWeakMap = isWeakMap;
|
---|
| 364 | exports.isWeakSet = isWeakSet;
|
---|