Skip to content

Commit d4cd863

Browse files
Enhance appendLine method to handle empty input and add newlines accordingly
1 parent 37c3877 commit d4cd863

File tree

6 files changed

+39
-5
lines changed

6 files changed

+39
-5
lines changed

dist/cjs/StringBuilder.js

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/cjs/StringBuilder.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/esm/StringBuilder.js

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/esm/StringBuilder.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/StringBuilder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ export default class StringBuilder
6767
}
6868

6969
appendLine(...items: any[]): this {
70-
this.appendLines(items);
70+
if(items?.length) this.appendLines(items);
71+
else this._partArray.push(this.newLine);
7172
return this;
7273
}
7374

tests/StringBuilder.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,33 @@ describe('StringBuilder', () => {
119119
sb.appendLines(['first', 'second']);
120120
expect(sb.toString()).toBe('first\nsecond\n');
121121
});
122+
123+
it('should add newline when appendLine called with no parameters', () => {
124+
const sb = new StringBuilder();
125+
sb.append('content');
126+
sb.appendLine(); // No parameters
127+
sb.append('more');
128+
expect(sb.toString()).toBe('content\nmore');
129+
expect(sb.toString()).toContain('\n');
130+
});
131+
132+
it('should add multiple newlines when appendLine called multiple times with no parameters', () => {
133+
const sb = new StringBuilder();
134+
sb.append('line1');
135+
sb.appendLine(); // First newline
136+
sb.appendLine(); // Second newline (empty line)
137+
sb.append('line2');
138+
expect(sb.toString()).toBe('line1\n\nline2');
139+
});
140+
141+
it('should use custom newline when appendLine called with no parameters', () => {
142+
const sb = new StringBuilder();
143+
sb.setNewLine('\r\n');
144+
sb.append('content');
145+
sb.appendLine(); // Should use \r\n
146+
sb.append('more');
147+
expect(sb.toString()).toBe('content\r\nmore');
148+
});
122149
});
123150

124151
describe('newLine handling', () => {

0 commit comments

Comments
 (0)