index.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. export function debounce(fn: Function, delay = 200, immediate = false) {
  2. let timer: ReturnType<typeof setTimeout>;
  3. return function (...args: any) {
  4. const context = globalThis;
  5. let result: unknown;
  6. args = arguments;
  7. if (timer) {
  8. clearTimeout(timer);
  9. }
  10. if (immediate && !timer) {
  11. result = fn.apply(context, args);
  12. }
  13. timer = setTimeout(() => {
  14. result = fn.apply(context, args);
  15. }, delay);
  16. return result;
  17. };
  18. }
  19. export const phoneConvert = (number: string) => {
  20. if (!number) return "";
  21. let pat = /(\d{3})\d*(\d{3})/;
  22. return number.replace(pat, "$1****$2");
  23. };
  24. /**
  25. * @description 根据语言获取时区
  26. */
  27. const language = ["pt-BR", "zh-CN", "en-US"];
  28. function getLang(utc: string) {
  29. let reg = new RegExp(utc, "i");
  30. const i = language.findIndex((n, index) => {
  31. if (reg.test(n)) {
  32. return index;
  33. }
  34. });
  35. let n = i < 0 ? 0 : i;
  36. return language[n];
  37. }
  38. export const timeFormat = (
  39. time: number,
  40. lang = "en",
  41. delimiter?: undefined | string,
  42. day = false
  43. ): string => {
  44. if (!time) return "";
  45. const len = (time + "").length;
  46. if (len === 10) {
  47. time = time * 1000;
  48. }
  49. lang = getLang(lang);
  50. if (delimiter) {
  51. return new Date(time)
  52. .toLocaleString(lang, {
  53. hour12: false,
  54. })
  55. .replaceAll("/", delimiter);
  56. }
  57. if (day) {
  58. return new Date(time).toLocaleString(lang, {
  59. year: "2-digit",
  60. month: "2-digit",
  61. day: "2-digit",
  62. hour12: false,
  63. });
  64. }
  65. return new Date(time).toLocaleString(lang, {
  66. year: "2-digit",
  67. month: "2-digit",
  68. day: "2-digit",
  69. hour: "2-digit",
  70. minute: "2-digit",
  71. hour12: false,
  72. });
  73. };
  74. /**
  75. * @description 复制文本
  76. */
  77. export let copyText = function (text: string) {
  78. if (navigator.clipboard) {
  79. copyText = (text: string) => {
  80. navigator.clipboard.writeText(text);
  81. };
  82. } else {
  83. copyText = (text) => {
  84. const input = document.createElement("input");
  85. input.setAttribute("value", text);
  86. document.body.appendChild(input);
  87. input.select();
  88. document.execCommand("copy");
  89. document.body.removeChild(input);
  90. };
  91. }
  92. copyText(text);
  93. };
  94. /**
  95. * @description 两个数值计算百分比
  96. * @param {current} 当前值
  97. * @param {source} 总值
  98. * @return number
  99. */
  100. export const percentage = (current: number, source: number) => {
  101. if (!source) return 0;
  102. const num = (current * 100) / source;
  103. return num > 100 ? 100 : flatPoint(num);
  104. };
  105. /**
  106. * @description 保留两位数浮点数
  107. */
  108. export const flatPoint = (num: number) => {
  109. return Number(num.toFixed(2));
  110. };
  111. /**
  112. * @description 是否是移动端
  113. */
  114. export function isMobile(): boolean {
  115. try {
  116. document.createEvent("TouchEvent");
  117. return true;
  118. } catch (e) {
  119. return false;
  120. }
  121. }