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.
271 lines (263 loc) • 8.57 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
brazilianStates: () => brazilianStates,
capitalizeText: () => capitalizeText,
formatBirthdate: () => formatBirthdate,
formatBranchNumber: () => formatBranchNumber,
formatBranchNumberWithDefault: () => formatBranchNumberWithDefault,
formatCountBankNumber: () => formatCountBankNumber,
formatCpf: () => formatCpf,
formatCurrency: () => formatCurrency,
formatCurrencyInput: () => formatCurrencyInput,
formatDateTime: () => formatDateTime,
formatFullName: () => formatFullName,
formatNumber: () => formatNumber,
formatPercent: () => formatPercent,
formatPhoneNumber: () => formatPhoneNumber,
formatPhoneNumberForWhatsApp: () => formatPhoneNumberForWhatsApp,
formatTextOnly: () => formatTextOnly,
formatZipCode: () => formatZipCode,
formattedDate: () => formattedDate,
getAcronym: () => getAcronym,
hideScroll: () => hideScroll,
parseBrazilianCurrency: () => parseBrazilianCurrency
});
module.exports = __toCommonJS(index_exports);
// 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;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
brazilianStates,
capitalizeText,
formatBirthdate,
formatBranchNumber,
formatBranchNumberWithDefault,
formatCountBankNumber,
formatCpf,
formatCurrency,
formatCurrencyInput,
formatDateTime,
formatFullName,
formatNumber,
formatPercent,
formatPhoneNumber,
formatPhoneNumberForWhatsApp,
formatTextOnly,
formatZipCode,
formattedDate,
getAcronym,
hideScroll,
parseBrazilianCurrency
});