type/check.ts

  1. /**
  2. * Type tools.
  3. *
  4. * @module type
  5. * @license Apache-2.0
  6. * @copyright Mat. 2018-present
  7. */
  8. /* eslint-disable @typescript-eslint/no-explicit-any */
  9. import type { Fun, JSAnyObj } from "../type/defs";
  10. /**
  11. * Determine if a given value is an `Array`.
  12. *
  13. * @function isArray
  14. * @param {unknown} c
  15. * @returns {Boolean}
  16. */
  17. export const isArray = Array.isArray;
  18. /**
  19. * Determine if a given value is a `Boolean`.
  20. *
  21. * @function isBoolean
  22. * @param {unknown} c
  23. * @returns {Boolean}
  24. */
  25. export const isBoolean = (c: unknown): c is boolean =>
  26. c != null && "boolean" === typeof c;
  27. /**
  28. * Determine if a given value is of `Date` type.
  29. *
  30. * @function isDate
  31. * @param {unknown} c
  32. * @returns {Date}
  33. */
  34. export const isDate = (c: unknown): c is Date => c instanceof Date;
  35. /**
  36. * Determine if a given value is a `Function`.
  37. *
  38. * @function isFunction
  39. * @param {unknown} c
  40. * @returns {Boolean}
  41. */
  42. export const isFunction = (c: unknown): c is Fun =>
  43. c != null && "function" === typeof c;
  44. /**
  45. * Determine if a given value is a proper `Number`
  46. * (not `NaN` and not `Infinity`).
  47. *
  48. * @function isNumber
  49. * @param {unknown} c
  50. * @returns {Boolean}
  51. */
  52. export const isNumber = (c: unknown): c is number =>
  53. c != null && "number" === typeof c &&
  54. !Number.isNaN(c) && Number.isFinite(c);
  55. /**
  56. * Determine if a given value is an `Object`
  57. * (not `null`, not `undefined` and not `Array`).
  58. *
  59. * @function isObject
  60. * @param {unknown} c
  61. * @returns {Boolean}
  62. */
  63. export const isObject = (c: unknown): c is JSAnyObj =>
  64. c != null && "object" === typeof c && !isArray(c);
  65. /**
  66. * Determine if a given value is of `RegExp` type.
  67. *
  68. * @function isRegExp
  69. * @param {unknown} c
  70. * @returns {RegExp}
  71. */
  72. export const isRegExp = (c: unknown): c is RegExp => c instanceof RegExp;
  73. /**
  74. * Determine if a given value is a `String`.
  75. *
  76. * @function isString
  77. * @param {unknown} c
  78. * @returns {Boolean}
  79. */
  80. export const isString = (c: unknown): c is string =>
  81. c != null && "string" === typeof c;