Skip to content

Commit 0ced931

Browse files
authored
refactor(types): add Speed, Scale, BadgeSize literal types (JhaSourav07#859)
## Description Centralizes `Speed`, `Scale`, and `BadgeSize` as literal/branded types in `types/index.ts`. Fixes JhaSourav07#319 ## Pillar - [ ] 🎨 Pillar 1 β€” New Theme Design - [ ] πŸ“ Pillar 2 β€” Geometric SVG Improvement - [ ] πŸ• Pillar 3 β€” Timezone Logic Optimization - [x] πŸ› οΈ Other (Bug fix, refactoring, docs) ## Checklist before requesting a review: - [x] I have read the `CONTRIBUTING.md` file. - [x] I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME`). - [x] I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). - [x] My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). - [ ] I have updated `README.md` if I added a new theme or URL parameter. - [x] I have started the repo. - [x] I have made sure that i have only one commit to merge in this PR. - [x] The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts). - [ ] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents a49fd7b + d8fe0a1 commit 0ced931

3 files changed

Lines changed: 16 additions & 9 deletions

File tree

β€Žlib/svg/sanitizer.tsβ€Ž

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import type { HexColor } from '../../types/index';
7+
import type { SpeedString } from '../../types/index';
78

89
const HEX_COLOR_REGEX = /^([0-9A-Fa-f]{3}|[0-9A-Fa-f]{4}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$/;
910

@@ -54,17 +55,17 @@ export function sanitizeHexColor(input: string | undefined | null, fallback: str
5455
* Expected format: [number]s (e.g., "8s", "1.5s").
5556
* Valid range: 2s to 20s.
5657
*/
57-
export function sanitizeSpeed(speed: string | undefined | null, fallback = '8s'): string {
58-
if (!speed) return fallback;
58+
export function sanitizeSpeed(speed: string | undefined | null, fallback = '8s'): SpeedString {
59+
if (!speed) return fallback as SpeedString;
5960
const trimmed = speed.trim();
6061
const match = trimmed.match(/^(\d+(\.\d+)?)s$/);
6162
if (match) {
6263
const numeric = parseFloat(match[1]);
6364
if (numeric >= 2 && numeric <= 20) {
64-
return trimmed;
65+
return trimmed as SpeedString;
6566
}
6667
}
67-
return fallback;
68+
return fallback as SpeedString;
6869
}
6970

7071
/**

β€Žscripts/benchmark-svg.tsβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { performance } from 'perf_hooks';
22
import { generateSVG } from '../lib/svg/generator';
3-
import { hexColor } from '../lib/svg/sanitizer';
3+
import { hexColor, sanitizeSpeed } from '../lib/svg/sanitizer';
44

55
const stats = {
66
currentStreak: 12,
@@ -15,7 +15,7 @@ const baseParams = {
1515
accent: '00ffaa',
1616
text: 'ffffff',
1717
scale: 'linear' as const,
18-
speed: '8s',
18+
speed: sanitizeSpeed('8s'),
1919
};
2020

2121
const calendar = {

β€Žtypes/index.tsβ€Ž

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
export type HexColor = string & { __brand: 'HexColor' };
22

3+
export type Scale = 'linear' | 'log';
4+
5+
export type BadgeSize = 'small' | 'medium' | 'large';
6+
7+
export type SpeedString = `${number}s`;
8+
39
/**
410
* Processed streak statistics calculated from the user's GitHub contribution data.
511
*/
@@ -102,10 +108,10 @@ export interface BadgeParams {
102108
accent: HexColor;
103109

104110
/** Duration of the radar scan line animation (e.g. '4s', '8s', '12s'). Defaults to '8s'. */
105-
speed: string;
111+
speed: SpeedString;
106112

107113
/** Tower height scaling algorithm. 'linear' scales proportionally; 'log' uses logarithmic scale for high contributors. Defaults to 'linear'. */
108-
scale: 'linear' | 'log';
114+
scale: Scale;
109115

110116
/** Font family override for badge typography (e.g. 'monospace'). Defaults to theme font. */
111117
font?: string;
@@ -141,5 +147,5 @@ export interface BadgeParams {
141147
height?: number;
142148

143149
/** Preset size of the badge. 'small', 'medium', or 'large'. Overrides width and height. */
144-
size?: 'small' | 'medium' | 'large';
150+
size?: BadgeSize;
145151
}

0 commit comments

Comments
Β (0)