4
1

index.cjs.js 9.1 KB

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