utils.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { SortOrder } from "antd/lib/table/interface";
  2. import lodash from "lodash";
  3. export function fullUrl(url: string): string {
  4. if (process.env.PUBLIC_URL.includes("http")) {
  5. //for CDN
  6. return process.env.PUBLIC_URL + url;
  7. } else {
  8. return window.location.origin + process.env.PUBLIC_URL + url;
  9. }
  10. }
  11. export function PaliToEn(pali: string): string {
  12. let output: string = pali.toLowerCase();
  13. output = output.replaceAll("ā", "a");
  14. output = output.replaceAll("ī", "i");
  15. output = output.replaceAll("ū", "u");
  16. output = output.replaceAll("ḍ", "d");
  17. output = output.replaceAll("ṭ", "t");
  18. output = output.replaceAll("ḷ", "l");
  19. output = output.replaceAll("ṅ", "n");
  20. output = output.replaceAll("ṇ", "n");
  21. output = output.replaceAll("ñ", "n");
  22. output = output.replaceAll("ṃ", "m");
  23. return output;
  24. }
  25. export function PaliReal(inStr: string | undefined | null): string {
  26. if (typeof inStr !== "string") {
  27. return "";
  28. }
  29. const paliLetter = "abcdefghijklmnoprstuvyāīūṅñṭḍṇḷṃ";
  30. let output: string = "";
  31. inStr = inStr.toLowerCase();
  32. inStr = inStr.replace(/ṁ/g, "ṃ");
  33. inStr = inStr.replace(/ŋ/g, "ṃ");
  34. for (const iterator of inStr) {
  35. if (paliLetter.includes(iterator)) {
  36. output += iterator;
  37. }
  38. }
  39. return output;
  40. }
  41. export const getSorterUrl = (sorter?: Record<string, SortOrder>): string => {
  42. let url: string = "";
  43. for (const key in sorter) {
  44. if (Object.prototype.hasOwnProperty.call(sorter, key)) {
  45. const element = sorter[key];
  46. const dir = element === "ascend" ? "asc" : "desc";
  47. let orderby = key;
  48. if (orderby === "updatedAt") {
  49. orderby = "updated_at";
  50. }
  51. url = `&order=${orderby}&dir=${dir}`;
  52. }
  53. }
  54. return url;
  55. };
  56. export const convertToPlain = (html: string): string => {
  57. // Create a new div element
  58. var tempDivElement = document.createElement("div");
  59. // Set the HTML content with the given value
  60. tempDivElement.innerHTML = html;
  61. // Retrieve the text property of the element
  62. return tempDivElement.textContent || tempDivElement.innerText || "";
  63. };
  64. export const randomString = (): string => {
  65. return lodash.times(20, () => lodash.random(35).toString(36)).join("");
  66. };