inference.ts 4.6 KB

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