utils.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. const [datePart, timePart] = dateTime.split(" ");
  67. const [year, month, day] = datePart.split("-");
  68. const formattedDate = `${day}/${month}/${year}`;
  69. if (timePart) {
  70. const [hours, minutes, seconds] = timePart.split(":");
  71. const formattedTime = `${hours}:${minutes}:${seconds}`;
  72. return `${formattedDate} ${formattedTime}`;
  73. }
  74. return formattedDate;
  75. };
  76. /**
  77. * @description Formata a moeda.
  78. * @param {number} value valor.
  79. * @returns {string} valor formatado.
  80. */
  81. const formatToBRLCurrency = (value) => {
  82. if (value != null) {
  83. value = parseFloat(value);
  84. return value.toLocaleString("pt-BR", {
  85. minimumFractionDigits: 2,
  86. style: "currency",
  87. currency: "BRL",
  88. });
  89. }
  90. return value;
  91. };
  92. const normalizeString = (val) =>
  93. val
  94. .toLowerCase()
  95. .normalize("NFKD")
  96. .replace(/[\u0300-\u036f~]/g, "");
  97. const formatPercentage = (value) => {
  98. if (value == null) return "\u2014";
  99. return `${(value * 100).toFixed(0)}%`;
  100. };
  101. export {
  102. formatDateDMYtoYMD,
  103. formatDateYMDtoDMY,
  104. excerpt,
  105. convertDateTime,
  106. formatToBRLCurrency,
  107. normalizeString,
  108. formatPercentage,
  109. };