ava.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. import test from 'ava'
  2. import {
  3. isError,
  4. isEmptyArray,
  5. isObject,
  6. isPlainObject,
  7. isAnyObject,
  8. isUndefined,
  9. isNull,
  10. isNullOrUndefined,
  11. isFunction,
  12. isArray,
  13. isString,
  14. isEmptyString,
  15. isFullString,
  16. isBoolean,
  17. isRegExp,
  18. isNumber,
  19. isDate,
  20. isSymbol,
  21. isPrimitive,
  22. isType,
  23. isMap,
  24. isWeakMap,
  25. isSet,
  26. isWeakSet,
  27. isFullArray,
  28. // isBlob,
  29. // isFile,
  30. isPromise,
  31. isNaNValue,
  32. isEmptyObject,
  33. isOneOf,
  34. isFullObject,
  35. } from '../src/index'
  36. // const blob = Buffer.from([])
  37. test('Basic true tests', t => {
  38. t.is(isError(new Error('')), true)
  39. t.is(isUndefined(undefined), true)
  40. t.is(isNull(null), true)
  41. t.is(isNullOrUndefined(null), true)
  42. t.is(isNullOrUndefined(undefined), true)
  43. t.is(isObject({}), true)
  44. t.is(isEmptyObject({}), true)
  45. t.is(isFullObject({0: ''}), true)
  46. t.is(isFullObject({'': ''}), true)
  47. t.is(isObject(new Object()), true)
  48. t.is(isArray([]), true)
  49. t.is(isEmptyArray([]), true)
  50. t.is(isFullArray(['']), true)
  51. t.is(isArray(new Array()), true)
  52. t.is(isString(''), true)
  53. t.is(isString('_'), true)
  54. t.is(isEmptyString(''), true)
  55. t.is(isFullString(' '), true)
  56. t.is(isBoolean(true), true)
  57. t.is(isBoolean(false), true)
  58. t.is(isRegExp(/./), true)
  59. t.is(isRegExp(/./gi), true)
  60. t.is(isNumber(0), true)
  61. t.is(isNumber(1), true)
  62. t.is(isDate(new Date()), true)
  63. t.is(isSymbol(Symbol()), true)
  64. t.is(isMap(new Map()), true)
  65. t.is(isWeakMap(new WeakMap()), true)
  66. t.is(isSet(new Set()), true)
  67. t.is(isWeakSet(new WeakSet()), true)
  68. // t.is(isBlob(blob), true)
  69. // t.is(isFile(new File([''], '', { type: 'text/html' })), true)
  70. t.is(isPromise(new Promise((resolve, reject) => {})), true)
  71. })
  72. test('Basic false tests', t => {
  73. t.is(isError({}), false)
  74. t.is(isNumber(NaN), false)
  75. t.is(isDate(new Date('_')), false)
  76. t.is(isDate(NaN), false)
  77. t.is(isUndefined(NaN), false)
  78. t.is(isNull(NaN), false)
  79. t.is(isObject(NaN), false)
  80. t.is(isArray(NaN), false)
  81. t.is(isString(NaN), false)
  82. t.is(isEmptyString(' '), false)
  83. t.is(isFullString(''), false)
  84. t.is(isBoolean(NaN), false)
  85. t.is(isRegExp(NaN), false)
  86. t.is(isSymbol(NaN), false)
  87. t.is(isMap(new WeakMap()), false)
  88. t.is(isWeakMap(new Map()), false)
  89. t.is(isSet(new WeakSet()), false)
  90. t.is(isWeakSet(new Set()), false)
  91. t.is(isNullOrUndefined(NaN), false)
  92. })
  93. test('isFunction', t => {
  94. t.is(isFunction(NaN), false)
  95. t.is(isFunction(() => {}), true)
  96. t.is(isFunction(function () {}), true)
  97. t.is(isFunction(async () => {}), true)
  98. t.is(isFunction(async function () {}), true)
  99. t.is(isFunction(function * () {}), true)
  100. t.is(isFunction(async function * () {}), true)
  101. const _ = { fn: () => {}, method () {} }
  102. t.is(isFunction(_.fn), true)
  103. t.is(isFunction(_.method), true)
  104. })
  105. test('isEmptyObject', t => {
  106. t.is(isEmptyObject({}), true)
  107. t.is(isEmptyObject(new Object()), true)
  108. t.is(isEmptyObject('{}'), false)
  109. t.is(isEmptyObject('{}'), false)
  110. t.is(isEmptyObject(null), false)
  111. t.is(isEmptyObject(new Date()), false)
  112. t.is(isEmptyObject(new Error('')), false)
  113. t.is(isEmptyObject(new Date()), false)
  114. t.is(isEmptyObject(Symbol()), false)
  115. t.is(isEmptyObject(new Map()), false)
  116. t.is(isEmptyObject(new WeakMap()), false)
  117. t.is(isEmptyObject(new Set()), false)
  118. t.is(isEmptyObject(new WeakSet()), false)
  119. })
  120. test('isEmptyArray', t => {
  121. t.is(isEmptyArray([]), true)
  122. t.is(isEmptyArray(new Array()), true)
  123. t.is(isEmptyArray(new Array(0)), true)
  124. t.is(isEmptyArray(new Array(1)), false)
  125. t.is(isEmptyArray([undefined]), false)
  126. t.is(isEmptyArray(null), false)
  127. t.is(isEmptyArray(new Date()), false)
  128. t.is(isEmptyArray(new Error('')), false)
  129. t.is(isEmptyArray(new Date()), false)
  130. t.is(isEmptyArray(Symbol()), false)
  131. t.is(isEmptyArray(new Map()), false)
  132. t.is(isEmptyArray(new WeakMap()), false)
  133. t.is(isEmptyArray(new Set()), false)
  134. t.is(isEmptyArray(new WeakSet()), false)
  135. })
  136. test('isFullArray', t => {
  137. t.is(isFullArray(new Array(1)), true)
  138. t.is(isFullArray([undefined]), true)
  139. t.is(isFullArray([null]), true)
  140. t.is(isFullArray(['']), true)
  141. t.is(isFullArray([]), false)
  142. t.is(isFullArray(new Array()), false)
  143. t.is(isFullArray(new Array(0)), false)
  144. t.is(isFullArray(null), false)
  145. t.is(isFullArray(new Date()), false)
  146. t.is(isFullArray(new Error('')), false)
  147. t.is(isFullArray(new Date()), false)
  148. t.is(isFullArray(Symbol()), false)
  149. t.is(isFullArray(new Map()), false)
  150. t.is(isFullArray(new WeakMap()), false)
  151. t.is(isFullArray(new Set()), false)
  152. t.is(isFullArray(new WeakSet()), false)
  153. })
  154. test('NaN tests', t => {
  155. t.is(isNaNValue(NaN), true)
  156. t.is(isNaNValue(new Error('')), false)
  157. t.is(isNaNValue(undefined), false)
  158. t.is(isNaNValue(null), false)
  159. t.is(isNaNValue(undefined), false)
  160. t.is(isNaNValue({}), false)
  161. t.is(isNaNValue(new Object()), false)
  162. t.is(
  163. isNaNValue(() => {}),
  164. false
  165. )
  166. t.is(isNaNValue([]), false)
  167. t.is(isNaNValue(new Array()), false)
  168. t.is(isNaNValue(''), false)
  169. t.is(isNaNValue('_'), false)
  170. t.is(isNaNValue(''), false)
  171. t.is(isNaNValue(' '), false)
  172. t.is(isNaNValue(true), false)
  173. t.is(isNaNValue(false), false)
  174. t.is(isNaNValue(/./), false)
  175. t.is(isNaNValue(/./gi), false)
  176. t.is(isNaNValue(0), false)
  177. t.is(isNaNValue(1), false)
  178. t.is(isNaNValue(new Date()), false)
  179. t.is(isNaNValue(Symbol()), false)
  180. t.is(isNaNValue(new Map()), false)
  181. t.is(isNaNValue(new WeakMap()), false)
  182. t.is(isNaNValue(new Set()), false)
  183. t.is(isNaNValue(new WeakSet()), false)
  184. t.is(isNaNValue(new Promise((resolve, reject) => {})), false)
  185. })
  186. test('Primitive tests', t => {
  187. // true
  188. t.is(isPrimitive(0), true)
  189. t.is(isPrimitive(''), true)
  190. t.is(isPrimitive('str'), true)
  191. t.is(isPrimitive(Symbol()), true)
  192. t.is(isPrimitive(true), true)
  193. t.is(isPrimitive(false), true)
  194. t.is(isPrimitive(null), true)
  195. t.is(isPrimitive(undefined), true)
  196. // false
  197. t.is(isPrimitive(NaN), false)
  198. t.is(isPrimitive([]), false)
  199. t.is(isPrimitive(new Array()), false)
  200. t.is(isPrimitive({}), false)
  201. t.is(isPrimitive(new Object()), false)
  202. t.is(isPrimitive(new Date()), false)
  203. t.is(
  204. isPrimitive(() => {}),
  205. false
  206. )
  207. })
  208. test('Date exception', t => {
  209. t.is(isDate(new Date('_')), false)
  210. })
  211. test('Generic isType', t => {
  212. // -----------------------------
  213. // This is correct old fashion syntax for classes, if this is missing
  214. function MyClass () {}
  215. MyClass.prototype.constructor = MyClass
  216. // @ts-ignore
  217. const myClass = new MyClass()
  218. // -----------------------------
  219. class MyOtherClass { constructor() {} }
  220. // this is expected behaviour
  221. t.is(isType('', String), true)
  222. t.is(isType('_', String), true)
  223. t.is(isType('Hello World', String), true)
  224. t.is(isType(NaN, Number), true)
  225. t.is(isType(0, Number), true)
  226. t.is(isType(1, Number), true)
  227. t.is(isType({}, Object), true)
  228. t.is(isType(new Object(), Object), true)
  229. t.is(isType([], Array), true)
  230. t.is(isType(new Array(), Array), true)
  231. t.is(
  232. isType(() => {}, Function),
  233. true
  234. )
  235. t.is(isType(true, Boolean), true)
  236. t.is(isType(false, Boolean), true)
  237. t.is(isType(new Date('_'), Date), true)
  238. t.is(isType(new Date(), Date), true)
  239. t.is(isType(/./, RegExp), true)
  240. t.is(isType(/./gi, RegExp), true)
  241. t.is(isType(myClass, MyClass), true)
  242. t.is(isType(new MyOtherClass(), MyOtherClass), true)
  243. t.is(isType(myClass, MyOtherClass), false)
  244. t.is(isType(Symbol(), Symbol), true)
  245. // t.is(isType(null, Null), true)
  246. // t.is(isType(undefined, Undefined), true)
  247. // It SHOULD fail
  248. t.is(isType(5, String), false)
  249. t.is(isType(null, Object), false)
  250. // Not sure if this would be the expected behaviour but everything is an object
  251. // so I would say so
  252. t.is(isType(myClass, Object), true)
  253. })
  254. test('isObject vs isAnyObject', t => {
  255. // -----------------------------
  256. // This is correct old fashion syntax for classes, if this is missing
  257. function MyClass () {}
  258. MyClass.prototype.constructor = MyClass
  259. // @ts-ignore
  260. const myClass = new MyClass()
  261. // -----------------------------
  262. class MyClass2 { constructor() {} }
  263. const myClass2 = new MyClass2()
  264. const mySpecialObject = {}
  265. Object.setPrototypeOf(mySpecialObject, {
  266. toDate: function () {
  267. return new Date()
  268. },
  269. })
  270. // IS OBJECT
  271. // plain object
  272. t.is(isObject({}), true)
  273. t.is(isObject(new Object()), true)
  274. t.is(isPlainObject({}), true)
  275. t.is(isPlainObject(new Object()), true)
  276. // classes & prototypes
  277. t.is(isObject(myClass), false)
  278. t.is(isObject(myClass2), false)
  279. t.is(isObject(mySpecialObject), false)
  280. t.is(isPlainObject(myClass), false)
  281. t.is(isPlainObject(myClass2), false)
  282. t.is(isPlainObject(mySpecialObject), false)
  283. // arrays and dates
  284. t.is(isObject([]), false)
  285. t.is(isObject(new Array()), false)
  286. t.is(isObject(new Date('_')), false)
  287. t.is(isObject(new Date()), false)
  288. t.is(isPlainObject([]), false)
  289. t.is(isPlainObject(new Array()), false)
  290. t.is(isPlainObject(new Date('_')), false)
  291. t.is(isPlainObject(new Date()), false)
  292. // IS ANY OBJECT
  293. // plain object
  294. t.is(isAnyObject({}), true)
  295. t.is(isAnyObject(new Object()), true)
  296. // classes & prototypes
  297. t.is(isAnyObject(myClass), true)
  298. t.is(isAnyObject(myClass2), true)
  299. t.is(isAnyObject(mySpecialObject), true)
  300. // arrays and dates
  301. t.is(isAnyObject([]), false)
  302. t.is(isAnyObject(new Array()), false)
  303. t.is(isAnyObject(new Date('_')), false)
  304. t.is(isAnyObject(new Date()), false)
  305. })
  306. test('isOneOf', t => {
  307. t.is(isOneOf(isString, isNumber)('_'), true)
  308. t.is(isOneOf(isString, isNumber)(1), true)
  309. t.is(isOneOf(isString, isNumber)(undefined), false)
  310. t.is(isOneOf(isString, isNumber, isBoolean)('_'), true)
  311. t.is(isOneOf(isString, isNumber, isBoolean)(1), true)
  312. t.is(isOneOf(isString, isNumber, isBoolean)(true), true)
  313. t.is(isOneOf(isString, isNumber, isBoolean)(undefined), false)
  314. t.is(isOneOf(isString, isNumber, isBoolean, isArray)('_'), true)
  315. t.is(isOneOf(isString, isNumber, isBoolean, isArray)(1), true)
  316. t.is(isOneOf(isString, isNumber, isBoolean, isArray)(true), true)
  317. t.is(isOneOf(isString, isNumber, isBoolean, isArray)([]), true)
  318. t.is(isOneOf(isString, isNumber, isBoolean, isArray)(undefined), false)
  319. t.is(isOneOf(isString, isNumber, isBoolean, isArray, isPlainObject)('_'), true)
  320. t.is(isOneOf(isString, isNumber, isBoolean, isArray, isPlainObject)(1), true)
  321. t.is(isOneOf(isString, isNumber, isBoolean, isArray, isPlainObject)(true), true)
  322. t.is(isOneOf(isString, isNumber, isBoolean, isArray, isPlainObject)([]), true)
  323. t.is(isOneOf(isString, isNumber, isBoolean, isArray, isPlainObject)({}), true)
  324. t.is(isOneOf(isString, isNumber, isBoolean, isArray, isPlainObject)(undefined), false)
  325. })
  326. test('type related tests', t => {
  327. t.pass()
  328. // const fn: string | ((k: number) => string) = (p) => 'a'
  329. // if (!isFunction(fn)) {
  330. // fn
  331. // }
  332. // const a: Record<string, number> = {}
  333. // a[fn(1)] = fn(2)
  334. // const myArray: string | string[] = ['a', 'b']
  335. // if (!isArray(myArray)) {
  336. // myArray
  337. // }
  338. // const a: Record<string, number> = {}
  339. // a[myArray[1]] = myArray[0]
  340. // const myArray: string | any[] = [1, 2, 'a', 'b']
  341. // if (!isArray(myArray)) {
  342. // myArray
  343. // }
  344. // const a: Record<string, number> = {}
  345. // a[myArray[1]] = myArray[0]
  346. })