UNPKG

zen-digital-utils

Version:

Zen Utils is a collection of utility functions to simplify and enhance your development workflow. This project aims to provide reusable and efficient utilities for common tasks.

224 lines (218 loc) 6.66 kB
// src/utils/dom/hideScroll.ts function hideScroll(isOpen) { if (typeof document === "undefined") return; if (isOpen) { document.body.classList.add("!overflow-hidden"); } else { document.body.classList.remove("!overflow-hidden"); } } // src/utils/states/index.ts var brazilianStates = [ { value: "AC", label: "Acre" }, { value: "AL", label: "Alagoas" }, { value: "AP", label: "Amap\xE1" }, { value: "AM", label: "Amazonas" }, { value: "BA", label: "Bahia" }, { value: "CE", label: "Cear\xE1" }, { value: "DF", label: "Distrito Federal" }, { value: "ES", label: "Esp\xEDrito Santo" }, { value: "GO", label: "Goi\xE1s" }, { value: "MA", label: "Maranh\xE3o" }, { value: "MT", label: "Mato Grosso" }, { value: "MS", label: "Mato Grosso do Sul" }, { value: "MG", label: "Minas Gerais" }, { value: "PA", label: "Par\xE1" }, { value: "PB", label: "Para\xEDba" }, { value: "PR", label: "Paran\xE1" }, { value: "PE", label: "Pernambuco" }, { value: "PI", label: "Piau\xED" }, { value: "RJ", label: "Rio de Janeiro" }, { value: "RN", label: "Rio Grande do Norte" }, { value: "RS", label: "Rio Grande do Sul" }, { value: "RO", label: "Rond\xF4nia" }, { value: "RR", label: "Roraima" }, { value: "SC", label: "Santa Catarina" }, { value: "SP", label: "S\xE3o Paulo" }, { value: "SE", label: "Sergipe" }, { value: "TO", label: "Tocantins" } ]; // src/utils/formatters/date/index.ts function formattedDate(thisDate) { const dateParts = thisDate.split("-"); const date = /* @__PURE__ */ new Date(`${dateParts[1]}/${dateParts[2]}/${dateParts[0]}`); const day = String(date.getDate()).padStart(2, "0"); const month = String(date.getMonth() + 1).padStart(2, "0"); const year = date.getFullYear(); return `${day}/${month}/${year}`; } function formatBirthdate(value) { const digits = value.replace(/\D/g, ""); const day = digits.slice(0, 2); const month = digits.slice(2, 4); const year = digits.slice(4, 8); let formatted = ""; if (day) formatted += day; if (month) formatted += `/${month}`; if (year) formatted += `/${year}`; return formatted; } function formatDateTime(dateTimeString) { const [date, time] = dateTimeString.split(" "); return `${date} \u2022 ${time}`; } // src/utils/formatters/general/index.ts function formatPercent(value) { if (value == null || value === "") { return "R$ 0,00"; } let numberValue = Number(value); if (Number.isNaN(numberValue)) { return "R$ 0,00"; } numberValue = Math.floor(numberValue * 100) / 100; const formattedValue = numberValue.toFixed(2).replace(".", ",").replace(/\B(?=(\d{3})+(?!\d))/g, "."); return `${formattedValue}`; } function formatFullName(value) { return value.replace(/[^a-záãéêíóõúç\s]/gi, "").replace(/\s+/g, " "); } function formatTextOnly(value) { return value.replace(/[^a-zA-ZÀ-ÿ\s]/g, ""); } function formatZipCode(value) { value = value.replace(/\D/g, ""); value = value.slice(0, 8); if (value.length > 5) { value = value.replace(/(\d{5})(\d)/, "$1-$2"); } return value; } function capitalizeText(text) { if (!text) return ""; return text.split(" ").map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join(" "); } function getAcronym(fullName) { if (!fullName) return ""; const names = fullName.trim().split(" "); const firstInitial = names[0][0].toUpperCase(); const secondInitial = names[1] ? names[1][0].toUpperCase() : ""; return firstInitial + secondInitial; } // src/utils/formatters/currency/index.ts function formatCurrencyInput(value) { let numberValue = value == null ? void 0 : value.replace(/\D/g, ""); if (!value || !numberValue) { return "R$ 0,00"; } numberValue = (Number.parseInt(numberValue) / 100).toFixed(2).toString(); return `R$ ${numberValue.replace(".", ",").replace(/\B(?=(\d{3})+(?!\d))/g, ".")}`; } function formatCurrency(value) { if (value == null || value === "") { return "R$ 0,00"; } let numberValue = Number(value); if (Number.isNaN(numberValue)) { return "R$ 0,00"; } numberValue = Math.floor(numberValue * 100) / 100; const formattedValue = numberValue.toFixed(2).replace(".", ",").replace(/\B(?=(\d{3})+(?!\d))/g, "."); return `R$ ${formattedValue}`; } function parseBrazilianCurrency(str) { str = str.replace(/[^0-9,.-]+/g, ""); str = str.replace(/\./g, ""); str = str.replace(",", "."); return Number.parseFloat(str); } // src/utils/formatters/numbers/index.ts function formatPhoneNumberForWhatsApp(value) { const cleanValue = value.replace(/\D/g, ""); return cleanValue; } function formatNumber(value) { value = value.replace(/\D/g, ""); return value; } function formatPhoneNumber(value) { if (!value) { return ""; } const valueWithoutMask = value.replace(/\D/g, ""); const maxLength = 11; const limitedValue = valueWithoutMask.slice(0, maxLength); if (limitedValue.length > 10) { return limitedValue.replace(/^(\d{2})(\d{5})(\d{4})/, "($1) $2-$3"); } if (limitedValue.length > 6) { return limitedValue.replace(/^(\d{2})(\d{4})(\d{0,4})/, "($1) $2-$3"); } if (limitedValue.length > 2) { return limitedValue.replace(/^(\d{2})(\d{0,5})/, "($1) $2"); } return limitedValue.replace(/^(\d{0,2})/, "($1"); } function formatCountBankNumber(value) { value = value.replace(/\D/g, ""); value = value.slice(0, 13); if (value.length > 12) { value = value.replace(/(\d{13})/, "$1"); } return value; } function formatBranchNumber(value) { value = value.replace(/\D/g, ""); value = value.slice(0, 5); if (value.length > 4) { value = value.replace(/(\d{4})(\d)/, "$1-$2"); } return value; } function formatBranchNumberWithDefault(value) { value = value.replace(/\D/g, ""); if (value.length === 4) { value += "0"; } value = value.slice(0, 5); if (value.length > 4) { value = value.replace(/(\d{4})(\d)/, "$1-$2"); } return value; } function formatCpf(value) { value = value.replace(/\D/g, ""); value = value.slice(0, 11); value = value.replace(/(\d{3})(\d)/, "$1.$2"); value = value.replace(/(\d{3})(\d)/, "$1.$2"); value = value.replace(/(\d{3})(\d{1,2})$/, "$1-$2"); return value; } export { brazilianStates, capitalizeText, formatBirthdate, formatBranchNumber, formatBranchNumberWithDefault, formatCountBankNumber, formatCpf, formatCurrency, formatCurrencyInput, formatDateTime, formatFullName, formatNumber, formatPercent, formatPhoneNumber, formatPhoneNumberForWhatsApp, formatTextOnly, formatZipCode, formattedDate, getAcronym, hideScroll, parseBrazilianCurrency };