11'use strict'
22
3+ var util = require ( 'util' )
4+
35/**
46 * Helper object to format and aggragate lines of code.
57 * Lines are aggregated in a `code` array, and need to be joined to obtain a proper code snippet.
@@ -16,14 +18,29 @@ var CodeBuilder = function (indentation, join) {
1618}
1719
1820/**
19- * Add given indentation level to given string
20- * @param {number } [indentationLevel=0] Desired level of indentation for this line
21- * @param {string } line Line of code
21+ * Add given indentation level to given string and format the string (variadic)
22+ * @param {number } [indentationLevel=0] - Desired level of indentation for this line
23+ * @param {string } line - Line of code. Can contain formatting placeholders
24+ * @param {...anyobject } - Parameter to bind to `line`'s formatting placeholders
2225 * @return {string }
26+ *
27+ * @example
28+ * var builder = CodeBuilder('\t')
29+ *
30+ * builder.buildLine('console.log("hello world")')
31+ * // returns: 'console.log("hello world")'
32+ *
33+ * builder.buildLine(2, 'console.log("hello world")')
34+ * // returns: 'console.log("\t\thello world")'
35+ *
36+ * builder.buildLine(2, 'console.log("%s %s")', 'hello', 'world')
37+ * // returns: 'console.log("\t\thello world")'
2338 */
2439CodeBuilder . prototype . buildLine = function ( indentationLevel , line ) {
2540 var lineIndentation = ''
41+ var slice = 2
2642 if ( Object . prototype . toString . call ( indentationLevel ) === '[object String]' ) {
43+ slice = 1
2744 line = indentationLevel
2845 indentationLevel = 0
2946 } else if ( indentationLevel === null ) {
@@ -35,29 +52,32 @@ CodeBuilder.prototype.buildLine = function (indentationLevel, line) {
3552 indentationLevel --
3653 }
3754
38- return lineIndentation + line
55+ var format = Array . prototype . slice . call ( arguments , slice , arguments . length )
56+ format . unshift ( lineIndentation + line )
57+
58+ return util . format . apply ( this , format )
3959}
4060
4161/**
42- * Add a line at the top of current lines with given indentation level
62+ * Invoke buildLine() and add the line at the top of current lines
4363 * @param {number } [indentationLevel=0] Desired level of indentation for this line
4464 * @param {string } line Line of code
4565 * @return {this }
4666 */
47- CodeBuilder . prototype . unshift = function ( indentationLevel , str ) {
48- this . code . unshift ( this . buildLine ( indentationLevel , str ) )
67+ CodeBuilder . prototype . unshift = function ( ) {
68+ this . code . unshift ( this . buildLine . apply ( this , arguments ) )
4969
5070 return this
5171}
5272
5373/**
54- * Add a line at the end of current lines with given indentation level
74+ * Invoke buildLine() and add the line at the bottom of current lines
5575 * @param {number } [indentationLevel=0] Desired level of indentation for this line
5676 * @param {string } line Line of code
5777 * @return {this }
5878 */
59- CodeBuilder . prototype . push = function ( indentationLevel , str ) {
60- this . code . push ( this . buildLine ( indentationLevel , str ) )
79+ CodeBuilder . prototype . push = function ( ) {
80+ this . code . push ( this . buildLine . apply ( this , arguments ) )
6181
6282 return this
6383}
0 commit comments