| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- import { useI18n } from "vue-i18n";
- /**
- * @description Corta um valor em um determinado tamanho, adicionando "..." no final.
- * @param {*} value valor a ser cortado (convertido para string).
- * @param {number} maxLength tamanho máximo antes de truncar.
- * @returns {string} string truncada.
- */
- const truncate = (value, maxLength = 40) => {
- const text = value == null ? "" : String(value);
- if (text.length <= maxLength) return text;
- return `${text.slice(0, maxLength)}...`;
- };
- /**
- * @description Formata uma data de DD/MM/YYYY para YYYY-MM-DD
- * @param {string} date data.
- * @param {string} time tempo.
- * @throws {Error} Caso a data seja nula ou invalida.
- * @returns {string} data formatada.
- */
- const formatDateDMYtoYMD = (date, time) => {
- if (!date) throw new Error(useI18n().t("validation.rules.required"));
- const testDate =
- /^([0-2][0-9]|(3)[0-1])(\/)(((0)[0-9])|((1)[0-2]))(\/)\d{4}$/;
- if (testDate.test(date) === false)
- throw new Error(useI18n().t("validation.rules.date"));
- const [day, month, year] = date.split("/");
- return `${year}-${month}-${day} ${time ? time : ""}`;
- };
- /**
- * @description Converte uma data e hora para o formato brasileiro.
- * @param {string} dateTimeString data e hora.
- * @returns {string} data e hora no formato brasileiro.
- * @throws {Error} Caso a data seja nula ou invalida.
- * @returns {string} data formatada.
- * @example
- * // convertDateTime("2023-05-23T13:07:27.000000Z");
- * // Output: 23/05/2023 10:07:27
- */
- const convertDateTime = (dateTimeString) => {
- const dateTime = new Date(dateTimeString);
- const options = {
- timeZone: "America/Sao_Paulo",
- day: "2-digit",
- month: "2-digit",
- year: "numeric",
- hour: "2-digit",
- minute: "2-digit",
- second: "2-digit",
- };
- const formattedDateTime = dateTime
- .toLocaleString("pt-BR", options)
- .replace(",", "");
- return formattedDateTime;
- };
- /**
- * @description Formata uma data de YYYY-MM-DD para DD/MM/YYYY
- * @param {string} dateTime data e hora.
- * @returns {string} data e hora no formato brasileiro.
- * @example
- * // formatDateYMDtoDMY("2023-05-23T13:07:27.000000Z");
- * // Output: 23/05/2023 10:07:27
- */
- const formatDateYMDtoDMY = (dateTime) => {
- if (!dateTime || typeof dateTime !== "string") return "-";
- const normalizedDateTime = dateTime.trim().replace("T", " ");
- if (!normalizedDateTime) return "-";
- const [datePart, timePart] = normalizedDateTime.split(" ");
- const [year, month, day] = datePart.split("-");
- if (!year || !month || !day) return "-";
- const formattedDate = `${day}/${month}/${year}`;
- if (timePart) {
- const [hours, minutes, seconds = "00"] = timePart
- .replace("Z", "")
- .split(":");
- const formattedTime = `${hours}:${minutes}:${seconds.split(".")[0]}`;
- return `${formattedDate} ${formattedTime}`;
- }
- return formattedDate;
- };
- /**
- * @description Formata a moeda.
- * @param {number} value valor.
- * @returns {string} valor formatado.
- */
- const formatToBRLCurrency = (value) => {
- if (value != null) {
- value = parseFloat(value);
- return value.toLocaleString("pt-BR", {
- minimumFractionDigits: 2,
- style: "currency",
- currency: "BRL",
- });
- }
- return value;
- };
- const normalizeString = (val) =>
- val
- .toLowerCase()
- .normalize("NFKD")
- .replace(/[\u0300-\u036f~]/g, "");
- const normalizeUnitName = (value) => {
- const name = String(value ?? "").trim();
- return name && !["null", "undefined"].includes(name.toLowerCase())
- ? name
- : null;
- };
- const formatUnitName = (unit, fallback = "Unidade sem nome") =>
- normalizeUnitName(unit?.name) ||
- normalizeUnitName(unit?.fantasy_name) ||
- normalizeUnitName(unit?.social_reason) ||
- fallback;
- const isUnderage = (birthDate, referenceDate = new Date()) => {
- if (!birthDate || typeof birthDate !== "string") return false;
- const normalizedDate = birthDate.trim().split(" ")[0];
- const parts = normalizedDate.includes("/")
- ? normalizedDate.split("/").reverse()
- : normalizedDate.split("-");
- const [year, month, day] = parts.map(Number);
- if (!year || !month || !day) return false;
- const parsedBirthDate = new Date(year, month - 1, day);
- const isValidDate =
- parsedBirthDate.getFullYear() === year &&
- parsedBirthDate.getMonth() === month - 1 &&
- parsedBirthDate.getDate() === day;
- if (!isValidDate) return false;
- const eighteenthBirthday = new Date(year + 18, month - 1, day);
- return eighteenthBirthday > referenceDate;
- };
- export {
- formatDateDMYtoYMD,
- formatDateYMDtoDMY,
- truncate,
- convertDateTime,
- formatToBRLCurrency,
- normalizeString,
- formatUnitName,
- isUnderage,
- };
|