Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
103 lines (89 loc) · 2.79 KB

File metadata and controls

103 lines (89 loc) · 2.79 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'use strict'
var util = require('util')
/**
* Helper object to format and aggragate lines of code.
* Lines are aggregated in a `code` array, and need to be joined to obtain a proper code snippet.
*
* @class
*
* @param {string} indentation Desired indentation character for aggregated lines of code
* @param {string} join Desired character to join each line of code
*/
var CodeBuilder = function (indentation, join) {
this.code = []
this.indentation = indentation
this.lineJoin = join || '\n'
}
/**
* Add given indentation level to given string and format the string (variadic)
* @param {number} [indentationLevel=0] - Desired level of indentation for this line
* @param {string} line - Line of code. Can contain formatting placeholders
* @param {...anyobject} - Parameter to bind to `line`'s formatting placeholders
* @return {string}
*
* @example
* var builder = CodeBuilder('\t')
*
* builder.buildLine('console.log("hello world")')
* // returns: 'console.log("hello world")'
*
* builder.buildLine(2, 'console.log("hello world")')
* // returns: 'console.log("\t\thello world")'
*
* builder.buildLine(2, 'console.log("%s %s")', 'hello', 'world')
* // returns: 'console.log("\t\thello world")'
*/
CodeBuilder.prototype.buildLine = function (indentationLevel, line) {
var lineIndentation = ''
var slice = 2
if (Object.prototype.toString.call(indentationLevel) === '[object String]') {
slice = 1
line = indentationLevel
indentationLevel = 0
} else if (indentationLevel === null) {
return null
}
while (indentationLevel) {
lineIndentation += this.indentation
indentationLevel--
}
var format = Array.prototype.slice.call(arguments, slice, arguments.length)
format.unshift(lineIndentation + line)
return util.format.apply(this, format)
}
/**
* Invoke buildLine() and add the line at the top of current lines
* @param {number} [indentationLevel=0] Desired level of indentation for this line
* @param {string} line Line of code
* @return {this}
*/
CodeBuilder.prototype.unshift = function () {
this.code.unshift(this.buildLine.apply(this, arguments))
return this
}
/**
* Invoke buildLine() and add the line at the bottom of current lines
* @param {number} [indentationLevel=0] Desired level of indentation for this line
* @param {string} line Line of code
* @return {this}
*/
CodeBuilder.prototype.push = function () {
this.code.push(this.buildLine.apply(this, arguments))
return this
}
/**
* Add an empty line at the end of current lines
* @return {this}
*/
CodeBuilder.prototype.blank = function () {
this.code.push(null)
return this
}
/**
* Concatenate all current lines using the given lineJoin
* @return {string}
*/
CodeBuilder.prototype.join = function () {
return this.code.join(this.lineJoin)
}
module.exports = CodeBuilder
Morty Proxy This is a proxified and sanitized view of the page, visit original site.