utils.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import type { SortOrder } from "antd/lib/table/interface";
  2. import lodash from "lodash";
  3. export function dashboardBasePath(): string {
  4. if (import.meta.env.BASE_URL.includes("http")) {
  5. //for CDN
  6. return import.meta.env.BASE_URL;
  7. } else {
  8. return window.location.origin + import.meta.env.BASE_URL;
  9. }
  10. }
  11. export function fullUrl(url: string): string {
  12. if (import.meta.env.BASE_URL.includes("http")) {
  13. //for CDN
  14. return import.meta.env.BASE_URL + url;
  15. } else {
  16. return window.location.origin + import.meta.env.BASE_URL + url;
  17. }
  18. }
  19. export function PaliToEn(pali: string): string {
  20. let output: string = pali.toLowerCase();
  21. output = output.replaceAll("ā", "a");
  22. output = output.replaceAll("ī", "i");
  23. output = output.replaceAll("ū", "u");
  24. output = output.replaceAll("ḍ", "d");
  25. output = output.replaceAll("ṭ", "t");
  26. output = output.replaceAll("ḷ", "l");
  27. output = output.replaceAll("ṅ", "n");
  28. output = output.replaceAll("ṇ", "n");
  29. output = output.replaceAll("ñ", "n");
  30. output = output.replaceAll("ṃ", "m");
  31. return output;
  32. }
  33. export function PaliReal(inStr: string | undefined | null): string {
  34. if (typeof inStr !== "string") {
  35. return "";
  36. }
  37. const paliLetter = "abcdefghijklmnoprstuvyāīūṅñṭḍṇḷṃ";
  38. let output: string = "";
  39. inStr = inStr.toLowerCase();
  40. inStr = inStr.replace(/ṁ/g, "ṃ");
  41. inStr = inStr.replace(/ŋ/g, "ṃ");
  42. for (const iterator of inStr) {
  43. if (paliLetter.includes(iterator)) {
  44. output += iterator;
  45. }
  46. }
  47. return output;
  48. }
  49. export const getSorterUrl = (sorter?: Record<string, SortOrder>): string => {
  50. let url: string = "";
  51. for (const key in sorter) {
  52. if (Object.prototype.hasOwnProperty.call(sorter, key)) {
  53. const element = sorter[key];
  54. const dir = element === "ascend" ? "asc" : "desc";
  55. let orderby = key;
  56. if (orderby === "updatedAt") {
  57. orderby = "updated_at";
  58. }
  59. url = `&order=${orderby}&dir=${dir}`;
  60. }
  61. }
  62. return url;
  63. };
  64. export const convertToPlain = (html: string): string => {
  65. // Create a new div element
  66. const tempDivElement = document.createElement("div");
  67. // Set the HTML content with the given value
  68. tempDivElement.innerHTML = html;
  69. // Retrieve the text property of the element
  70. return tempDivElement.textContent || tempDivElement.innerText || "";
  71. };
  72. export const randomString = (): string => {
  73. return lodash.times(20, () => lodash.random(35).toString(36)).join("");
  74. };
  75. /**
  76. * 10进制数字转为16进制字符串
  77. * @param {number} arg
  78. * @returns
  79. */
  80. /*
  81. 作者:sq800
  82. 链接:https://juejin.cn/post/7250029395024281656
  83. 来源:稀土掘金
  84. 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  85. */
  86. export const numToHex = (arg: number) => {
  87. try {
  88. const a = arg.toString(16).toUpperCase();
  89. return a.length % 2 === 1 ? "0" + a : a;
  90. } catch (e) {
  91. console.warn("数字转16进制出错:", e);
  92. }
  93. };
  94. export const scrollToTop = () => {
  95. document.getElementById("article-root")?.scrollIntoView();
  96. };