index.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /* globals window, document, HTMLElement */
  2. 'use strict';
  3. var test = require('tape');
  4. var is = require('../index.js');
  5. var forEach = require('foreach');
  6. var toStr = Object.prototype.toString;
  7. var genFn = require('make-generator-function');
  8. test('is.type', function (t) {
  9. var booleans = [true, false];
  10. forEach(booleans, function (boolean) {
  11. t.ok(is.type(boolean, 'boolean'), '"' + boolean + '" is a boolean');
  12. });
  13. var numbers = [1, 0 / 1, 0 / -1, NaN, Infinity, -Infinity];
  14. forEach(numbers, function (number) {
  15. t.ok(is.type(number, 'number'), '"' + number + '" is a number');
  16. });
  17. var objects = [{}, null, new Date()];
  18. forEach(objects, function (object) {
  19. t.ok(is.type(object, 'object'), '"' + object + '" is an object');
  20. });
  21. var strings = ['', 'abc'];
  22. forEach(strings, function (string) {
  23. t.ok(is.type(string, 'string'), '"' + string + '" is a string');
  24. });
  25. t.ok(is.type(undefined, 'undefined'), 'undefined is undefined');
  26. t.end();
  27. });
  28. test('is.undef', function (t) {
  29. t.ok(is.undef(), 'absent undefined is undefined');
  30. t.ok(is.undef(undefined), 'literal undefined is undefined');
  31. t.notOk(is.undef(null), 'null is not undefined');
  32. t.notOk(is.undef({}), 'object is not undefined');
  33. t.end();
  34. });
  35. test('is.defined', function (t) {
  36. t.notOk(is.defined(), 'undefined is not defined');
  37. t.ok(is.defined(null), 'null is defined');
  38. t.ok(is.defined({}), 'object is defined');
  39. t.end();
  40. });
  41. test('is.empty', function (t) {
  42. t.ok(is.empty(''), 'empty string is empty');
  43. t.ok(is.empty(Object('')), 'empty String object is empty');
  44. t.ok(is.empty([]), 'empty array is empty');
  45. t.ok(is.empty({}), 'empty object is empty');
  46. t.ok(is.empty(null), 'null is empty');
  47. t.ok(is.empty(), 'undefined is empty');
  48. t.ok(is.empty(undefined), 'undefined is empty');
  49. t.ok(is.empty(false), 'false is empty');
  50. t.ok(is.empty(0), '0 is empty');
  51. t.ok(is.empty(NaN), 'nan is empty');
  52. (function () {
  53. t.ok(is.empty(arguments), 'empty arguments is empty');
  54. }());
  55. t.notOk(is.empty({ a: 1 }), 'nonempty object is not empty');
  56. t.notOk(is.empty(true), 'true is not empty');
  57. t.notOk(is.empty(/a/g), 'regex is not empty');
  58. t.notOk(is.empty(new Date()), 'date is not empty');
  59. t.end();
  60. });
  61. test('is.equal', function (t) {
  62. t.test('primitives', function (pt) {
  63. var primitives = [true, false, undefined, null, '', 'foo', 0, Infinity, -Infinity];
  64. pt.plan(primitives.length);
  65. for (var i = 0; i < primitives.length; ++i) {
  66. pt.ok(is.equal(primitives[i], primitives[i]), 'primitives are equal to themselves: ' + primitives[i]);
  67. }
  68. pt.end();
  69. });
  70. t.test('arrays', function (at) {
  71. at.ok(is.equal([1, 2, 3], [1, 2, 3]), 'arrays are shallowly equal');
  72. at.ok(is.equal([1, 2, [3, 4]], [1, 2, [3, 4]]), 'arrays are deep equal');
  73. at.notOk(is.equal([1, 2, 3], [5, 2, 3]), 'inequal arrays are not equal');
  74. at.notOk(is.equal([1, 2], [2, 3]), 'inequal arrays are not equal');
  75. at.notOk(is.equal([1, 2, 3], [2, 3]), 'inequal length arrays are not equal');
  76. at.ok(is.equal([], []), 'empty arrays are equal');
  77. var arr = [1, 2];
  78. at.ok(is.equal(arr, arr), 'array is equal to itself');
  79. at.end();
  80. });
  81. t.test('dates', function (dt) {
  82. dt.plan(2);
  83. var now = new Date();
  84. dt.ok(is.equal(now, new Date(now.getTime())), 'two equal date objects are equal');
  85. setTimeout(function () {
  86. dt.notOk(is.equal(now, new Date()), 'two inequal date objects are not equal');
  87. dt.end();
  88. }, 10);
  89. });
  90. t.test('plain objects', function (ot) {
  91. ot.ok(is.equal({ a: 1, b: 2, c: 3 }, { a: 1, b: 2, c: 3 }), 'objects are shallowly equal');
  92. ot.ok(is.equal({ a: { b: 1 } }, { a: { b: 1 } }), 'objects are deep equal');
  93. ot.notOk(is.equal({ a: 1 }, { a: 2 }), 'inequal objects are not equal');
  94. ot.end();
  95. });
  96. t.test('object instances', function (ot) {
  97. var F = function F() {
  98. this.foo = 'bar';
  99. };
  100. F.prototype = {};
  101. var G = function G() {
  102. this.foo = 'bar';
  103. };
  104. var f = new F();
  105. var g = new G();
  106. ot.ok(is.equal(f, f), 'the same object instances are equal');
  107. ot.ok(is.equal(f, new F()), 'two object instances are equal when the prototype and props are the same');
  108. ot.ok(is.equal(f, new G()), 'two object instances are equal when the prototype is not the same, but props are');
  109. g.bar = 'baz';
  110. ot.notOk(is.equal(f, g), 'object instances are not equal when the prototype and props are not the same');
  111. ot.notOk(is.equal(g, f), 'object instances are not equal when the prototype and props are not the same');
  112. ot.end();
  113. });
  114. t.test('functions', function (ft) {
  115. var F = function () {};
  116. F.prototype = {};
  117. var G = function () {};
  118. G.prototype = new Date();
  119. ft.notEqual(F.prototype, G.prototype, 'F and G have different prototypes');
  120. ft.notOk(is.equal(F, G), 'two functions are not equal when the prototype is not the same');
  121. var H = function () {};
  122. H.prototype = F.prototype;
  123. ft.equal(F.prototype, H.prototype, 'F and H have the same prototype');
  124. ft.ok(is.equal(F, H), 'two functions are equal when the prototype is the same');
  125. ft.end();
  126. });
  127. t.end();
  128. });
  129. test('is.hosted', function (t) {
  130. t.ok(is.hosted('a', { a: {} }), 'object is hosted');
  131. t.ok(is.hosted('a', { a: [] }), 'array is hosted');
  132. t.ok(is.hosted('a', { a: function () {} }), 'function is hosted');
  133. t.notOk(is.hosted('a', { a: true }), 'boolean value is not hosted');
  134. t.notOk(is.hosted('a', { a: false }), 'boolean value is not hosted');
  135. t.notOk(is.hosted('a', { a: 3 }), 'number value is not hosted');
  136. t.notOk(is.hosted('a', { a: undefined }), 'undefined value is not hosted');
  137. t.notOk(is.hosted('a', { a: 'abc' }), 'string value is not hosted');
  138. t.notOk(is.hosted('a', { a: null }), 'null value is not hosted');
  139. t.end();
  140. });
  141. test('is.instance', function (t) {
  142. t.ok(is.instance(new Date(), Date), 'new Date is instanceof Date');
  143. var F = function () {};
  144. t.ok(is.instance(new F(), F), 'new constructor is instanceof constructor');
  145. t.end();
  146. });
  147. test('is.nil', function (t) {
  148. var isNull = is.nil;
  149. t.equal(isNull, is['null'], 'is.nil is the same as is.null');
  150. t.ok(isNull(null), 'null is null');
  151. t.notOk(isNull(undefined), 'undefined is not null');
  152. t.notOk(isNull({}), 'object is not null');
  153. t.end();
  154. });
  155. test('is.args', function (t) {
  156. t.notOk(is.args([]), 'array is not arguments');
  157. (function () {
  158. t.ok(is.args(arguments), 'arguments is arguments');
  159. }());
  160. (function () {
  161. t.notOk(is.args(Array.prototype.slice.call(arguments)), 'sliced arguments is not arguments');
  162. }());
  163. var fakeOldArguments = {
  164. callee: function () {},
  165. length: 3
  166. };
  167. t.ok(is.args(fakeOldArguments), 'old-style arguments object is arguments');
  168. t.end();
  169. });
  170. test('is.args.empty', function (t) {
  171. t.notOk(is.args.empty([]), 'empty array is not empty arguments');
  172. (function () {
  173. t.ok(is.args.empty(arguments), 'empty arguments is empty arguments');
  174. }());
  175. (function () {
  176. t.notOk(is.args.empty(Array.prototype.slice.call(arguments)), 'empty sliced arguments is not empty arguments');
  177. }());
  178. t.end();
  179. });
  180. test('is.array', function (t) {
  181. t.ok(is.array([]), 'array is array');
  182. (function () {
  183. t.ok(is.array(Array.prototype.slice.call(arguments)), 'sliced arguments is array');
  184. }());
  185. t.end();
  186. });
  187. test('is.array.empty', function (t) {
  188. t.ok(is.array.empty([]), 'empty array is empty array');
  189. (function () {
  190. t.notOk(is.array.empty(arguments), 'empty arguments is not empty array');
  191. }());
  192. (function () {
  193. t.ok(is.array.empty(Array.prototype.slice.call(arguments)), 'empty sliced arguments is empty array');
  194. }());
  195. t.end();
  196. });
  197. test('is.isarraylike', function (t) {
  198. t.notOk(is.arraylike(), 'undefined is not array-like');
  199. t.notOk(is.arraylike(null), 'null is not array-like');
  200. t.notOk(is.arraylike(false), 'false is not array-like');
  201. t.notOk(is.arraylike(true), 'true is not array-like');
  202. t.ok(is.arraylike({ length: 0 }), 'object with zero length is array-like');
  203. t.ok(is.arraylike({ length: 1 }), 'object with positive length is array-like');
  204. t.notOk(is.arraylike({ length: -1 }), 'object with negative length is not array-like');
  205. t.notOk(is.arraylike({ length: NaN }), 'object with NaN length is not array-like');
  206. t.notOk(is.arraylike({ length: 'foo' }), 'object with string length is not array-like');
  207. t.notOk(is.arraylike({ length: '' }), 'object with empty string length is not array-like');
  208. t.ok(is.arraylike([]), 'array is array-like');
  209. (function () {
  210. t.ok(is.arraylike(arguments), 'empty arguments is array-like');
  211. }());
  212. (function () {
  213. t.ok(is.arraylike(arguments), 'nonempty arguments is array-like');
  214. }(1, 2, 3));
  215. t.end();
  216. });
  217. test('is.bool', function (t) {
  218. t.ok(is.bool(true), 'literal true is a boolean');
  219. t.ok(is.bool(false), 'literal false is a boolean');
  220. t.ok(is.bool(Object(true)), 'object true is a boolean');
  221. t.ok(is.bool(Object(false)), 'object false is a boolean');
  222. t.notOk(is.bool(), 'undefined is not a boolean');
  223. t.notOk(is.bool(null), 'null is not a boolean');
  224. t.end();
  225. });
  226. test('is.false', function (t) {
  227. var isFalse = is['false'];
  228. t.ok(isFalse(false), 'false is false');
  229. t.ok(isFalse(Object(false)), 'object false is false');
  230. t.notOk(isFalse(true), 'true is not false');
  231. t.notOk(isFalse(), 'undefined is not false');
  232. t.notOk(isFalse(null), 'null is not false');
  233. t.notOk(isFalse(''), 'empty string is not false');
  234. t.end();
  235. });
  236. test('is.true', function (t) {
  237. var isTrue = is['true'];
  238. t.ok(isTrue(true), 'true is true');
  239. t.ok(isTrue(Object(true)), 'object true is true');
  240. t.notOk(isTrue(false), 'false is not true');
  241. t.notOk(isTrue(), 'undefined is not true');
  242. t.notOk(isTrue(null), 'null is not true');
  243. t.notOk(isTrue(''), 'empty string is not true');
  244. t.end();
  245. });
  246. test('is.date', function (t) {
  247. t.ok(is.date(new Date()), 'new Date is date');
  248. t.notOk(is.date(), 'undefined is not date');
  249. t.notOk(is.date(null), 'null is not date');
  250. t.notOk(is.date(''), 'empty string is not date');
  251. var nowTS = (new Date()).getTime();
  252. t.notOk(is.date(nowTS), 'timestamp is not date');
  253. var F = function () {};
  254. F.prototype = new Date();
  255. t.notOk(is.date(new F()), 'Date subtype is not date');
  256. t.end();
  257. });
  258. test('is.date.valid', function (t) {
  259. t.ok(is.date.valid(new Date()), 'new Date() is a valid date');
  260. t.notOk(is.date.valid(new Date('')), 'new Date("") is not a valid date');
  261. t.end();
  262. });
  263. test('is.element', function (t) {
  264. t.notOk(is.element(), 'undefined is not element');
  265. t.test('when HTMLElement exists', { skip: typeof HTMLElement === 'undefined' }, function (st) {
  266. var element = document.createElement('div');
  267. st.ok(is.element(element), 'HTMLElement is element');
  268. st.notOk(is.element({ nodeType: 1 }), 'object with nodeType is not element');
  269. st.end();
  270. });
  271. t.end();
  272. });
  273. test('is.error', function (t) {
  274. var err = new Error('foo');
  275. t.ok(is.error(err), 'Error is error');
  276. t.notOk(is.error({}), 'object is not error');
  277. var objWithErrorToString = {
  278. toString: function () {
  279. return '[object Error]';
  280. }
  281. };
  282. t.equal(String(objWithErrorToString), toStr.call(new Error()), 'obj has Error\'s toString');
  283. t.notOk(is.error(objWithErrorToString), 'object with Error\'s toString is not error');
  284. t.end();
  285. });
  286. test('is.fn', function (t) {
  287. t.equal(is['function'], is.fn, 'alias works');
  288. t.ok(is.fn(function () {}), 'function is function');
  289. if (typeof window !== 'undefined') {
  290. // in IE7/8, typeof alert === 'object'
  291. t.ok(is.fn(window.alert), 'window.alert is function');
  292. }
  293. t.notOk(is.fn({}), 'object is not function');
  294. t.notOk(is.fn(null), 'null is not function');
  295. t.test('generator functions', { skip: !genFn }, function (st) {
  296. t.ok(is.fn(genFn), 'generator function is function');
  297. st.end();
  298. });
  299. t.end();
  300. });
  301. test('is.number', function (t) {
  302. t.ok(is.number(0), 'positive zero is number');
  303. t.ok(is.number(0 / -1), 'negative zero is number');
  304. t.ok(is.number(3), 'three is number');
  305. t.ok(is.number(NaN), 'NaN is number');
  306. t.ok(is.number(Infinity), 'infinity is number');
  307. t.ok(is.number(-Infinity), 'negative infinity is number');
  308. t.ok(is.number(Object(42)), 'object number is number');
  309. t.notOk(is.number(), 'undefined is not number');
  310. t.notOk(is.number(null), 'null is not number');
  311. t.notOk(is.number(true), 'true is not number');
  312. t.end();
  313. });
  314. test('is.infinite', function (t) {
  315. t.ok(is.infinite(Infinity), 'positive infinity is infinite');
  316. t.ok(is.infinite(-Infinity), 'negative infinity is infinite');
  317. t.notOk(is.infinite(NaN), 'NaN is not infinite');
  318. t.notOk(is.infinite(0), 'a number is not infinite');
  319. t.end();
  320. });
  321. test('is.decimal', function (t) {
  322. t.ok(is.decimal(1.1), 'decimal is decimal');
  323. t.notOk(is.decimal(0), 'zero is not decimal');
  324. t.notOk(is.decimal(1), 'integer is not decimal');
  325. t.notOk(is.decimal(NaN), 'NaN is not decimal');
  326. t.notOk(is.decimal(Infinity), 'Infinity is not decimal');
  327. t.end();
  328. });
  329. test('is.divisibleBy', function (t) {
  330. t.ok(is.divisibleBy(4, 2), '4 is divisible by 2');
  331. t.ok(is.divisibleBy(4, 2), '4 is divisible by 2');
  332. t.ok(is.divisibleBy(0, 1), '0 is divisible by 1');
  333. t.ok(is.divisibleBy(Infinity, 1), 'infinity is divisible by anything');
  334. t.ok(is.divisibleBy(1, Infinity), 'anything is divisible by infinity');
  335. t.ok(is.divisibleBy(Infinity, Infinity), 'infinity is divisible by infinity');
  336. t.notOk(is.divisibleBy(1, 0), '1 is not divisible by 0');
  337. t.notOk(is.divisibleBy(NaN, 1), 'NaN is not divisible by 1');
  338. t.notOk(is.divisibleBy(1, NaN), '1 is not divisible by NaN');
  339. t.notOk(is.divisibleBy(NaN, NaN), 'NaN is not divisible by NaN');
  340. t.notOk(is.divisibleBy(1, 3), '1 is not divisible by 3');
  341. t.end();
  342. });
  343. test('is.integer', function (t) {
  344. t.ok(is.integer(0), '0 is integer');
  345. t.ok(is.integer(3), '3 is integer');
  346. t.notOk(is.integer(1.1), '1.1 is not integer');
  347. t.notOk(is.integer(NaN), 'NaN is not integer');
  348. t.notOk(is.integer(Infinity), 'infinity is not integer');
  349. t.notOk(is.integer(null), 'null is not integer');
  350. t.notOk(is.integer(), 'undefined is not integer');
  351. t.end();
  352. });
  353. test('is.maximum', function (t) {
  354. t.ok(is.maximum(3, [3, 2, 1]), '3 is maximum of [3,2,1]');
  355. t.ok(is.maximum(3, [1, 2, 3]), '3 is maximum of [1,2,3]');
  356. t.ok(is.maximum(4, [1, 2, 3]), '4 is maximum of [1,2,3]');
  357. t.ok(is.maximum('c', ['a', 'b', 'c']), 'c is maximum of [a,b,c]');
  358. t.notOk(is.maximum(2, [1, 2, 3]), '2 is not maximum of [1,2,3]');
  359. var nanError = new TypeError('NaN is not a valid value');
  360. t['throws'](function () { return is.maximum(NaN); }, nanError, 'throws when first value is NaN');
  361. var error = new TypeError('second argument must be array-like');
  362. t['throws'](function () { return is.maximum(2, null); }, error, 'throws when second value is not array-like');
  363. t['throws'](function () { return is.maximum(2, {}); }, error, 'throws when second value is not array-like');
  364. t.end();
  365. });
  366. test('is.minimum', function (t) {
  367. t.ok(is.minimum(1, [1, 2, 3]), '1 is minimum of [1,2,3]');
  368. t.ok(is.minimum(0, [1, 2, 3]), '0 is minimum of [1,2,3]');
  369. t.ok(is.minimum('a', ['a', 'b', 'c']), 'a is minimum of [a,b,c]');
  370. t.notOk(is.minimum(2, [1, 2, 3]), '2 is not minimum of [1,2,3]');
  371. var nanError = new TypeError('NaN is not a valid value');
  372. t['throws'](function () { return is.minimum(NaN); }, nanError, 'throws when first value is NaN');
  373. var error = new TypeError('second argument must be array-like');
  374. t['throws'](function () { return is.minimum(2, null); }, error, 'throws when second value is not array-like');
  375. t['throws'](function () { return is.minimum(2, {}); }, error, 'throws when second value is not array-like');
  376. t.end();
  377. });
  378. test('is.nan', function (t) {
  379. t.ok(is.nan(NaN), 'NaN is not a number');
  380. t.ok(is.nan('abc'), 'string is not a number');
  381. t.ok(is.nan(true), 'boolean is not a number');
  382. t.ok(is.nan({}), 'object is not a number');
  383. t.ok(is.nan([]), 'array is not a number');
  384. t.ok(is.nan(function () {}), 'function is not a number');
  385. t.notOk(is.nan(0), 'zero is a number');
  386. t.notOk(is.nan(3), 'three is a number');
  387. t.notOk(is.nan(1.1), '1.1 is a number');
  388. t.notOk(is.nan(Infinity), 'infinity is a number');
  389. t.end();
  390. });
  391. test('is.even', function (t) {
  392. t.ok(is.even(0), 'zero is even');
  393. t.ok(is.even(2), 'two is even');
  394. t.ok(is.even(Infinity), 'infinity is even');
  395. t.notOk(is.even(1), '1 is not even');
  396. t.notOk(is.even(), 'undefined is not even');
  397. t.notOk(is.even(null), 'null is not even');
  398. t.notOk(is.even(NaN), 'NaN is not even');
  399. t.end();
  400. });
  401. test('is.odd', function (t) {
  402. t.ok(is.odd(1), 'zero is odd');
  403. t.ok(is.odd(3), 'two is odd');
  404. t.ok(is.odd(Infinity), 'infinity is odd');
  405. t.notOk(is.odd(0), '0 is not odd');
  406. t.notOk(is.odd(2), '2 is not odd');
  407. t.notOk(is.odd(), 'undefined is not odd');
  408. t.notOk(is.odd(null), 'null is not odd');
  409. t.notOk(is.odd(NaN), 'NaN is not odd');
  410. t.end();
  411. });
  412. test('is.ge', function (t) {
  413. t.ok(is.ge(3, 2), '3 is greater than 2');
  414. t.notOk(is.ge(2, 3), '2 is not greater than 3');
  415. t.ok(is.ge(3, 3), '3 is greater than or equal to 3');
  416. t.ok(is.ge('abc', 'a'), 'abc is greater than a');
  417. t.ok(is.ge('abc', 'abc'), 'abc is greater than or equal to abc');
  418. t.notOk(is.ge('a', 'abc'), 'a is not greater than abc');
  419. t.notOk(is.ge(Infinity, 0), 'infinity is not greater than anything');
  420. t.notOk(is.ge(0, Infinity), 'anything is not greater than infinity');
  421. var error = new TypeError('NaN is not a valid value');
  422. t['throws'](function () { return is.ge(NaN, 2); }, error, 'throws when first value is NaN');
  423. t['throws'](function () { return is.ge(2, NaN); }, error, 'throws when second value is NaN');
  424. t.end();
  425. });
  426. test('is.gt', function (t) {
  427. t.ok(is.gt(3, 2), '3 is greater than 2');
  428. t.notOk(is.gt(2, 3), '2 is not greater than 3');
  429. t.notOk(is.gt(3, 3), '3 is not greater than 3');
  430. t.ok(is.gt('abc', 'a'), 'abc is greater than a');
  431. t.notOk(is.gt('abc', 'abc'), 'abc is not greater than abc');
  432. t.notOk(is.gt('a', 'abc'), 'a is not greater than abc');
  433. t.notOk(is.gt(Infinity, 0), 'infinity is not greater than anything');
  434. t.notOk(is.gt(0, Infinity), 'anything is not greater than infinity');
  435. var error = new TypeError('NaN is not a valid value');
  436. t['throws'](function () { return is.gt(NaN, 2); }, error, 'throws when first value is NaN');
  437. t['throws'](function () { return is.gt(2, NaN); }, error, 'throws when second value is NaN');
  438. t.end();
  439. });
  440. test('is.le', function (t) {
  441. t.ok(is.le(2, 3), '2 is lesser than or equal to 3');
  442. t.notOk(is.le(3, 2), '3 is not lesser than or equal to 2');
  443. t.ok(is.le(3, 3), '3 is lesser than or equal to 3');
  444. t.ok(is.le('a', 'abc'), 'a is lesser than or equal to abc');
  445. t.ok(is.le('abc', 'abc'), 'abc is lesser than or equal to abc');
  446. t.notOk(is.le('abc', 'a'), 'abc is not lesser than or equal to a');
  447. t.notOk(is.le(Infinity, 0), 'infinity is not lesser than or equal to anything');
  448. t.notOk(is.le(0, Infinity), 'anything is not lesser than or equal to infinity');
  449. var error = new TypeError('NaN is not a valid value');
  450. t['throws'](function () { return is.le(NaN, 2); }, error, 'throws when first value is NaN');
  451. t['throws'](function () { return is.le(2, NaN); }, error, 'throws when second value is NaN');
  452. t.end();
  453. });
  454. test('is.lt', function (t) {
  455. t.ok(is.lt(2, 3), '2 is lesser than 3');
  456. t.notOk(is.lt(3, 2), '3 is not lesser than 2');
  457. t.notOk(is.lt(3, 3), '3 is not lesser than 3');
  458. t.ok(is.lt('a', 'abc'), 'a is lesser than abc');
  459. t.notOk(is.lt('abc', 'abc'), 'abc is not lesser than abc');
  460. t.notOk(is.lt('abc', 'a'), 'abc is not lesser than a');
  461. t.notOk(is.lt(Infinity, 0), 'infinity is not lesser than anything');
  462. t.notOk(is.lt(0, Infinity), 'anything is not lesser than infinity');
  463. var error = new TypeError('NaN is not a valid value');
  464. t['throws'](function () { return is.lt(NaN, 2); }, error, 'throws when first value is NaN');
  465. t['throws'](function () { return is.lt(2, NaN); }, error, 'throws when second value is NaN');
  466. t.end();
  467. });
  468. test('is.within', function (t) {
  469. t.test('throws on NaN', function (st) {
  470. var nanError = new TypeError('NaN is not a valid value');
  471. st['throws'](function () { return is.within(NaN, 0, 0); }, nanError, 'throws when first value is NaN');
  472. st['throws'](function () { return is.within(0, NaN, 0); }, nanError, 'throws when second value is NaN');
  473. st['throws'](function () { return is.within(0, 0, NaN); }, nanError, 'throws when third value is NaN');
  474. st.end();
  475. });
  476. t.test('throws on non-number', function (st) {
  477. var error = new TypeError('all arguments must be numbers');
  478. st['throws'](function () { return is.within('', 0, 0); }, error, 'throws when first value is string');
  479. st['throws'](function () { return is.within(0, '', 0); }, error, 'throws when second value is string');
  480. st['throws'](function () { return is.within(0, 0, ''); }, error, 'throws when third value is string');
  481. st['throws'](function () { return is.within({}, 0, 0); }, error, 'throws when first value is object');
  482. st['throws'](function () { return is.within(0, {}, 0); }, error, 'throws when second value is object');
  483. st['throws'](function () { return is.within(0, 0, {}); }, error, 'throws when third value is object');
  484. st['throws'](function () { return is.within(null, 0, 0); }, error, 'throws when first value is null');
  485. st['throws'](function () { return is.within(0, null, 0); }, error, 'throws when second value is null');
  486. st['throws'](function () { return is.within(0, 0, null); }, error, 'throws when third value is null');
  487. st['throws'](function () { return is.within(undefined, 0, 0); }, error, 'throws when first value is undefined');
  488. st['throws'](function () { return is.within(0, undefined, 0); }, error, 'throws when second value is undefined');
  489. st['throws'](function () { return is.within(0, 0, undefined); }, error, 'throws when third value is undefined');
  490. st.end();
  491. });
  492. t.ok(is.within(2, 1, 3), '2 is between 1 and 3');
  493. t.ok(is.within(0, -1, 1), '0 is between -1 and 1');
  494. t.ok(is.within(2, 0, Infinity), 'infinity always returns true');
  495. t.ok(is.within(2, Infinity, 2), 'infinity always returns true');
  496. t.ok(is.within(Infinity, 0, 1), 'infinity always returns true');
  497. t.notOk(is.within(2, -1, -1), '2 is not between -1 and 1');
  498. t.end();
  499. });
  500. test('is.object', function (t) {
  501. t.ok(is.object({}), 'object literal is object');
  502. t.notOk(is.object(), 'undefined is not an object');
  503. t.notOk(is.object(null), 'null is not an object');
  504. t.notOk(is.object(true), 'true is not an object');
  505. t.notOk(is.object(''), 'string is not an object');
  506. t.notOk(is.object(NaN), 'NaN is not an object');
  507. t.notOk(is.object(Object), 'object constructor is not an object');
  508. t.notOk(is.object(function () {}), 'function is not an object');
  509. t.test('Symbols', { skip: typeof Symbol !== 'function' }, function (st) {
  510. st.notOk(is.object(Symbol('foo')), 'symbol is not an object');
  511. st.end();
  512. });
  513. t.end();
  514. });
  515. test('is.primitive', function (t) {
  516. t.notOk(is.primitive({}), 'object literal is not a primitive');
  517. t.notOk(is.primitive([]), 'array is not a primitive');
  518. t.ok(is.primitive(), 'undefined is a primitive');
  519. t.ok(is.primitive(null), 'null is a primitive');
  520. t.ok(is.primitive(true), 'true is a primitive');
  521. t.ok(is.primitive(''), 'string is a primitive');
  522. t.ok(is.primitive(NaN), 'NaN is a primitive');
  523. t.notOk(is.primitive(Object), 'object constructor is not a primitive');
  524. t.notOk(is.primitive(function () {}), 'function is not a primitive');
  525. t.test('Symbols', { skip: typeof Symbol !== 'function' }, function (st) {
  526. st.ok(is.primitive(Symbol('foo')), 'symbol is a primitive');
  527. st.end();
  528. });
  529. t.end();
  530. });
  531. test('is.hash', function (t) {
  532. t.ok(is.hash({}), 'empty object literal is hash');
  533. t.ok(is.hash({ 1: 2, a: 'b' }), 'object literal is hash');
  534. t.notOk(is.hash(), 'undefined is not a hash');
  535. t.notOk(is.hash(null), 'null is not a hash');
  536. t.notOk(is.hash(new Date()), 'date is not a hash');
  537. t.notOk(is.hash(Object('')), 'string object is not a hash');
  538. t.notOk(is.hash(''), 'string literal is not a hash');
  539. t.notOk(is.hash(Object(0)), 'number object is not a hash');
  540. t.notOk(is.hash(1), 'number literal is not a hash');
  541. t.notOk(is.hash(true), 'true is not a hash');
  542. t.notOk(is.hash(false), 'false is not a hash');
  543. t.notOk(is.hash(Object(false)), 'boolean obj is not hash');
  544. t.notOk(is.hash(false), 'literal false is not hash');
  545. t.notOk(is.hash(true), 'literal true is not hash');
  546. t.test('commonJS environment', { skip: typeof module === 'undefined' }, function (st) {
  547. st.ok(is.hash(module.exports), 'module.exports is a hash');
  548. st.end();
  549. });
  550. t.test('browser stuff', { skip: typeof window === 'undefined' }, function (st) {
  551. st.notOk(is.hash(window), 'window is not a hash');
  552. st.notOk(is.hash(document.createElement('div')), 'element is not a hash');
  553. st.end();
  554. });
  555. t.test('node stuff', { skip: typeof process === 'undefined' }, function (st) {
  556. st.notOk(is.hash(global), 'global is not a hash');
  557. st.notOk(is.hash(process), 'process is not a hash');
  558. st.end();
  559. });
  560. t.end();
  561. });
  562. test('is.regexp', function (t) {
  563. t.ok(is.regexp(/a/g), 'regex literal is regex');
  564. t.ok(is.regexp(new RegExp('a', 'g')), 'regex object is regex');
  565. t.notOk(is.regexp(), 'undefined is not regex');
  566. t.notOk(is.regexp(function () {}), 'function is not regex');
  567. t.notOk(is.regexp('/a/g'), 'string regex is not regex');
  568. t.end();
  569. });
  570. test('is.string', function (t) {
  571. t.ok(is.string('foo'), 'string literal is string');
  572. t.ok(is.string(Object('foo')), 'string object is string');
  573. t.notOk(is.string(), 'undefined is not string');
  574. t.notOk(is.string(String), 'string constructor is not string');
  575. var F = function () {};
  576. F.prototype = Object('');
  577. t.notOk(is.string(F), 'string subtype is not string');
  578. t.end();
  579. });
  580. test('is.base64', function (t) {
  581. t.ok(is.base64('wxyzWXYZ/+=='), 'string is base64 encoded');
  582. t.ok(is.base64(''), 'zero length string is base64 encoded');
  583. t.notOk(is.base64('wxyzWXYZ123/+=='), 'string length not a multiple of four is not base64 encoded');
  584. t.notOk(is.base64('wxyzWXYZ1234|]=='), 'string with invalid characters is not base64 encoded');
  585. t.notOk(is.base64('wxyzWXYZ1234==/+'), 'string with = not at end is not base64 encoded');
  586. t.notOk(is.base64('wxyzWXYZ1234/==='), 'string ending with === is not base64 encoded');
  587. t.end();
  588. });
  589. test('is.hex', function (t) {
  590. t.ok(is.hex('abcdABCD1234'), 'string is hex encoded');
  591. t.ok(is.hex(''), 'zero length string is hex encoded');
  592. t.notOk(is.hex('wxyzWXYZ1234/+=='), 'string with invalid characters is not hex encoded');
  593. t.end();
  594. });
  595. test('is.symbol', function (t) {
  596. t.test('not symbols', function (st) {
  597. var notSymbols = [true, false, null, undefined, {}, [], function () {}, 42, NaN, Infinity, /a/g, '', 0, -0, new Error('error')];
  598. forEach(notSymbols, function (notSymbol) {
  599. st.notOk(is.symbol(notSymbol), notSymbol + ' is not symbol');
  600. });
  601. st.end();
  602. });
  603. t.test('symbols', { skip: typeof Symbol !== 'function' }, function (st) {
  604. st.ok(is.symbol(Symbol('foo')), 'Symbol("foo") is symbol');
  605. var notKnownSymbols = ['length', 'name', 'arguments', 'caller', 'prototype', 'for', 'keyFor'];
  606. var symbolKeys = Object.getOwnPropertyNames(Symbol).filter(function (name) {
  607. return notKnownSymbols.indexOf(name) < 0;
  608. });
  609. forEach(symbolKeys, function (symbolKey) {
  610. st.ok(is.symbol(Symbol[symbolKey]), symbolKey + ' is symbol');
  611. });
  612. st.end();
  613. });
  614. t.end();
  615. });
  616. test('is.bigint', function (t) {
  617. t.test('not bigints', function (st) {
  618. var notBigints = [true, false, null, undefined, {}, [], function () {}, 42, NaN, Infinity, /a/g, '', 0, -0, new Error('error')];
  619. forEach(notBigints, function (notBigint) {
  620. st.notOk(is.bigint(notBigint), notBigint + ' is not bigint');
  621. });
  622. st.end();
  623. });
  624. t.test('bigints', { skip: typeof BigInt !== 'function' }, function (st) {
  625. var bigInts = [
  626. Function('return 42n')(), // eslint-disable-line no-new-func
  627. BigInt(42)
  628. ];
  629. forEach(bigInts, function (bigInt) {
  630. st.ok(is.bigint(bigInt), bigInt + ' is bigint');
  631. });
  632. st.end();
  633. });
  634. t.end();
  635. });