UNPKG

generator-begcode

Version:

Spring Boot + Angular/React/Vue in one handy generator

51 lines (50 loc) 1.51 kB
const lowestPaddingCount = (text) => text .split('\n') .map(x => { let count = 0; for (const char of x) { if (char === ' ') { count++; } else { break; } } return count; }) .reduce((a, b) => Math.min(a, b), Number.MAX_SAFE_INTEGER); const removePadding = (text) => { const lines = text.split('\n'); const lowestPadding = lowestPaddingCount(text); return lines.map(x => x.substring(lowestPadding, x.length)).join('\n'); }; const removeEmptyFirstLine = (text) => (text.startsWith('\n') ? text.substring(1, text.length) : text); const toPrompt = (text) => removePadding(removeEmptyFirstLine(text)); export class Prompt { _text; constructor(_text = '') { this._text = _text; } text(text) { return new Prompt(this._text + toPrompt(text)); } block(text) { return new Prompt(`${this._text}\`\`\`${toPrompt(text)}\`\`\``); } json(obj) { return new Prompt(`${this._text}\`\`\`${toPrompt(JSON.stringify(obj))}\`\`\``); } line(builder) { if (typeof builder === 'string') { return new Prompt(`${this._text}\n${toPrompt(builder)}`); } const result = builder(new Prompt()); if (typeof result === 'string') { return new Prompt(`${this._text}\n${toPrompt(result)}`); } return new Prompt(`${this._text}\n${result.toString()}`); } toString() { return this._text; } }