|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +class CodeBuilder { |
| 5 | + /** |
| 6 | + * |
| 7 | + * @param {Number} indentSize |
| 8 | + * @param {String} indentCharacter |
| 9 | + */ |
| 10 | + constructor (indentSize, indentCharacter) { |
| 11 | + this.indentSize = indentSize; |
| 12 | + this.indentCharacter = indentCharacter; |
| 13 | + this.currentIndentCount = 0; |
| 14 | + this.snippet = ''; |
| 15 | + this.usings = []; |
| 16 | + this.newLineChar = '\n'; |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * |
| 21 | + * @param {String} using - The namespace that is needed to be imported in order |
| 22 | + * for the code to be able to be built on it's own |
| 23 | + */ |
| 24 | + addUsing (using) { |
| 25 | + this.usings.push(using); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * |
| 30 | + * @param {String} line - the line to be appended |
| 31 | + */ |
| 32 | + appendLine (line) { |
| 33 | + this.snippet += this.indentation + line + this.newLineChar; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * |
| 38 | + * @param {String[]} lines |
| 39 | + */ |
| 40 | + appendLines (lines) { |
| 41 | + lines.forEach((l) => { |
| 42 | + this.appendLine(l); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * |
| 48 | + * @param {String} body - the value to append to the running block of code |
| 49 | + */ |
| 50 | + append (body) { |
| 51 | + this.snippet += body; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * |
| 56 | + * @param {String} line - the line to be appened followed by the start of a new block |
| 57 | + */ |
| 58 | + appendBlock (line) { |
| 59 | + this.snippet += this.indentation + line + this.newLineChar + |
| 60 | + this.indentation + '{' + this.newLineChar; |
| 61 | + this.currentIndentCount++; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * |
| 66 | + * @param {String} extra - value to add after closing brace |
| 67 | + */ |
| 68 | + endBlock (extra) { |
| 69 | + if (!extra) { |
| 70 | + extra = ''; |
| 71 | + } |
| 72 | + this.currentIndentCount--; |
| 73 | + this.snippet += this.indentation + '}' + extra + this.newLineChar; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * |
| 78 | + * @param {Boolean} addUsings - Whether to actually append the usings at the top |
| 79 | + * |
| 80 | + * @returns {String} the full block of code |
| 81 | + */ |
| 82 | + build (addUsings) { |
| 83 | + if (addUsings) { |
| 84 | + var builder = new CodeBuilder(this.indentSize, this.indentCharacter); |
| 85 | + this.uniqueUsings().forEach((using) => { |
| 86 | + builder.appendLine(`using ${using};`); |
| 87 | + }); |
| 88 | + builder.append(this.snippet); |
| 89 | + return builder.build(false); |
| 90 | + } |
| 91 | + |
| 92 | + return this.snippet; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @returns {Array<String>} the unique usings |
| 97 | + */ |
| 98 | + uniqueUsings () { |
| 99 | + var arr = [], |
| 100 | + i = 0; |
| 101 | + for (i = 0; i < this.usings.length; i++) { |
| 102 | + if (!arr.includes(this.usings[i])) { |
| 103 | + arr.push(this.usings[i]); |
| 104 | + } |
| 105 | + } |
| 106 | + return arr.sort(); |
| 107 | + } |
| 108 | + |
| 109 | + get indentation () { |
| 110 | + return this.indentCharacter.repeat(this.indentSize * this.currentIndentCount); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +module.exports = CodeBuilder; |
0 commit comments