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/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 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