Skip to content

Commit 30b50c4

Browse files
committed
feat: enable eol in buffer
1 parent 252dc30 commit 30b50c4

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/file_buffer.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
* console.log(buffer.flush())
2626
*/
2727
export class FileBuffer {
28+
/**
29+
* Whether to add an end-of-line character at the end of the output
30+
*/
31+
#eol: boolean = false
32+
2833
/**
2934
* Collected lines
3035
*/
@@ -60,6 +65,21 @@ export class FileBuffer {
6065
return this.#buffer.length
6166
}
6267

68+
/**
69+
* Enable or disable end-of-line character at the end of output
70+
*
71+
* @param enabled - Whether to add EOL character
72+
* @returns This FileBuffer instance for method chaining
73+
*
74+
* @example
75+
* const buffer = new FileBuffer()
76+
* buffer.eol(true).writeLine('Hello').flush() // 'Hello\n\n'
77+
*/
78+
eol(enabled: boolean) {
79+
this.#eol = enabled
80+
return this
81+
}
82+
6383
/**
6484
* Write a new line to the output with current indentation
6585
*
@@ -115,6 +135,14 @@ export class FileBuffer {
115135
return this
116136
}
117137

138+
/**
139+
* Convert the buffer to a string representation
140+
*
141+
* @example
142+
* const buffer = new FileBuffer()
143+
* buffer.writeLine('Hello')
144+
* console.log(buffer.toString())
145+
*/
118146
toString() {
119147
return this.flush()
120148
}
@@ -133,6 +161,6 @@ export class FileBuffer {
133161
}
134162

135163
this.#compiledOutput = this.#buffer.join('\n')
136-
return this.#compiledOutput
164+
return this.#eol ? `${this.#compiledOutput}\n` : this.#compiledOutput
137165
}
138166
}

0 commit comments

Comments
 (0)