1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
|
# TextMate Grammar Token Examples
This file contains examples of every major TextMate token style for theme testing.
## Comments
<!-- HTML comment -->
// Single line comment
/_ Multi-line comment _/
# Shell comment
/_ JSDoc comment with @param and @return _/
## Strings
"Double quoted string"
'Single quoted string'
`Backtick string`
"String with \"escaped\" quotes"
'String with \'escaped\' quotes'
`String with \`escaped\` backticks`
## Template Literals
`Simple template literal`
`Template with ${variable} interpolation`
`Template with ${function.call()} expression`
Multi-line template with ${nested.interpolation}
## Numbers
42
-17
3.14159
-0.001
1e10
-2.5e-8
0xFF
0o755
0b1010
## Keywords
if else elif for while do switch case default
function class extends implements import export
return break continue throw try catch finally
var let const static async await yield
new this super null undefined true false
## Storage Types
int float double string boolean char void
static final abstract private public protected
readonly volatile transient synchronized
## Constants
MAX_VALUE
DEFAULT_TIMEOUT
API_ENDPOINT
PI
E
## Variables
variableName
\_privateVariable
$specialVariable
camelCase
snake_case
PascalCase
kebab-case
## Functions
functionName()
method.call()
object.property()
array[index]
arrowFunction => expression
## Operators
- - - / % ++ --
== === != !== > < >= <=
&& || ! & | ^ ~ << >> >>>
= += -= \*= /= %= &= |= ^= <<= >>= >>>=
## Punctuation
, ; : . ... ( ) [ ] { } < > / \\
# @ $ % ^ & \* - \_ + = | ~ ` ?
## Entities
ClassName
InterfaceName
EnumName
TypeName
MethodName
PropertyName
## Tags
<div>
<span>
<p>
<a href="link">
<img src="image.jpg" alt="description" />
## Attributes
class="container"
id="main"
data-value="123"
disabled
required
readonly
## CSS Selectors & Properties
.container
#header
.button:hover
input[type="text"]
::before
::after
color: #ffffff;
background: linear-gradient(45deg, #ff0000, #00ff00);
font-size: 16px;
margin: 0 auto;
padding: 10px 20px;
## Regular Expressions
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
/\d{3}-\d{3}-\d{4}/g
/(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})/
## URLs & Paths
https://example.com/path/to/resource
file:///Users/username/project
./relative/path
../parent/directory
/home/user/documents
## JSON
{
"name": "example",
"version": "1.0.0",
"dependencies": {
"react": "^18.0.0",
"typescript": "^4.9.0"
},
"scripts": {
"start": "node index.js",
"test": "jest"
}
}
## XML/HTML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="content">
<h1>Title</h1>
<p>Paragraph text</p>
</div>
</body>
</html>
## SQL
SELECT u.id, u.name, u.email, COUNT(o.id) as order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE u.active = true
AND o.created_at >= '2023-01-01'
GROUP BY u.id, u.name, u.email
HAVING COUNT(o.id) > 5
ORDER BY order_count DESC
LIMIT 10;
## GraphQL
query GetUserProfile($userId: ID!, $includePosts: Boolean!) {
user(id: $userId) {
id
name
email
avatar
createdAt
posts @include(if: $includePosts) {
id
title
content
publishedAt
comments(first: 10) {
edges {
node {
id
author
content
createdAt
}
}
}
}
}
}
## Shell/Bash
#!/bin/bash
# Variables
PROJECT*DIR="/home/user/projects"
BACKUP_DIR="$PROJECT_DIR/backups"
TIMESTAMP=$(date +"%Y%m%d*%H%M%S")
# Functions
create*backup() {
local source_dir=$1
local backup_file="$BACKUP_DIR/backup*$TIMESTAMP.tar.gz"
echo "Creating backup of $source_dir..."
tar -czf "$backup_file" "$source_dir"
echo "Backup created: $backup_file"
}
# Conditional logic
if [ -d "$PROJECT_DIR" ]; then
create_backup "$PROJECT_DIR"
else
echo "Project directory not found: $PROJECT_DIR"
exit 1
fi
## Python
import os
import sys
from typing import List, Dict, Optional
import requests
from dataclasses import dataclass
@dataclass
class User:
id: int
name: str
email: Optional[str] = None
active: bool = True
def __post_init__(self):
if not self.name.strip():
raise ValueError("Name cannot be empty")
class UserService:
def **init**(self, api_url: str):
self.api_url = api_url
self.session = requests.Session()
async def get_user(self, user_id: int) -> Optional[User]:
"""Fetch user data from API."""
try:
response = await self.session.get(f"{self.api_url}/users/{user_id}")
response.raise_for_status()
data = response.json()
return User(**data)
except requests.RequestException as e:
print(f"Error fetching user {user_id}: {e}")
return None
## Rust
use std::collections::HashMap;
use std::fs::File;
use std::io::{self, Read};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
pub id: u64,
pub name: String,
pub email: Option<String>, #[serde(default)]
pub active: bool,
pub created_at: chrono::DateTime<chrono::Utc>,
}
impl User {
pub fn new(id: u64, name: String, email: Option<String>) -> Self {
Self {
id,
name,
email,
active: true,
created_at: chrono::Utc::now(),
}
}
pub fn display_name(&self) -> String {
match &self.email {
Some(email) => format!("{} <{}>", self.name, email),
None => self.name.clone(),
}
}
}
pub struct UserService {
api_url: String,
client: reqwest::Client,
}
impl UserService {
pub fn new(api_url: String) -> Self {
Self {
api_url,
client: reqwest::Client::new(),
}
}
pub async fn get_user(&self, user_id: u64) -> Result<User, Box<dyn std::error::Error>> {
let url = format!("{}/users/{}", self.api_url, user_id);
let response = self.client.get(&url).send().await?;
let user: User = response.json().await?;
Ok(user)
}
}
## Go
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
)
type User struct {
ID int64 `json:"id"`
Name string `json:"name"`
Email \*string `json:"email,omitempty"`
Active bool `json:"active"`
CreatedAt time.Time `json:"created_at"`
}
type UserService struct {
re UserRepository
}
func NewUserService(repo UserRepository) \*UserService {
return &UserService{repo: repo}
}
func (s *UserService) GetUser(ctx context.Context, id int64) (*User, error) {
user, err := s.repo.FindByID(ctx, id)
if err != nil {
return nil, fmt.Errorf("failed to get user %d: %w", id, err)
}
return user, nil
}
func (s *UserService) CreateUser(ctx context.Context, req *CreateUserRequest) (\*User, error) {
user := &User{
Name: req.Name,
Email: req.Email,
Active: true,
CreatedAt: time.Now(),
}
if err := s.repo.Create(ctx, user); err != nil {
return nil, fmt.Errorf("failed to create user: %w", err)
}
return user, nil
}
## YAML
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
namespace: production
labels:
app: web-app
version: v1.2.3
spec:
replicas: 3
selector:
matchLabels:
app: web-app
template:
metadata:
labels:
app: web-app
tier: frontend
spec:
containers: - name: web-app
image: nginx:1.21-alpine
ports: - containerPort: 80
protocol: TCP
env: - name: NODE_ENV
value: "production" - name: API_URL
valueFrom:
secretKeyRef:
name: app-secrets
key: api-url
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
livenessProbe:
httpGet:
path: /health
port: 80
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 80
initialDelaySeconds: 5
periodSeconds: 5
## TOML
[project]
name = "example-app"
version = "1.0.0"
description = "An example application"
authors = ["John Doe <[email protected]>"]
license = "MIT"
readme = "README.md"
homepage = "https://example.com"
repository = "https://github.com/johndoe/example-app"
keywords = ["web", "api", "rust"]
categories = ["web-programming"]
edition = "2021"
[dependencies]
tokio = { version = "1.0", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
reqwest = { version = "0.11", features = ["json"] }
chrono = { version = "0.4", features = ["serde"] }
log = "0.4"
env_logger = "0.10"
[dev-dependencies]
tokio-test = "0.4"
mockito = "1.0"
[[bin]]
name = "server"
path = "src/main.rs"
[[bin]]
name = "client"
path = "src/client.rs"
## Dockerfile
FROM node:18-alpine AS base
# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml\* ./
RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED 1
RUN \
if [ -f yarn.lock ]; then yarn run build; \
elif [ -f package-lock.json ]; then npm run build; \
elif [ -f pnpm-lock.yaml ]; then pnpm run build; \
else echo "Lockfile not found." && exit 1; \
fi
# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app
ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder /app/public ./public
# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
ENV PORT 3000
# set hostname to localhost
ENV HOSTNAME "0.0.0.0"
# server.js is created by next build from the standalone output
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
CMD ["node", "server.js"]
## Makefile
.PHONY: help build test clean install dev lint format
# Default target
.DEFAULT_GOAL := help
# Variables
APP_NAME := myapp
VERSION := $(shell git describe --tags --always --dirty)
BUILD_DIR := ./build
DIST_DIR := ./dist
GO_FILES := $(shell find . -name '\*.go' -type f)
help: ## Show this help message
@echo "Available targets:"
@grep -E '^[a-zA-Z_-]+:._?## ._$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.\*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
install: ## Install dependencies
go mod download
npm install
build: ## Build the application
@echo "Building $(APP_NAME) version $(VERSION)..."
mkdir -p $(BUILD_DIR)
go build -ldflags "-X main.version=$(VERSION)" -o $(BUILD_DIR)/$(APP_NAME) ./cmd/main.go
test: ## Run tests
go test -v ./...
npm test
lint: ## Run linters
golangci-lint run
npx eslint .
format: ## Format code
go fmt ./...
npx prettier --write .
dev: ## Run in development mode
go run ./cmd/main.go --dev
clean: ## Clean build artifacts
rm -rf $(BUILD_DIR)
rm -rf $(DIST_DIR)
go clean -cache
docker-build: ## Build Docker image
docker build -t $(APP_NAME):$(VERSION) .
docker tag $(APP_NAME):$(VERSION) $(APP_NAME):latest
docker-run: ## Run Docker container
docker run -p 8080:8080 $(APP_NAME):latest
release: ## Create a new release
@echo "Creating release $(VERSION)"
git tag -a $(VERSION) -m "Release $(VERSION)"
git push origin $(VERSION)
goreleaser release --rm-dist
## Git Diff
diff --git a/src/components/UserProfile.tsx b/src/components/UserProfile.tsx
index 1234567..abcdefg 100644
--- a/src/components/UserProfile.tsx
+++ b/src/components/UserProfile.tsx
@@ -10,7 +10,7 @@ interface User {
id: number
name: string
email?: string
- createdAt: Date
* readonly createdAt: Date
active: boolean
}
@@ -25,8 +25,12 @@ const UserProfile: FC<{ user: User }> = ({ user }) => {
const [isEditing, setIsEditing] = useState(false)
const [formData, setFormData] = useState(user)
- const handleSubmit = async (e: React.FormEvent) => {
- e.preventDefault()
- // Handle form submission
- }
- return (
* <div className="user-card">
- <div className={`user-card ${user.active ? 'active' : 'inactive'}`}>
<h3>{user.name}</h3>
<p>{user.email}</p>
</div>
|