refactor: Centralize calculator statistics and categories into a new module, lazy load search functionality, and remove unused font preloads.

This commit is contained in:
Ben
2026-03-08 19:15:42 -07:00
parent 0114e00618
commit 1093208324
7 changed files with 161 additions and 96 deletions

View File

@@ -5,6 +5,7 @@ from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent
CALCLIST = BASE_DIR / 'calculators_list.md'
OUTPUT_FILE = BASE_DIR / 'hdyc-svelte/src/lib/data/calculators.ts'
STATS_FILE = BASE_DIR / 'hdyc-svelte/src/lib/data/stats.ts'
CATEGORY_KEYS = [
'length',
@@ -29,6 +30,29 @@ CATEGORY_KEYS = [
'other',
]
CATEGORIES = {
'length': {'label': 'Length / Distance', 'icon': '📏'},
'weight': {'label': 'Weight / Mass', 'icon': '⚖️'},
'temperature': {'label': 'Temperature', 'icon': '🌡️'},
'volume': {'label': 'Volume', 'icon': '🧪'},
'fluids': {'label': 'Fluids', 'icon': '💧'},
'area': {'label': 'Area', 'icon': '🔳'},
'speed': {'label': 'Speed / Velocity', 'icon': '💨'},
'pressure': {'label': 'Pressure', 'icon': '🔽'},
'energy': {'label': 'Energy', 'icon': ''},
'magnetism': {'label': 'Magnetism', 'icon': '🧲'},
'power': {'label': 'Power', 'icon': '🔌'},
'data': {'label': 'Data Storage', 'icon': '💾'},
'time': {'label': 'Time', 'icon': '⏱️'},
'angle': {'label': 'Angle', 'icon': '📐'},
'number-systems': {'label': 'Number Systems', 'icon': '🔢'},
'radiation': {'label': 'Radiation', 'icon': '☢️'},
'electrical': {'label': 'Electrical', 'icon': '🔋'},
'force': {'label': 'Force / Torque', 'icon': '💪'},
'light': {'label': 'Light', 'icon': '💡'},
'other': {'label': 'Other', 'icon': '🔄'},
}
CATEGORY_SET = set(CATEGORY_KEYS)
# Lightweight label normalization to catch duplicate/identity conversions
@@ -418,28 +442,12 @@ export interface CalculatorDef {
}
export const categories: Record<string, { label: string; icon: string }> = {
length: { label: 'Length / Distance', icon: '📏' },
weight: { label: 'Weight / Mass', icon: '⚖️' },
temperature: { label: 'Temperature', icon: '🌡️' },
volume: { label: 'Volume', icon: '🧪' },
fluids: { label: 'Fluids', icon: '💧' },
area: { label: 'Area', icon: '🔳' },
speed: { label: 'Speed / Velocity', icon: '💨' },
pressure: { label: 'Pressure', icon: '🔽' },
energy: { label: 'Energy', icon: '' },
magnetism: { label: 'Magnetism', icon: '🧲' },
power: { label: 'Power', icon: '🔌' },
data: { label: 'Data Storage', icon: '💾' },
time: { label: 'Time', icon: '⏱️' },
angle: { label: 'Angle', icon: '📐' },
'number-systems':{ label: 'Number Systems', icon: '🔢' },
radiation: { label: 'Radiation', icon: '☢️' },
electrical: { label: 'Electrical', icon: '🔋' },
force: { label: 'Force / Torque', icon: '💪' },
light: { label: 'Light', icon: '💡' },
other: { label: 'Other', icon: '🔄' },
};
"""
for k, v in CATEGORIES.items():
out += f" '{k}': {json.dumps(v, ensure_ascii=False).replace('{', '{ ').replace('}', ' }')},\n"
out += "};\n"
out += """
export const calculators: CalculatorDef[] = [
"""
for e in calculators_ts_entries:
@@ -453,9 +461,9 @@ export const calculators: CalculatorDef[] = [
out += """
];
"""
const slugIndex = new Map(calculators.map(c => [c.slug, c]));
out += """
export function getCalculatorBySlug(slug: string): CalculatorDef | undefined {
return slugIndex.get(slug);
}
@@ -487,5 +495,16 @@ export function searchCalculators(query: string): CalculatorDef[] {
print(f"Generated {len(calculators_ts_entries)} calculators into calculators.ts")
# Generate stats.ts
active_count = len([e for e in calculators_ts_entries if not e.get('hidden')])
stats_content = f"""// THIS FILE IS AUTO-GENERATED BY migrate.py
export const categories: Record<string, {{ label: string; icon: string }}> = {json.dumps(CATEGORIES, indent=2, ensure_ascii=False)};
export const totalCalculators = {active_count};
"""
with open(STATS_FILE, 'w', encoding='utf-8') as f:
f.write(stats_content)
print(f"Generated stats.ts with {active_count} active calculators")
if __name__ == '__main__':
process()