Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Base64Page } from "@/pages/base64"
import { GistPage } from "@/pages/gist"
import { HomePage } from "@/pages/home"
import { QRCodePage } from "@/pages/qrcode"
import { StringLengthPage } from "@/pages/string-length"
import { TimestampPage } from "@/pages/timestamp"

export function App() {
Expand All @@ -30,6 +31,7 @@ export function App() {
<Route path="/timestamp" element={<TimestampPage />} />
<Route path="/gist" element={<GistPage />} />
<Route path="/qrcode" element={<QRCodePage />} />
<Route path="/string-length" element={<StringLengthPage />} />
</Routes>
</main>
</SidebarInset>
Expand Down
4 changes: 4 additions & 0 deletions src/components/app-sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const navItems = [
title: "Image",
items: [{ title: "QR Code", url: "/qrcode" }],
},
{
title: "String",
items: [{ title: "String Length", url: "/string-length" }],
},
]

export function AppSidebar({ ...props }: React.ComponentProps<typeof Sidebar>) {
Expand Down
65 changes: 65 additions & 0 deletions src/pages/string-length.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { useState } from "react"

import { Textarea } from "@/components/ui/textarea"

function countBytes(str: string): number {
return new TextEncoder().encode(str).length
}

function countWords(str: string): number {
const trimmed = str.trim()
return trimmed === "" ? 0 : trimmed.split(/\s+/).length
}

function countLines(str: string): number {
return str === "" ? 0 : str.split("\n").length
}

export function StringLengthPage() {
const [input, setInput] = useState("")

const charCount = input.length
const charNoSpaceCount = input.replace(/\s/g, "").length
const byteCount = countBytes(input)
const wordCount = countWords(input)
const lineCount = countLines(input)

const stats: { label: string; value: number }[] = [
{ label: "Characters", value: charCount },
{ label: "Characters (no spaces)", value: charNoSpaceCount },
{ label: "Bytes (UTF-8)", value: byteCount },
{ label: "Words", value: wordCount },
{ label: "Lines", value: lineCount },
]

return (
<div className="flex flex-col gap-6 p-6">
<div>
<h1 className="text-2xl font-semibold">String Length</h1>
<p className="text-muted-foreground">
Count characters, bytes, words, and lines in a string.
</p>
</div>
<div className="flex flex-col gap-3">
<label className="text-sm font-medium">Input</label>
<Textarea
rows={8}
value={input}
onChange={(e) => setInput(e.target.value)}
placeholder="Type or paste text here..."
/>
</div>
<div className="flex flex-wrap gap-3">
{stats.map(({ label, value }) => (
<div
key={label}
className="flex min-w-36 flex-col gap-1 rounded-lg border bg-muted px-4 py-3"
>
<span className="text-xs text-muted-foreground">{label}</span>
<span className="font-mono text-2xl font-semibold">{value}</span>
</div>
))}
</div>
</div>
)
}
Loading