utils.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { SortOrder } from "antd/lib/table/interface";
  2. import lodash from "lodash";
  3. export function dashboardBasePath(): string {
  4. if (process.env.PUBLIC_URL.includes("http")) {
  5. //for CDN
  6. return process.env.PUBLIC_URL;
  7. } else {
  8. return window.location.origin + process.env.PUBLIC_URL;
  9. }
  10. }
  11. export function fullUrl(url: string): string {
  12. if (process.env.PUBLIC_URL.includes("http")) {
  13. //for CDN
  14. return process.env.PUBLIC_URL + url;
  15. } else {
  16. return window.location.origin + process.env.PUBLIC_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. var 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. };