/
hikas
/
RegisterConverter
Обзор
Документация
Войти
/
hikas
/
RegisterConverter
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
script.js
167 строк
4 KB
TonyHikas
First
06 июн 2026, 21:55
06 июн 2026, 21:55
82c8131
Код
Авторство
О чём код?
const inputEl = document.getElementById("input-text"); const outputEl = document.getElementById("output-text"); const charCountEl = document.getElementById("char-count"); const actionsGrid = document.getElementById("actions-grid"); const toastEl = document.getElementById("toast"); let currentAction = "upper"; let toastTimer = null; const converters = { upper: (text) => text.toUpperCase(), lower: (text) => text.toLowerCase(), title: (text) => text .toLowerCase() .replace(/(?:^|\s|[-_/\\.])(\p{L})/gu, (match) => match.toUpperCase()), sentence: (text) => text .toLowerCase() .replace(/(^\s*\p{L})|([.!?]\s+\p{L})/gu, (match) => match.toUpperCase()), camel: (text) => { const words = splitWords(text); if (words.length === 0) return ""; return words .map((word, index) => { const lower = word.toLowerCase(); return index === 0 ? lower : capitalize(lower); }) .join(""); }, pascal: (text) => splitWords(text) .map((word) => capitalize(word.toLowerCase())) .join(""), snake: (text) => splitWords(text) .map((word) => word.toLowerCase()) .join("_"), kebab: (text) => splitWords(text) .map((word) => word.toLowerCase()) .join("-"), toggle: (text) => [...text] .map((char) => char === char.toUpperCase() ? char.toLowerCase() : char.toUpperCase() ) .join(""), }; function splitWords(text) { return text .replace(/([a-z0-9])([A-Z])/g, "$1 $2") .replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2") .split(/[\s_\-.]+/) .filter(Boolean); } function capitalize(word) { if (!word) return ""; return word.charAt(0).toUpperCase() + word.slice(1); } function convertText() { const source = inputEl.value; const converter = converters[currentAction]; if (!converter) { outputEl.value = ""; updateCharCount(0); return; } const result = converter(source); outputEl.value = result; updateCharCount(result.length); } function updateCharCount(length) { charCountEl.textContent = `${length} ${pluralize(length, "символ", "символа", "символов")}`; } function pluralize(count, one, few, many) { const mod10 = count % 10; const mod100 = count % 100; if (mod10 === 1 && mod100 !== 11) return one; if (mod10 >= 2 && mod10 <= 4 && (mod100 < 10 || mod100 >= 20)) return few; return many; } function showToast(message) { toastEl.textContent = message; toastEl.classList.add("show"); clearTimeout(toastTimer); toastTimer = setTimeout(() => { toastEl.classList.remove("show"); }, 1800); } function setActiveAction(action) { currentAction = action; actionsGrid.querySelectorAll(".action-btn").forEach((button) => { button.classList.toggle("active", button.dataset.action === action); }); convertText(); } inputEl.addEventListener("input", convertText); actionsGrid.addEventListener("click", (event) => { const button = event.target.closest(".action-btn"); if (!button) return; setActiveAction(button.dataset.action); }); document.getElementById("btn-clear").addEventListener("click", () => { inputEl.value = ""; convertText(); inputEl.focus(); }); document.getElementById("btn-paste").addEventListener("click", async () => { try { const text = await navigator.clipboard.readText(); inputEl.value = text; convertText(); showToast("Текст вставлен"); } catch { showToast("Не удалось вставить текст"); } }); document.getElementById("btn-copy").addEventListener("click", async () => { if (!outputEl.value) { showToast("Нечего копировать"); return; } try { await navigator.clipboard.writeText(outputEl.value); showToast("Скопировано"); } catch { outputEl.select(); document.execCommand("copy"); showToast("Скопировано"); } }); document.getElementById("btn-swap").addEventListener("click", () => { const previousOutput = outputEl.value; inputEl.value = previousOutput; convertText(); }); convertText();