utils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { useI18n } from "vue-i18n";
  2. /**
  3. * @description Corta uma string em um determinado tamanho.
  4. * @param {string} string string a ser cortada.
  5. * @param {string} size tamanho da string.
  6. * @returns {string} string cortada.
  7. */
  8. const excerpt = (string, size = 30) => {
  9. if (size == null) return string;
  10. if (string.length > size) {
  11. string = string.substring(0, size) + "...";
  12. }
  13. return string;
  14. };
  15. /**
  16. * @description Formata uma data de DD/MM/YYYY para YYYY-MM-DD
  17. * @param {string} date data.
  18. * @param {string} time tempo.
  19. * @throws {Error} Caso a data seja nula ou invalida.
  20. * @returns {string} data formatada.
  21. */
  22. const formatDateDMYtoYMD = (date, time) => {
  23. if (!date) throw new Error(useI18n().t("validation.rules.required"));
  24. const testDate =
  25. /^([0-2][0-9]|(3)[0-1])(\/)(((0)[0-9])|((1)[0-2]))(\/)\d{4}$/;
  26. if (testDate.test(date) === false)
  27. throw new Error(useI18n().t("validation.rules.date"));
  28. const [day, month, year] = date.split("/");
  29. return `${year}-${month}-${day} ${time ? time : ""}`;
  30. };
  31. /**
  32. * @description Converte uma data e hora para o formato brasileiro.
  33. * @param {string} dateTimeString data e hora.
  34. * @returns {string} data e hora no formato brasileiro.
  35. * @throws {Error} Caso a data seja nula ou invalida.
  36. * @returns {string} data formatada.
  37. * @example
  38. * // convertDateTime("2023-05-23T13:07:27.000000Z");
  39. * // Output: 23/05/2023 10:07:27
  40. */
  41. const convertDateTime = (dateTimeString) => {
  42. const dateTime = new Date(dateTimeString);
  43. const options = {
  44. timeZone: "America/Sao_Paulo",
  45. day: "2-digit",
  46. month: "2-digit",
  47. year: "numeric",
  48. hour: "2-digit",
  49. minute: "2-digit",
  50. second: "2-digit",
  51. };
  52. const formattedDateTime = dateTime
  53. .toLocaleString("pt-BR", options)
  54. .replace(",", "");
  55. return formattedDateTime;
  56. };
  57. /**
  58. * @description Formata uma data de YYYY-MM-DD para DD/MM/YYYY
  59. * @param {string} dateTime data e hora.
  60. * @returns {string} data e hora no formato brasileiro.
  61. * @example
  62. * // formatDateYMDtoDMY("2023-05-23T13:07:27.000000Z");
  63. * // Output: 23/05/2023 10:07:27
  64. */
  65. const formatDateYMDtoDMY = (dateTime) => {
  66. if (!dateTime) return "—";
  67. const [datePart, timePart] = dateTime.split(" ");
  68. const [year, month, day] = datePart.split("-");
  69. const formattedDate = `${day}/${month}/${year}`;
  70. if (timePart) {
  71. const [hours, minutes, seconds] = timePart.split(":");
  72. const formattedTime = `${hours}:${minutes}:${seconds}`;
  73. return `${formattedDate} ${formattedTime}`;
  74. }
  75. return formattedDate;
  76. };
  77. /**
  78. * @description Formata a moeda.
  79. * @param {number} value valor.
  80. * @returns {string} valor formatado.
  81. */
  82. const formatToBRLCurrency = (value) => {
  83. if (value != null) {
  84. value = parseFloat(value);
  85. return value.toLocaleString("pt-BR", {
  86. minimumFractionDigits: 2,
  87. style: "currency",
  88. currency: "BRL",
  89. });
  90. }
  91. return value;
  92. };
  93. const normalizeString = (val) =>
  94. val
  95. .toLowerCase()
  96. .normalize("NFKD")
  97. .replace(/[̀-ͯ~]/g, "");
  98. const getStatusColor = (status) => {
  99. const s = typeof status === "object" ? status?.value : status;
  100. if (s === "active") return "positive";
  101. if (s === "pending") return "warning";
  102. if (s === "inactive") return "inactive";
  103. return "grey-6";
  104. };
  105. const getStatusI18nKey = (status) => {
  106. const s = typeof status === "object" ? status?.value : status;
  107. if (s === "active") return "common.status.active";
  108. if (s === "inactive") return "common.status.inactive";
  109. if (s === "pending") return "common.status.pending";
  110. return "common.status.inactive";
  111. };
  112. export {
  113. formatDateDMYtoYMD,
  114. formatDateYMDtoDMY,
  115. excerpt,
  116. convertDateTime,
  117. formatToBRLCurrency,
  118. normalizeString,
  119. getStatusColor,
  120. getStatusI18nKey,
  121. };