utils.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. export function ApiFetch(
  2. url: string,
  3. method = "GET",
  4. data?: any
  5. ): Promise<Response> {
  6. const apiHost = process.env.REACT_APP_API_HOST;
  7. interface ajaxParam {
  8. method: string;
  9. body?: string;
  10. headers?: any;
  11. }
  12. let param: ajaxParam = {
  13. method: method,
  14. };
  15. if (typeof data !== "undefined") {
  16. param.body = JSON.stringify(data);
  17. }
  18. if (localStorage.getItem("token")) {
  19. param.headers = { token: localStorage.getItem("token") };
  20. }
  21. return new Promise((resolve, reject) => {
  22. let apiUrl = apiHost + url;
  23. console.log("api", apiUrl);
  24. fetch(apiUrl, param)
  25. .then((response) => response.json())
  26. .then((response) => {
  27. resolve(response);
  28. })
  29. .catch((error) => {
  30. reject(error);
  31. });
  32. });
  33. }
  34. export function ApiGetText(url: string): Promise<String> {
  35. const apiHost = process.env.REACT_APP_API_HOST
  36. ? process.env.REACT_APP_API_HOST
  37. : "http://localhost/api";
  38. return new Promise((resolve, reject) => {
  39. let apiUrl = apiHost + url;
  40. console.log("api", apiUrl);
  41. fetch(apiUrl)
  42. .then((response) => response.text())
  43. .then((response) => {
  44. resolve(response);
  45. })
  46. .catch((error) => {
  47. reject(error);
  48. });
  49. });
  50. }
  51. export function PaliToEn(pali: string): string {
  52. let output: string = pali.toLowerCase();
  53. output = output.replaceAll(" ", "_");
  54. output = output.replaceAll("-", "_");
  55. output = output.replaceAll("ā", "a");
  56. output = output.replaceAll("ī", "i");
  57. output = output.replaceAll("ū", "u");
  58. output = output.replaceAll("ḍ", "d");
  59. output = output.replaceAll("ṭ", "t");
  60. output = output.replaceAll("ḷ", "l");
  61. return output;
  62. }
  63. export function PaliReal(inStr: string): string {
  64. if (typeof inStr === "undefined") {
  65. return "";
  66. }
  67. const paliLetter = "abcdefghijklmnoprstuvyāīūṅñṭḍṇḷṃ";
  68. let output: string = "";
  69. inStr = inStr.toLowerCase();
  70. inStr = inStr.replace(/ṁ/g, "ṃ");
  71. inStr = inStr.replace(/ŋ/g, "ṃ");
  72. for (const iterator of inStr) {
  73. if (paliLetter.includes(iterator)) {
  74. output += iterator;
  75. }
  76. }
  77. return output;
  78. }