From 066aa7c8500051d7fca1e369f6af3c56ee7b09aa Mon Sep 17 00:00:00 2001 From: Matt Panzer Date: Thu, 21 Aug 2025 22:07:32 -0400 Subject: [PATCH 1/2] API suggestion with bench test --- typescript/index.ts | 43 +++++++++++++++++++++++++++++++++- typescript/test/parse.bench.ts | 13 ++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 typescript/test/parse.bench.ts diff --git a/typescript/index.ts b/typescript/index.ts index affb57a..3c47681 100644 --- a/typescript/index.ts +++ b/typescript/index.ts @@ -1,4 +1,45 @@ -import { version, Parser } from "./pkg/cooklang_wasm"; +import { version, Parser, Recipe } from "./pkg/cooklang_wasm"; export { version, Parser }; export type { ScaledRecipeWithReport } from "./pkg/cooklang_wasm"; + +type RecipeRenderer = (recipe: Recipe) => T; + +export class CooklangRecipe { + + private recipe: Recipe; + + static parser = new Parser(); + + static fromString(raw: string) { + const recipe = CooklangRecipe.parser.parse(raw); + return new CooklangRecipe(recipe.recipe); + } + + static fromStringNaive(raw: string) { + const parser = new Parser(); + const recipe = parser.parse(raw); + return new CooklangRecipe(recipe.recipe); + } + + static async fromFile(fp: File) { + return CooklangRecipe.fromString(await fp.text()); + } + + constructor(recipe: Recipe) { + this.recipe = recipe; + } + + public render(renderer: RecipeRenderer) { + return renderer(this.recipe); + } + + get ingredients() { + return this.recipe.ingredients; + } + + get metadata() { + return {} // Todo: implement + } + +} diff --git a/typescript/test/parse.bench.ts b/typescript/test/parse.bench.ts new file mode 100644 index 0000000..a012436 --- /dev/null +++ b/typescript/test/parse.bench.ts @@ -0,0 +1,13 @@ +import { describe, bench } from "vitest"; +import { CooklangRecipe } from ".."; + +describe("Recipe Parse", async () => { + bench("optimized", async () => { + const recipeRaw = "this could be a @recipe"; + CooklangRecipe.fromString(recipeRaw); + }); + bench("naive", async () => { + const recipeRaw = "this could be a @recipe"; + CooklangRecipe.fromStringNaive(recipeRaw); + }); +}) \ No newline at end of file From 7697010a1ed24f79aa939d5e1da5a5ac865d3bd8 Mon Sep 17 00:00:00 2001 From: Matt Panzer Date: Thu, 21 Aug 2025 22:08:24 -0400 Subject: [PATCH 2/2] Add bench test command --- typescript/package.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/typescript/package.json b/typescript/package.json index 8219ffb..168677f 100644 --- a/typescript/package.json +++ b/typescript/package.json @@ -23,11 +23,12 @@ "build-wasm": "wasm-pack build --target bundler", "watch-wasm": "wasm-pack build --dev --mode no-install --target bundler", "prepare": "npm run build-wasm && npm run build", - "test": "vitest" + "test": "vitest", + "bench": "vitest bench" }, "devDependencies": { "vite-plugin-wasm": "^3.4.1", "vitest": "^3.2.3", "wasm-pack": "^0.13.1" } -} +} \ No newline at end of file