"use client"; import { useState } from "react"; import { CaseSensitive, Copy, Trash2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Textarea } from "@/components/ui/textarea"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { toast } from "sonner"; export default function CaseConverterPage() { const [input, setInput] = useState(""); const copyToClipboard = async (text: string) => { if (!text) return; try { await navigator.clipboard.writeText(text); toast.success("已复制到剪贴板"); } catch { toast.error("复制失败"); } }; const toUpper = () => setInput(input.toUpperCase()); const toLower = () => setInput(input.toLowerCase()); const toTitleCase = () => { const result = input .toLowerCase() .split(/[\s_-]+/) .map(word => word.charAt(0).toUpperCase() + word.slice(1)) .join(" "); setInput(result); }; const toCamelCase = () => { const words = input.toLowerCase().split(/[\s_-]+/); const result = words[0] + words.slice(1).map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(""); setInput(result); }; const toPascalCase = () => { const result = input .toLowerCase() .split(/[\s_-]+/) .map(word => word.charAt(0).toUpperCase() + word.slice(1)) .join(""); setInput(result); }; const toSnakeCase = () => { const result = input .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g) ?.map(x => x.toLowerCase()) .join("_") || ""; setInput(result); }; const toKebabCase = () => { const result = input .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g) ?.map(x => x.toLowerCase()) .join("-") || ""; setInput(result); }; const clearAll = () => setInput(""); return (
大写、小写、驼峰、蛇形等格式快速转换
Camel Case
第一个单词小写,后续单词首字母大写,如 `helloWorld`。
Snake Case
所有字母小写,单词间用下划线连接,如 `hello_world`。
Kebab Case
所有字母小写,单词间用连字符连接,如 `hello-world`。