utils.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import { useI18n } from "vue-i18n";
  2. const checaMoeda = (moeda) => {
  3. let currencyOptions = {};
  4. if (moeda == 1) {
  5. currencyOptions = {
  6. locale: "pt-BR",
  7. currency: "BRL",
  8. currencyDisplay: "symbol",
  9. hideCurrencySymbolOnFocus: false,
  10. hideGroupingSeparatorOnFocus: false,
  11. hideNegligibleDecimalDigitsOnFocus: false,
  12. autoDecimalDigits: true,
  13. useGrouping: true,
  14. accountingSign: false,
  15. };
  16. } else if (moeda == 2) {
  17. currencyOptions = {
  18. currency: "PYG",
  19. locale: "es-PY",
  20. valueAsInteger: true,
  21. distractionFree: true,
  22. precision: 0,
  23. autoDecimalMode: true,
  24. valueRange: { min: 0 },
  25. allowNegative: true,
  26. };
  27. } else if (moeda == 3) {
  28. currencyOptions = {
  29. locale: "en-US",
  30. currency: "USD",
  31. currencyDisplay: "symbol",
  32. hideCurrencySymbolOnFocus: true,
  33. hideGroupingSeparatorOnFocus: true,
  34. hideNegligibleDecimalDigitsOnFocus: false,
  35. autoDecimalDigits: true,
  36. useGrouping: true,
  37. accountingSign: false,
  38. };
  39. }
  40. return currencyOptions;
  41. };
  42. const excerpt = (string, size = 30) => {
  43. if (size == null) return string;
  44. if (string.length > size) {
  45. string = string.substring(0, size) + "...";
  46. }
  47. return string;
  48. };
  49. // formatters
  50. // formatters de data e hora
  51. const convertDateTime = (dateTimeString) => {
  52. const dateTime = new Date(dateTimeString);
  53. const options = {
  54. timeZone: "America/Sao_Paulo",
  55. day: "2-digit",
  56. month: "2-digit",
  57. year: "numeric",
  58. hour: "2-digit",
  59. minute: "2-digit",
  60. second: "2-digit",
  61. };
  62. const formattedDateTime = dateTime
  63. .toLocaleString("pt-BR", options)
  64. .replace(",", "");
  65. return formattedDateTime;
  66. };
  67. const formatDateDMYtoYMD = (date, time) => {
  68. if (!date) throw new Error(useI18n().t("validation.rules.required"));
  69. const testDate =
  70. /^([0-2][0-9]|(3)[0-1])(\/)(((0)[0-9])|((1)[0-2]))(\/)\d{4}$/;
  71. if (testDate.test(date) === false)
  72. throw new Error(useI18n().t("validation.rules.date"));
  73. const [day, month, year] = date.split("/");
  74. return `${year}-${month}-${day} ${time ? time : ""}`;
  75. };
  76. const formatDateYMDtoDMY = (dateTime) => {
  77. const [datePart, timePart] = dateTime.split(" ");
  78. const [year, month, day] = datePart.split("-");
  79. const formattedDate = `${day}/${month}/${year}`;
  80. if (timePart) {
  81. const [hours, minutes, seconds] = timePart.split(":");
  82. const formattedTime = `${hours}:${minutes}:${seconds}`;
  83. return `${formattedDate} ${formattedTime}`;
  84. }
  85. return formattedDate;
  86. };
  87. // formatters de moeda e number
  88. const convertToCents = (value) => {
  89. if (value === null || value === undefined || value === "") return null;
  90. if (typeof value === "number") {
  91. return Math.round(value * 100);
  92. }
  93. if (typeof value === "string") {
  94. const cleanValue = value.replace(/[R$\s.]/g, "").replace(",", ".");
  95. const numericValue = parseFloat(cleanValue);
  96. return isNaN(numericValue) ? null : Math.round(numericValue * 100);
  97. }
  98. return null;
  99. };
  100. const formatCurrency = (value) => {
  101. return new Intl.NumberFormat("pt-BR", {
  102. style: "currency",
  103. currency: "BRL",
  104. minimumFractionDigits: 2,
  105. }).format(Number(value ?? 0));
  106. };
  107. const formatCurrencyCompact = (value) => {
  108. return new Intl.NumberFormat("pt-BR", {
  109. style: "currency",
  110. currency: "BRL",
  111. notation: "compact",
  112. maximumFractionDigits: 1,
  113. }).format(Number(value ?? 0));
  114. };
  115. const formatDecimal = (value) => {
  116. return Number(value ?? 0).toLocaleString("pt-BR", {
  117. minimumFractionDigits: 1,
  118. maximumFractionDigits: 1,
  119. });
  120. };
  121. const formatInteger = (value) => {
  122. return Number(value ?? 0).toLocaleString("pt-BR");
  123. };
  124. const formatPercent = (value, digits = 2) => {
  125. return `${Number(value ?? 0).toFixed(digits).replace(".", ",")}%`;
  126. };
  127. // filters
  128. const filterCurrency = (value) => {
  129. if (value) {
  130. value = parseFloat(value);
  131. return value.toLocaleString("pt-BR", {
  132. style: "currency",
  133. currency: "BRL",
  134. });
  135. }
  136. return value;
  137. };
  138. const filterUnidadeMedida = (value) => {
  139. return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".");
  140. };
  141. // validators
  142. const validaData = (date) => {
  143. const regex = /^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[0-2])\/\d{4}$/;
  144. return regex.test(date);
  145. };
  146. const validaHora = (time) => {
  147. const regex = /^([0-1][0-9]|2[0-3]):[0-5][0-9]$/;
  148. return regex.test(time);
  149. };
  150. const validaDataHora = (dataHora) => {
  151. const regex =
  152. /^(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]$/;
  153. return regex.test(dataHora);
  154. };
  155. // outros
  156. const truncatedName = (name) => {
  157. if (name.length <= 15) return name;
  158. const extension = name.split(".").pop();
  159. const nameWithoutExt = name.substring(0, name.lastIndexOf("."));
  160. return `${nameWithoutExt.substring(0, 8)}...${extension}`;
  161. };
  162. export const maskCpf = (cpf) => {
  163. if (!cpf) {
  164. return "";
  165. }
  166. const cleanedCpf = String(cpf).replace(/\D/g, "");
  167. if (cleanedCpf.length !== 11) {
  168. console.warn(
  169. "CPF inválido fornecido para a máscara. Retornando valor original.",
  170. );
  171. return cpf;
  172. }
  173. const firstPart = cleanedCpf.slice(0, 3);
  174. const lastPart = cleanedCpf.slice(9, 11);
  175. return `${firstPart}.***.***-${lastPart}`;
  176. };
  177. export {
  178. checaMoeda,
  179. convertDateTime,
  180. convertToCents,
  181. excerpt,
  182. filterCurrency,
  183. filterUnidadeMedida,
  184. formatCurrency,
  185. formatCurrencyCompact,
  186. formatDateDMYtoYMD,
  187. formatDateYMDtoDMY,
  188. formatDecimal,
  189. formatInteger,
  190. formatPercent,
  191. truncatedName,
  192. validaData,
  193. validaDataHora,
  194. validaHora,
  195. };