Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion typescript/index.ts
Original file line number Diff line number Diff line change
@@ -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<T> = (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<T>(renderer: RecipeRenderer<T>) {
return renderer(this.recipe);
}

get ingredients() {
return this.recipe.ingredients;
}

get metadata() {
return {} // Todo: implement
}

}
5 changes: 3 additions & 2 deletions typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
13 changes: 13 additions & 0 deletions typescript/test/parse.bench.ts
Original file line number Diff line number Diff line change
@@ -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);
});
})