inference.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { isFunction, isObject, isString } from '@vue/shared';
  2. /**
  3. * 检查传入的值是否为undefined。
  4. *
  5. * @param {unknown} value 要检查的值。
  6. * @returns {boolean} 如果值是undefined,返回true,否则返回false。
  7. */
  8. function isUndefined(value?: unknown): value is undefined {
  9. return value === undefined;
  10. }
  11. /**
  12. * 检查传入的值是否为boolean
  13. * @param value
  14. * @returns 如果值是布尔值,返回true,否则返回false。
  15. */
  16. function isBoolean(value: unknown): value is boolean {
  17. return typeof value === 'boolean';
  18. }
  19. /**
  20. * 检查传入的值是否为空。
  21. *
  22. * 以下情况将被认为是空:
  23. * - 值为null。
  24. * - 值为undefined。
  25. * - 值为一个空字符串。
  26. * - 值为一个长度为0的数组。
  27. * - 值为一个没有元素的Map或Set。
  28. * - 值为一个没有属性的对象。
  29. *
  30. * @param {T} value 要检查的值。
  31. * @returns {boolean} 如果值为空,返回true,否则返回false。
  32. */
  33. function isEmpty<T = unknown>(value?: T): value is T {
  34. if (value === null || value === undefined) {
  35. return true;
  36. }
  37. if (Array.isArray(value) || isString(value)) {
  38. return value.length === 0;
  39. }
  40. if (value instanceof Map || value instanceof Set) {
  41. return value.size === 0;
  42. }
  43. if (isObject(value)) {
  44. return Object.keys(value).length === 0;
  45. }
  46. return false;
  47. }
  48. /**
  49. * 检查传入的字符串是否为有效的HTTP或HTTPS URL。
  50. *
  51. * @param {string} url 要检查的字符串。
  52. * @return {boolean} 如果字符串是有效的HTTP或HTTPS URL,返回true,否则返回false。
  53. */
  54. function isHttpUrl(url?: string): boolean {
  55. if (!url) {
  56. return false;
  57. }
  58. // 使用正则表达式测试URL是否以http:// 或 https:// 开头
  59. const httpRegex = /^https?:\/\/.*$/;
  60. return httpRegex.test(url);
  61. }
  62. /**
  63. * 检查传入的值是否为window对象。
  64. *
  65. * @param {any} value 要检查的值。
  66. * @returns {boolean} 如果值是window对象,返回true,否则返回false。
  67. */
  68. function isWindow(value: any): value is Window {
  69. return (
  70. typeof window !== 'undefined' && value !== null && value === value.window
  71. );
  72. }
  73. /**
  74. * 检查当前运行环境是否为Mac OS。
  75. *
  76. * 这个函数通过检查navigator.userAgent字符串来判断当前运行环境。
  77. * 如果userAgent字符串中包含"macintosh"或"mac os x"(不区分大小写),则认为当前环境是Mac OS。
  78. *
  79. * @returns {boolean} 如果当前环境是Mac OS,返回true,否则返回false。
  80. */
  81. function isMacOs(): boolean {
  82. const macRegex = /macintosh|mac os x/i;
  83. return macRegex.test(navigator.userAgent);
  84. }
  85. /**
  86. * 检查当前运行环境是否为Windows OS。
  87. *
  88. * 这个函数通过检查navigator.userAgent字符串来判断当前运行环境。
  89. * 如果userAgent字符串中包含"windows"或"win32"(不区分大小写),则认为当前环境是Windows OS。
  90. *
  91. * @returns {boolean} 如果当前环境是Windows OS,返回true,否则返回false。
  92. */
  93. function isWindowsOs(): boolean {
  94. const windowsRegex = /windows|win32/i;
  95. return windowsRegex.test(navigator.userAgent);
  96. }
  97. /**
  98. * 检查传入的值是否为数字
  99. * @param value
  100. */
  101. function isNumber(value: any): value is number {
  102. return typeof value === 'number' && Number.isFinite(value);
  103. }
  104. /**
  105. * Returns the first value in the provided list that is neither `null` nor `undefined`.
  106. *
  107. * This function iterates over the input values and returns the first one that is
  108. * not strictly equal to `null` or `undefined`. If all values are either `null` or
  109. * `undefined`, it returns `undefined`.
  110. *
  111. * @template T - The type of the input values.
  112. * @param {...(T | null | undefined)[]} values - A list of values to evaluate.
  113. * @returns {T | undefined} - The first value that is not `null` or `undefined`, or `undefined` if none are found.
  114. *
  115. * @example
  116. * // Returns 42 because it is the first non-null, non-undefined value.
  117. * getFirstNonNullOrUndefined(undefined, null, 42, 'hello'); // 42
  118. *
  119. * @example
  120. * // Returns 'hello' because it is the first non-null, non-undefined value.
  121. * getFirstNonNullOrUndefined(null, undefined, 'hello', 123); // 'hello'
  122. *
  123. * @example
  124. * // Returns undefined because all values are either null or undefined.
  125. * getFirstNonNullOrUndefined(undefined, null); // undefined
  126. */
  127. function getFirstNonNullOrUndefined<T>(
  128. ...values: (null | T | undefined)[]
  129. ): T | undefined {
  130. for (const value of values) {
  131. if (value !== undefined && value !== null) {
  132. return value;
  133. }
  134. }
  135. return undefined;
  136. }
  137. export {
  138. getFirstNonNullOrUndefined,
  139. isBoolean,
  140. isEmpty,
  141. isFunction,
  142. isHttpUrl,
  143. isMacOs,
  144. isNumber,
  145. isObject,
  146. isString,
  147. isUndefined,
  148. isWindow,
  149. isWindowsOs,
  150. };