Tofu/app/services/recipes/createStaticRecipeSource.js

28 lines
1.1 KiB
JavaScript

import { normalizeSearchText } from '#tofu/tools/text/normalizeSearchText.js'
// Recipe source backed by an index already held in memory (the startup
// fixture, or out/index.json once the import script has run).
//
// Both methods are asynchronous ON PURPOSE even though nothing here waits:
// a future createWebdavRecipeSource(client) implements the same contract and
// can be substituted without touching a single caller. One contract, several
// implementations — that is what makes this brick replaceable.
export function createStaticRecipeSource(index) {
async function getIndex() {
return { ok: true, index }
}
// Case- and accent-insensitive: « gnocchis a la sauge » finds
// « Gnocchis à la sauge ».
async function findByName(name) {
const normalizedName = normalizeSearchText(name)
if (normalizedName === '') return { ok: true, recipe: null }
const recipe = index.recipes.find(
(candidate) => normalizeSearchText(candidate.name) === normalizedName
)
return { ok: true, recipe: recipe === undefined ? null : recipe }
}
return { getIndex, findByName }
}