11'use strict'
22
3- const util = require ( 'util ' )
3+ const formatString = require ( './format ' )
44
55/**
66 * Helper object to format and aggragate lines of code.
@@ -13,6 +13,7 @@ const util = require('util')
1313 */
1414const CodeBuilder = function ( indentation , join ) {
1515 this . code = [ ]
16+ this . indentLevel = 0
1617 this . indentation = indentation
1718 this . lineJoin = join || '\n'
1819}
@@ -42,7 +43,7 @@ CodeBuilder.prototype.buildLine = function (indentationLevel, line) {
4243 if ( Object . prototype . toString . call ( indentationLevel ) === '[object String]' ) {
4344 slice = 1
4445 line = indentationLevel
45- indentationLevel = 0
46+ indentationLevel = this . indentLevel
4647 } else if ( indentationLevel === null ) {
4748 return null
4849 }
@@ -55,7 +56,7 @@ CodeBuilder.prototype.buildLine = function (indentationLevel, line) {
5556 const format = Array . prototype . slice . call ( arguments , slice , arguments . length )
5657 format . unshift ( lineIndentation + line )
5758
58- return util . format . apply ( this , format )
59+ return formatString . apply ( this , format )
5960}
6061
6162/**
@@ -100,4 +101,31 @@ CodeBuilder.prototype.join = function () {
100101 return this . code . join ( this . lineJoin )
101102}
102103
104+ /**
105+ * Increase indentation level
106+ * @returns {this }
107+ */
108+ CodeBuilder . prototype . indent = function ( ) {
109+ this . indentLevel ++
110+ return this
111+ }
112+
113+ /**
114+ * Decrease indentation level
115+ * @returns {this }
116+ */
117+ CodeBuilder . prototype . unindent = function ( ) {
118+ this . indentLevel --
119+ return this
120+ }
121+
122+ /**
123+ * Reset indentation level
124+ * @returns {this }
125+ */
126+ CodeBuilder . prototype . reindent = function ( ) {
127+ this . indentLevel = 0
128+ return this
129+ }
130+
103131module . exports = CodeBuilder
0 commit comments