index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /* globals window, HTMLElement */
  2. 'use strict';
  3. /**!
  4. * is
  5. * the definitive JavaScript type testing library
  6. *
  7. * @copyright 2013-2014 Enrico Marino / Jordan Harband
  8. * @license MIT
  9. */
  10. var objProto = Object.prototype;
  11. var owns = objProto.hasOwnProperty;
  12. var toStr = objProto.toString;
  13. var symbolValueOf;
  14. if (typeof Symbol === 'function') {
  15. symbolValueOf = Symbol.prototype.valueOf;
  16. }
  17. var bigIntValueOf;
  18. if (typeof BigInt === 'function') {
  19. bigIntValueOf = BigInt.prototype.valueOf;
  20. }
  21. var isActualNaN = function (value) {
  22. return value !== value;
  23. };
  24. var NON_HOST_TYPES = {
  25. 'boolean': 1,
  26. number: 1,
  27. string: 1,
  28. undefined: 1
  29. };
  30. var base64Regex = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/;
  31. var hexRegex = /^[A-Fa-f0-9]+$/;
  32. /**
  33. * Expose `is`
  34. */
  35. var is = {};
  36. /**
  37. * Test general.
  38. */
  39. /**
  40. * is.type
  41. * Test if `value` is a type of `type`.
  42. *
  43. * @param {*} value value to test
  44. * @param {String} type type
  45. * @return {Boolean} true if `value` is a type of `type`, false otherwise
  46. * @api public
  47. */
  48. is.a = is.type = function (value, type) {
  49. return typeof value === type;
  50. };
  51. /**
  52. * is.defined
  53. * Test if `value` is defined.
  54. *
  55. * @param {*} value value to test
  56. * @return {Boolean} true if 'value' is defined, false otherwise
  57. * @api public
  58. */
  59. is.defined = function (value) {
  60. return typeof value !== 'undefined';
  61. };
  62. /**
  63. * is.empty
  64. * Test if `value` is empty.
  65. *
  66. * @param {*} value value to test
  67. * @return {Boolean} true if `value` is empty, false otherwise
  68. * @api public
  69. */
  70. is.empty = function (value) {
  71. var type = toStr.call(value);
  72. var key;
  73. if (type === '[object Array]' || type === '[object Arguments]' || type === '[object String]') {
  74. return value.length === 0;
  75. }
  76. if (type === '[object Object]') {
  77. for (key in value) {
  78. if (owns.call(value, key)) {
  79. return false;
  80. }
  81. }
  82. return true;
  83. }
  84. return !value;
  85. };
  86. /**
  87. * is.equal
  88. * Test if `value` is equal to `other`.
  89. *
  90. * @param {*} value value to test
  91. * @param {*} other value to compare with
  92. * @return {Boolean} true if `value` is equal to `other`, false otherwise
  93. */
  94. is.equal = function equal(value, other) {
  95. if (value === other) {
  96. return true;
  97. }
  98. var type = toStr.call(value);
  99. var key;
  100. if (type !== toStr.call(other)) {
  101. return false;
  102. }
  103. if (type === '[object Object]') {
  104. for (key in value) {
  105. if (!is.equal(value[key], other[key]) || !(key in other)) {
  106. return false;
  107. }
  108. }
  109. for (key in other) {
  110. if (!is.equal(value[key], other[key]) || !(key in value)) {
  111. return false;
  112. }
  113. }
  114. return true;
  115. }
  116. if (type === '[object Array]') {
  117. key = value.length;
  118. if (key !== other.length) {
  119. return false;
  120. }
  121. while (key--) {
  122. if (!is.equal(value[key], other[key])) {
  123. return false;
  124. }
  125. }
  126. return true;
  127. }
  128. if (type === '[object Function]') {
  129. return value.prototype === other.prototype;
  130. }
  131. if (type === '[object Date]') {
  132. return value.getTime() === other.getTime();
  133. }
  134. return false;
  135. };
  136. /**
  137. * is.hosted
  138. * Test if `value` is hosted by `host`.
  139. *
  140. * @param {*} value to test
  141. * @param {*} host host to test with
  142. * @return {Boolean} true if `value` is hosted by `host`, false otherwise
  143. * @api public
  144. */
  145. is.hosted = function (value, host) {
  146. var type = typeof host[value];
  147. return type === 'object' ? !!host[value] : !NON_HOST_TYPES[type];
  148. };
  149. /**
  150. * is.instance
  151. * Test if `value` is an instance of `constructor`.
  152. *
  153. * @param {*} value value to test
  154. * @return {Boolean} true if `value` is an instance of `constructor`
  155. * @api public
  156. */
  157. is.instance = is['instanceof'] = function (value, constructor) {
  158. return value instanceof constructor;
  159. };
  160. /**
  161. * is.nil / is.null
  162. * Test if `value` is null.
  163. *
  164. * @param {*} value value to test
  165. * @return {Boolean} true if `value` is null, false otherwise
  166. * @api public
  167. */
  168. is.nil = is['null'] = function (value) {
  169. return value === null;
  170. };
  171. /**
  172. * is.undef / is.undefined
  173. * Test if `value` is undefined.
  174. *
  175. * @param {*} value value to test
  176. * @return {Boolean} true if `value` is undefined, false otherwise
  177. * @api public
  178. */
  179. is.undef = is.undefined = function (value) {
  180. return typeof value === 'undefined';
  181. };
  182. /**
  183. * Test arguments.
  184. */
  185. /**
  186. * is.args
  187. * Test if `value` is an arguments object.
  188. *
  189. * @param {*} value value to test
  190. * @return {Boolean} true if `value` is an arguments object, false otherwise
  191. * @api public
  192. */
  193. is.args = is.arguments = function (value) {
  194. var isStandardArguments = toStr.call(value) === '[object Arguments]';
  195. var isOldArguments = !is.array(value) && is.arraylike(value) && is.object(value) && is.fn(value.callee);
  196. return isStandardArguments || isOldArguments;
  197. };
  198. /**
  199. * Test array.
  200. */
  201. /**
  202. * is.array
  203. * Test if 'value' is an array.
  204. *
  205. * @param {*} value value to test
  206. * @return {Boolean} true if `value` is an array, false otherwise
  207. * @api public
  208. */
  209. is.array = Array.isArray || function (value) {
  210. return toStr.call(value) === '[object Array]';
  211. };
  212. /**
  213. * is.arguments.empty
  214. * Test if `value` is an empty arguments object.
  215. *
  216. * @param {*} value value to test
  217. * @return {Boolean} true if `value` is an empty arguments object, false otherwise
  218. * @api public
  219. */
  220. is.args.empty = function (value) {
  221. return is.args(value) && value.length === 0;
  222. };
  223. /**
  224. * is.array.empty
  225. * Test if `value` is an empty array.
  226. *
  227. * @param {*} value value to test
  228. * @return {Boolean} true if `value` is an empty array, false otherwise
  229. * @api public
  230. */
  231. is.array.empty = function (value) {
  232. return is.array(value) && value.length === 0;
  233. };
  234. /**
  235. * is.arraylike
  236. * Test if `value` is an arraylike object.
  237. *
  238. * @param {*} value value to test
  239. * @return {Boolean} true if `value` is an arguments object, false otherwise
  240. * @api public
  241. */
  242. is.arraylike = function (value) {
  243. return !!value && !is.bool(value)
  244. && owns.call(value, 'length')
  245. && isFinite(value.length)
  246. && is.number(value.length)
  247. && value.length >= 0;
  248. };
  249. /**
  250. * Test boolean.
  251. */
  252. /**
  253. * is.bool
  254. * Test if `value` is a boolean.
  255. *
  256. * @param {*} value value to test
  257. * @return {Boolean} true if `value` is a boolean, false otherwise
  258. * @api public
  259. */
  260. is.bool = is['boolean'] = function (value) {
  261. return toStr.call(value) === '[object Boolean]';
  262. };
  263. /**
  264. * is.false
  265. * Test if `value` is false.
  266. *
  267. * @param {*} value value to test
  268. * @return {Boolean} true if `value` is false, false otherwise
  269. * @api public
  270. */
  271. is['false'] = function (value) {
  272. return is.bool(value) && Boolean(Number(value)) === false;
  273. };
  274. /**
  275. * is.true
  276. * Test if `value` is true.
  277. *
  278. * @param {*} value value to test
  279. * @return {Boolean} true if `value` is true, false otherwise
  280. * @api public
  281. */
  282. is['true'] = function (value) {
  283. return is.bool(value) && Boolean(Number(value)) === true;
  284. };
  285. /**
  286. * Test date.
  287. */
  288. /**
  289. * is.date
  290. * Test if `value` is a date.
  291. *
  292. * @param {*} value value to test
  293. * @return {Boolean} true if `value` is a date, false otherwise
  294. * @api public
  295. */
  296. is.date = function (value) {
  297. return toStr.call(value) === '[object Date]';
  298. };
  299. /**
  300. * is.date.valid
  301. * Test if `value` is a valid date.
  302. *
  303. * @param {*} value value to test
  304. * @returns {Boolean} true if `value` is a valid date, false otherwise
  305. */
  306. is.date.valid = function (value) {
  307. return is.date(value) && !isNaN(Number(value));
  308. };
  309. /**
  310. * Test element.
  311. */
  312. /**
  313. * is.element
  314. * Test if `value` is an html element.
  315. *
  316. * @param {*} value value to test
  317. * @return {Boolean} true if `value` is an HTML Element, false otherwise
  318. * @api public
  319. */
  320. is.element = function (value) {
  321. return value !== undefined
  322. && typeof HTMLElement !== 'undefined'
  323. && value instanceof HTMLElement
  324. && value.nodeType === 1;
  325. };
  326. /**
  327. * Test error.
  328. */
  329. /**
  330. * is.error
  331. * Test if `value` is an error object.
  332. *
  333. * @param {*} value value to test
  334. * @return {Boolean} true if `value` is an error object, false otherwise
  335. * @api public
  336. */
  337. is.error = function (value) {
  338. return toStr.call(value) === '[object Error]';
  339. };
  340. /**
  341. * Test function.
  342. */
  343. /**
  344. * is.fn / is.function (deprecated)
  345. * Test if `value` is a function.
  346. *
  347. * @param {*} value value to test
  348. * @return {Boolean} true if `value` is a function, false otherwise
  349. * @api public
  350. */
  351. is.fn = is['function'] = function (value) {
  352. var isAlert = typeof window !== 'undefined' && value === window.alert;
  353. if (isAlert) {
  354. return true;
  355. }
  356. var str = toStr.call(value);
  357. return str === '[object Function]' || str === '[object GeneratorFunction]' || str === '[object AsyncFunction]';
  358. };
  359. /**
  360. * Test number.
  361. */
  362. /**
  363. * is.number
  364. * Test if `value` is a number.
  365. *
  366. * @param {*} value value to test
  367. * @return {Boolean} true if `value` is a number, false otherwise
  368. * @api public
  369. */
  370. is.number = function (value) {
  371. return toStr.call(value) === '[object Number]';
  372. };
  373. /**
  374. * is.infinite
  375. * Test if `value` is positive or negative infinity.
  376. *
  377. * @param {*} value value to test
  378. * @return {Boolean} true if `value` is positive or negative Infinity, false otherwise
  379. * @api public
  380. */
  381. is.infinite = function (value) {
  382. return value === Infinity || value === -Infinity;
  383. };
  384. /**
  385. * is.decimal
  386. * Test if `value` is a decimal number.
  387. *
  388. * @param {*} value value to test
  389. * @return {Boolean} true if `value` is a decimal number, false otherwise
  390. * @api public
  391. */
  392. is.decimal = function (value) {
  393. return is.number(value) && !isActualNaN(value) && !is.infinite(value) && value % 1 !== 0;
  394. };
  395. /**
  396. * is.divisibleBy
  397. * Test if `value` is divisible by `n`.
  398. *
  399. * @param {Number} value value to test
  400. * @param {Number} n dividend
  401. * @return {Boolean} true if `value` is divisible by `n`, false otherwise
  402. * @api public
  403. */
  404. is.divisibleBy = function (value, n) {
  405. var isDividendInfinite = is.infinite(value);
  406. var isDivisorInfinite = is.infinite(n);
  407. var isNonZeroNumber = is.number(value) && !isActualNaN(value) && is.number(n) && !isActualNaN(n) && n !== 0;
  408. return isDividendInfinite || isDivisorInfinite || (isNonZeroNumber && value % n === 0);
  409. };
  410. /**
  411. * is.integer
  412. * Test if `value` is an integer.
  413. *
  414. * @param value to test
  415. * @return {Boolean} true if `value` is an integer, false otherwise
  416. * @api public
  417. */
  418. is.integer = is['int'] = function (value) {
  419. return is.number(value) && !isActualNaN(value) && value % 1 === 0;
  420. };
  421. /**
  422. * is.maximum
  423. * Test if `value` is greater than 'others' values.
  424. *
  425. * @param {Number} value value to test
  426. * @param {Array} others values to compare with
  427. * @return {Boolean} true if `value` is greater than `others` values
  428. * @api public
  429. */
  430. is.maximum = function (value, others) {
  431. if (isActualNaN(value)) {
  432. throw new TypeError('NaN is not a valid value');
  433. } else if (!is.arraylike(others)) {
  434. throw new TypeError('second argument must be array-like');
  435. }
  436. var len = others.length;
  437. while (--len >= 0) {
  438. if (value < others[len]) {
  439. return false;
  440. }
  441. }
  442. return true;
  443. };
  444. /**
  445. * is.minimum
  446. * Test if `value` is less than `others` values.
  447. *
  448. * @param {Number} value value to test
  449. * @param {Array} others values to compare with
  450. * @return {Boolean} true if `value` is less than `others` values
  451. * @api public
  452. */
  453. is.minimum = function (value, others) {
  454. if (isActualNaN(value)) {
  455. throw new TypeError('NaN is not a valid value');
  456. } else if (!is.arraylike(others)) {
  457. throw new TypeError('second argument must be array-like');
  458. }
  459. var len = others.length;
  460. while (--len >= 0) {
  461. if (value > others[len]) {
  462. return false;
  463. }
  464. }
  465. return true;
  466. };
  467. /**
  468. * is.nan
  469. * Test if `value` is not a number.
  470. *
  471. * @param {*} value value to test
  472. * @return {Boolean} true if `value` is not a number, false otherwise
  473. * @api public
  474. */
  475. is.nan = function (value) {
  476. return !is.number(value) || value !== value;
  477. };
  478. /**
  479. * is.even
  480. * Test if `value` is an even number.
  481. *
  482. * @param {Number} value value to test
  483. * @return {Boolean} true if `value` is an even number, false otherwise
  484. * @api public
  485. */
  486. is.even = function (value) {
  487. return is.infinite(value) || (is.number(value) && value === value && value % 2 === 0);
  488. };
  489. /**
  490. * is.odd
  491. * Test if `value` is an odd number.
  492. *
  493. * @param {Number} value value to test
  494. * @return {Boolean} true if `value` is an odd number, false otherwise
  495. * @api public
  496. */
  497. is.odd = function (value) {
  498. return is.infinite(value) || (is.number(value) && value === value && value % 2 !== 0);
  499. };
  500. /**
  501. * is.ge
  502. * Test if `value` is greater than or equal to `other`.
  503. *
  504. * @param {Number} value value to test
  505. * @param {Number} other value to compare with
  506. * @return {Boolean}
  507. * @api public
  508. */
  509. is.ge = function (value, other) {
  510. if (isActualNaN(value) || isActualNaN(other)) {
  511. throw new TypeError('NaN is not a valid value');
  512. }
  513. return !is.infinite(value) && !is.infinite(other) && value >= other;
  514. };
  515. /**
  516. * is.gt
  517. * Test if `value` is greater than `other`.
  518. *
  519. * @param {Number} value value to test
  520. * @param {Number} other value to compare with
  521. * @return {Boolean}
  522. * @api public
  523. */
  524. is.gt = function (value, other) {
  525. if (isActualNaN(value) || isActualNaN(other)) {
  526. throw new TypeError('NaN is not a valid value');
  527. }
  528. return !is.infinite(value) && !is.infinite(other) && value > other;
  529. };
  530. /**
  531. * is.le
  532. * Test if `value` is less than or equal to `other`.
  533. *
  534. * @param {Number} value value to test
  535. * @param {Number} other value to compare with
  536. * @return {Boolean} if 'value' is less than or equal to 'other'
  537. * @api public
  538. */
  539. is.le = function (value, other) {
  540. if (isActualNaN(value) || isActualNaN(other)) {
  541. throw new TypeError('NaN is not a valid value');
  542. }
  543. return !is.infinite(value) && !is.infinite(other) && value <= other;
  544. };
  545. /**
  546. * is.lt
  547. * Test if `value` is less than `other`.
  548. *
  549. * @param {Number} value value to test
  550. * @param {Number} other value to compare with
  551. * @return {Boolean} if `value` is less than `other`
  552. * @api public
  553. */
  554. is.lt = function (value, other) {
  555. if (isActualNaN(value) || isActualNaN(other)) {
  556. throw new TypeError('NaN is not a valid value');
  557. }
  558. return !is.infinite(value) && !is.infinite(other) && value < other;
  559. };
  560. /**
  561. * is.within
  562. * Test if `value` is within `start` and `finish`.
  563. *
  564. * @param {Number} value value to test
  565. * @param {Number} start lower bound
  566. * @param {Number} finish upper bound
  567. * @return {Boolean} true if 'value' is is within 'start' and 'finish'
  568. * @api public
  569. */
  570. is.within = function (value, start, finish) {
  571. if (isActualNaN(value) || isActualNaN(start) || isActualNaN(finish)) {
  572. throw new TypeError('NaN is not a valid value');
  573. } else if (!is.number(value) || !is.number(start) || !is.number(finish)) {
  574. throw new TypeError('all arguments must be numbers');
  575. }
  576. var isAnyInfinite = is.infinite(value) || is.infinite(start) || is.infinite(finish);
  577. return isAnyInfinite || (value >= start && value <= finish);
  578. };
  579. /**
  580. * Test object.
  581. */
  582. /**
  583. * is.object
  584. * Test if `value` is an object.
  585. *
  586. * @param {*} value value to test
  587. * @return {Boolean} true if `value` is an object, false otherwise
  588. * @api public
  589. */
  590. is.object = function (value) {
  591. return toStr.call(value) === '[object Object]';
  592. };
  593. /**
  594. * is.primitive
  595. * Test if `value` is a primitive.
  596. *
  597. * @param {*} value value to test
  598. * @return {Boolean} true if `value` is a primitive, false otherwise
  599. * @api public
  600. */
  601. is.primitive = function isPrimitive(value) {
  602. if (!value) {
  603. return true;
  604. }
  605. if (typeof value === 'object' || is.object(value) || is.fn(value) || is.array(value)) {
  606. return false;
  607. }
  608. return true;
  609. };
  610. /**
  611. * is.hash
  612. * Test if `value` is a hash - a plain object literal.
  613. *
  614. * @param {*} value value to test
  615. * @return {Boolean} true if `value` is a hash, false otherwise
  616. * @api public
  617. */
  618. is.hash = function (value) {
  619. return is.object(value) && value.constructor === Object && !value.nodeType && !value.setInterval;
  620. };
  621. /**
  622. * Test regexp.
  623. */
  624. /**
  625. * is.regexp
  626. * Test if `value` is a regular expression.
  627. *
  628. * @param {*} value value to test
  629. * @return {Boolean} true if `value` is a regexp, false otherwise
  630. * @api public
  631. */
  632. is.regexp = function (value) {
  633. return toStr.call(value) === '[object RegExp]';
  634. };
  635. /**
  636. * Test string.
  637. */
  638. /**
  639. * is.string
  640. * Test if `value` is a string.
  641. *
  642. * @param {*} value value to test
  643. * @return {Boolean} true if 'value' is a string, false otherwise
  644. * @api public
  645. */
  646. is.string = function (value) {
  647. return toStr.call(value) === '[object String]';
  648. };
  649. /**
  650. * Test base64 string.
  651. */
  652. /**
  653. * is.base64
  654. * Test if `value` is a valid base64 encoded string.
  655. *
  656. * @param {*} value value to test
  657. * @return {Boolean} true if 'value' is a base64 encoded string, false otherwise
  658. * @api public
  659. */
  660. is.base64 = function (value) {
  661. return is.string(value) && (!value.length || base64Regex.test(value));
  662. };
  663. /**
  664. * Test base64 string.
  665. */
  666. /**
  667. * is.hex
  668. * Test if `value` is a valid hex encoded string.
  669. *
  670. * @param {*} value value to test
  671. * @return {Boolean} true if 'value' is a hex encoded string, false otherwise
  672. * @api public
  673. */
  674. is.hex = function (value) {
  675. return is.string(value) && (!value.length || hexRegex.test(value));
  676. };
  677. /**
  678. * is.symbol
  679. * Test if `value` is an ES6 Symbol
  680. *
  681. * @param {*} value value to test
  682. * @return {Boolean} true if `value` is a Symbol, false otherise
  683. * @api public
  684. */
  685. is.symbol = function (value) {
  686. return typeof Symbol === 'function' && toStr.call(value) === '[object Symbol]' && typeof symbolValueOf.call(value) === 'symbol';
  687. };
  688. /**
  689. * is.bigint
  690. * Test if `value` is an ES-proposed BigInt
  691. *
  692. * @param {*} value value to test
  693. * @return {Boolean} true if `value` is a BigInt, false otherise
  694. * @api public
  695. */
  696. is.bigint = function (value) {
  697. // eslint-disable-next-line valid-typeof
  698. return typeof BigInt === 'function' && toStr.call(value) === '[object BigInt]' && typeof bigIntValueOf.call(value) === 'bigint';
  699. };
  700. module.exports = is;