UNPKG

@alexanderson1993/cooklang-ts

Version:

Cooklang-TS is a TypeScript library for parsing and manipulating Cooklang recipes.

61 lines 2.33 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Parser_1 = __importDefault(require("./Parser")); class Recipe { constructor(source, options) { this.ingredients = []; this.cookwares = []; this.metadata = {}; this.steps = []; this.shoppingList = {}; this.parser = new Parser_1.default(options); if (source) Object.assign(this, this.parser.parse(source)); } toCooklang() { let metadataStr = ''; let stepStrs = []; let shoppingListStrs = []; for (let [key, value] of Object.entries(this.metadata)) { metadataStr += `>> ${key}: ${value}\n`; } for (let step of this.steps) { let stepStr = ''; for (let item of step) { if ('value' in item) { stepStr += item.value; } else { if (item.type == 'ingredient') stepStr += '@'; else if (item.type == 'cookware') stepStr += '#'; else stepStr += '~'; stepStr += item.name; stepStr += '{'; if (item.quantity) stepStr += item.quantity; if ('units' in item && item.units) stepStr += '%' + item.units; stepStr += '}'; if ('preparation' in item && item.preparation) stepStr += `(${item.preparation})`; } } stepStrs.push(stepStr); } for (let [category, items] of Object.entries(this.shoppingList)) { let shoppingListStr = ''; shoppingListStr += category + '\n'; shoppingListStr += items.map(x => x.name + (x.synonym ? '|' + x.synonym : '')).join('\n'); shoppingListStrs.push(shoppingListStr); } return [metadataStr, stepStrs.join('\n\n'), shoppingListStrs.join('\n\n')].join('\n'); } } exports.default = Recipe; //# sourceMappingURL=Recipe.js.map