utils.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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) throw new Error(useI18n().t("validation.rules.date"));
  27. const [day, month, year] = date.split("/");
  28. return `${year}-${month}-${day} ${time ? time : ""}`;
  29. };
  30. /**
  31. * @description Converte uma data e hora para o formato brasileiro.
  32. * @param {string} dateTimeString data e hora.
  33. * @returns {string} data e hora no formato brasileiro.
  34. * @throws {Error} Caso a data seja nula ou invalida.
  35. * @returns {string} data formatada.
  36. * @example
  37. * // convertDateTime("2023-05-23T13:07:27.000000Z");
  38. * // Output: 23/05/2023 10:07:27
  39. */
  40. const convertDateTime = (dateTimeString) => {
  41. const dateTime = new Date(dateTimeString);
  42. const options = {
  43. timeZone: "America/Sao_Paulo",
  44. day: "2-digit",
  45. month: "2-digit",
  46. year: "numeric",
  47. hour: "2-digit",
  48. minute: "2-digit",
  49. second: "2-digit",
  50. };
  51. const formattedDateTime = dateTime
  52. .toLocaleString("pt-BR", options)
  53. .replace(",", "");
  54. return formattedDateTime;
  55. };
  56. /**
  57. * @description Formata uma data de YYYY-MM-DD para DD/MM/YYYY
  58. * @param {string} dateTime data e hora.
  59. * @returns {string} data e hora no formato brasileiro.
  60. * @example
  61. * // formatDateYMDtoDMY("2023-05-23T13:07:27.000000Z");
  62. * // Output: 23/05/2023 10:07:27
  63. */
  64. const formatDateYMDtoDMY = (dateTime) => {
  65. const [datePart, timePart] = dateTime.split(" ");
  66. const [year, month, day] = datePart.split("-");
  67. const formattedDate = `${day}/${month}/${year}`;
  68. if (timePart) {
  69. const [hours, minutes, seconds] = timePart.split(":");
  70. const formattedTime = `${hours}:${minutes}:${seconds}`;
  71. return `${formattedDate} ${formattedTime}`;
  72. }
  73. return formattedDate;
  74. };
  75. /**
  76. * @description Checa a moeda selecionada.
  77. * @param {number} moeda moeda selecionada.
  78. * @returns {object} opções de moeda.
  79. */
  80. const checaMoeda = (moeda) => {
  81. let currencyOptions = {};
  82. if (moeda == 1) {
  83. currencyOptions = {
  84. locale: "pt-BR",
  85. currency: "BRL",
  86. currencyDisplay: "symbol",
  87. hideCurrencySymbolOnFocus: false,
  88. hideGroupingSeparatorOnFocus: false,
  89. hideNegligibleDecimalDigitsOnFocus: false,
  90. autoDecimalDigits: true,
  91. useGrouping: true,
  92. accountingSign: false,
  93. };
  94. } else if (moeda == 2) {
  95. currencyOptions = {
  96. currency: "PYG",
  97. locale: "es-PY",
  98. valueAsInteger: true,
  99. distractionFree: true,
  100. precision: 0,
  101. autoDecimalMode: true,
  102. valueRange: { min: 0 },
  103. allowNegative: true,
  104. };
  105. } else if (moeda == 3) {
  106. currencyOptions = {
  107. locale: "en-US",
  108. currency: "USD",
  109. currencyDisplay: "symbol",
  110. hideCurrencySymbolOnFocus: true,
  111. hideGroupingSeparatorOnFocus: true,
  112. hideNegligibleDecimalDigitsOnFocus: false,
  113. autoDecimalDigits: true,
  114. useGrouping: true,
  115. accountingSign: false,
  116. };
  117. }
  118. return currencyOptions;
  119. };
  120. /**
  121. * @description Filtra a moeda.
  122. * @param {number} value valor.
  123. * @returns {string} valor formatado.
  124. */
  125. const filterCurrency = (value) => {
  126. if (value) {
  127. value = parseFloat(value);
  128. return value.toLocaleString("pt-BR", {
  129. style: "currency",
  130. currency: "BRL",
  131. });
  132. }
  133. return value;
  134. };
  135. /**
  136. * @description Filtra a unidade de medida.
  137. * @param {number} value valor.
  138. * @returns {string} valor formatado.
  139. */
  140. const filterUnidadeMedida = (value) => {
  141. return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
  142. };
  143. /**
  144. * @description Valida se a data é válida.
  145. * @param {string} date data.
  146. * @returns {boolean} true se a data é válida, false caso contrário.
  147. */
  148. const validaData = (date) => {
  149. const regex = /^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[0-2])\/\d{4}$/;
  150. return regex.test(date);
  151. };
  152. /**
  153. * @description Valida se a hora é válida.
  154. * @param {string} time hora.
  155. * @returns {boolean} true se a hora é válida, false caso contrário.
  156. */
  157. const validaHora = (time) => {
  158. const regex = /^([0-1][0-9]|2[0-3]):[0-5][0-9]$/;
  159. return regex.test(time);
  160. };
  161. /**
  162. * @description Valida se a data e hora são válidas.
  163. * @param {string} dataHora data e hora.
  164. * @returns {boolean} true se a data e hora são válidas, false caso contrário.
  165. */
  166. const validaDataHora = (dataHora) => {
  167. const regex =
  168. /^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[0-2])\/\d{4}\s([0-1][0-9]|2[0-3]):[0-5][0-9]$/;
  169. return regex.test(dataHora);
  170. };
  171. /**
  172. * @description Formata a quantidade.
  173. * @param {number} value valor.
  174. * @returns {string} valor formatado.
  175. */
  176. const formatQuantity = (value) => {
  177. if (value) {
  178. return value
  179. .toString()
  180. .replace(/[^0-9]/g, "")
  181. .replace(/\B(?=(\d{3})+(?!\d))/g, ".");
  182. }
  183. return value;
  184. };
  185. /**
  186. * @description Formata a moeda.
  187. * @param {number} value valor.
  188. * @returns {string} valor formatado.
  189. */
  190. const formatCurrency = (value) => {
  191. if (value != null) {
  192. value = parseFloat(value);
  193. return value.toLocaleString("pt-BR", {
  194. minimumFractionDigits: 2,
  195. style: "currency",
  196. currency: "BRL",
  197. });
  198. }
  199. return value;
  200. };
  201. export {
  202. formatDateDMYtoYMD,
  203. formatDateYMDtoDMY,
  204. excerpt,
  205. convertDateTime,
  206. checaMoeda,
  207. filterCurrency,
  208. filterUnidadeMedida,
  209. validaData,
  210. validaHora,
  211. validaDataHora,
  212. formatQuantity,
  213. formatCurrency,
  214. };