@nodescript/core
Version:
Visual programming language for Browser and Node
45 lines • 1.12 kB
JavaScript
export class CodeBuilder {
constructor(indentString = ' ', currentIndent = 0) {
this.indentString = indentString;
this.currentIndent = currentIndent;
this.buffer = [];
}
toString() {
return this.buffer.join('');
}
push(str) {
this.buffer.push(str);
return this;
}
pushIndent() {
this.buffer.push(this.indentString.repeat(this.currentIndent));
return this;
}
line(str) {
this.pushIndent();
this.push(str);
this.push('\n');
return this;
}
indent(step = 1) {
this.currentIndent += step;
return this;
}
block(start, end, fn) {
this.line(start);
this.indent();
fn();
this.indent(-1);
this.line(end);
}
compose(async, expr, wrap) {
return async ? this.composeAsync(expr, wrap) : this.composeSync(expr, wrap);
}
composeSync(expr, wrap) {
return wrap(expr);
}
composeAsync(expr, wrap) {
return `${expr}.then(_ => ${wrap('_')})`;
}
}
//# sourceMappingURL=CodeBuilder.js.map