refactor: move scaler out of $lib/server so it can run in browser

RecipeView needs scaleIngredients on the client for live portion scaling.
Moved scaler.ts from $lib/server/recipes/ to $lib/recipes/.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-17 15:41:20 +02:00
parent 4d7783dd8b
commit 0f9aabe76b
4 changed files with 25 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
<script lang="ts">
import { scaleIngredients } from '$lib/server/recipes/scaler';
import { scaleIngredients } from '$lib/recipes/scaler';
import type { Recipe } from '$lib/types';
type Props = {

View File

@@ -0,0 +1,23 @@
import type { PageServerLoad } from './$types';
import { error } from '@sveltejs/kit';
import { getDb } from '$lib/server/db';
import { getRecipeById } from '$lib/server/recipes/repository';
import { scaleIngredients } from '$lib/recipes/scaler';
export const load: PageServerLoad = async ({ params, url }) => {
const id = Number(params.id);
if (!Number.isInteger(id) || id <= 0) error(400, { message: 'Invalid id' });
const recipe = getRecipeById(getDb(), id);
if (!recipe) error(404, { message: 'Nicht gefunden' });
const servingsParam = Number(url.searchParams.get('servings') ?? '');
const servings =
Number.isInteger(servingsParam) && servingsParam > 0
? servingsParam
: (recipe.servings_default ?? 4);
const defaultServings = recipe.servings_default ?? 4;
const factor = servings / defaultServings;
const scaled = scaleIngredients(recipe.ingredients, factor);
return { recipe, ingredients: scaled, servings };
};

View File

@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { scaleIngredients, roundQuantity } from '../../src/lib/server/recipes/scaler';
import { scaleIngredients, roundQuantity } from '../../src/lib/recipes/scaler';
import type { Ingredient } from '../../src/lib/types';
const mk = (q: number | null, unit: string | null, name: string): Ingredient => ({