utils.ts 2.1 KB

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