feat: Update total calculator count to include all calculators by modifying the migration script.

This commit is contained in:
Ben
2026-03-08 19:34:07 -07:00
parent 3ae77e02a0
commit 379bb60722
6 changed files with 37196 additions and 13 deletions

View File

@@ -1,20 +1,19 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { loadCalculators, searchCalculators, type CalculatorDef } from '$lib/data/calculatorLoader';
export let idPrefix = 'search';
let query = '';
let focused = false;
let selectedIndex = -1;
let lastQuery = '';
let searchCalculatorsFn: ((q: string) => any[]) | null = null;
let allCalcs: CalculatorDef[] = [];
$: if (query.length >= 1 && !searchCalculatorsFn) {
import('$lib/data/calculators').then(m => {
searchCalculatorsFn = m.searchCalculators;
});
$: if (query.length >= 1 && allCalcs.length === 0) {
loadCalculators().then(data => { allCalcs = data; });
}
$: results = (query.length >= 2 && searchCalculatorsFn) ? searchCalculatorsFn(query).slice(0, 8) : [];
$: results = (query.length >= 2 && allCalcs.length > 0) ? searchCalculators(allCalcs, query).slice(0, 8) : [];
$: listboxId = `${idPrefix}-listbox`;
$: inputId = `${idPrefix}-input`;
$: isOpen = focused && results.length > 0;

View File

@@ -3,15 +3,15 @@
import { onMount } from 'svelte';
import { page } from '$app/stores';
import { categories } from '$lib/data/stats';
import type { CalculatorDef } from '$lib/data/calculators';
import { loadCalculators, type CalculatorDef } from '$lib/data/calculatorLoader';
let allCalculators: CalculatorDef[] = [];
let isLoaded = false;
async function loadData() {
if (isLoaded || !browser) return;
const module = await import('$lib/data/calculators');
allCalculators = module.calculators;
const data = await loadCalculators();
allCalculators = data;
isLoaded = true;
}

View File

@@ -0,0 +1,43 @@
// Shared lazy loader fetches /data/calculators.json exactly once.
// Because this is a plain fetch (not a JS dynamic import), Vite will NOT
// emit a modulepreload for it, keeping the homepage bundle small.
export interface CalculatorDef {
slug: string;
name: string;
category: string;
type: string;
teaser: string;
labels: { in1: string; in2: string };
factor?: number;
offset?: number;
hidden?: boolean;
}
let cache: CalculatorDef[] | null = null;
let pending: Promise<CalculatorDef[]> | null = null;
export async function loadCalculators(): Promise<CalculatorDef[]> {
if (cache) return cache;
if (pending) return pending;
pending = fetch('/data/calculators.json')
.then(r => r.json())
.then((data: CalculatorDef[]) => {
cache = data;
pending = null;
return data;
});
return pending;
}
export function searchCalculators(calcs: CalculatorDef[], query: string): CalculatorDef[] {
const q = query.toLowerCase();
return calcs.filter(c =>
(c.name.toLowerCase().includes(q) ||
c.slug.includes(q) ||
c.labels.in1.toLowerCase().includes(q) ||
c.labels.in2.toLowerCase().includes(q)) && !c.hidden
);
}

View File

@@ -82,4 +82,4 @@ export const categories: Record<string, { label: string; icon: string }> = {
}
};
export const totalCalculators = 2460;
export const totalCalculators = 3124;

File diff suppressed because it is too large Load Diff

View File

@@ -1,11 +1,13 @@
import json
import re
import os
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'
CALCULATORS_JSON = BASE_DIR / 'hdyc-svelte/static/data/calculators.json'
CATEGORY_KEYS = [
'length',
@@ -496,15 +498,21 @@ 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')])
total_count = len(calculators_ts_entries)
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};
export const totalCalculators = {total_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")
print(f"Generated stats.ts with {total_count} total calculators")
# Generate calculators.json for true lazy loading
os.makedirs(os.path.dirname(CALCULATORS_JSON), exist_ok=True)
with open(CALCULATORS_JSON, 'w', encoding='utf-8') as f:
json.dump(calculators_ts_entries, f, ensure_ascii=False, indent=2)
print(f"Generated calculators.json (Size: {os.path.getsize(CALCULATORS_JSON) // 1024}KB)")
if __name__ == '__main__':
process()