Skip to content

Commit

Permalink
feat(text): append, appendLine, appendLines
Browse files Browse the repository at this point in the history
  • Loading branch information
seb-cr committed May 9, 2023
1 parent 3fda8fd commit a3bdac0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Work with text files using the `withFile` function. It opens the file, passes th
```ts
await withFile('example.txt', (f) => {
f.replaceAll('old', 'new');
f.appendLine('footer');
f.insertLine('fizz', { aboveEvery: 'buzz' });
f.deleteLine('bye');
});
Expand Down
32 changes: 32 additions & 0 deletions src/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,36 @@ export class Text {
});
this.content = lines.join('\n');
}

/**
* Appends `text` to the end of the text.
*
* @param text The text to append.
*/
append(text: string) {
this.content += text;
}

/**
* Appends a line to the end of the text.
*
* @param line The text to append.
*/
appendLine(line: string) {
if (!this.content.endsWith('\n')) {
this.content += '\n';
}
this.content += `${line}\n`;
}

/**
* Appends lines to the end of the text.
*
* @param lines Array of lines to append.
*/
appendLines(lines: string[]) {
if (lines.length > 0) {
this.appendLine(lines.join('\n'));
}
}
}
48 changes: 48 additions & 0 deletions tests/text.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,52 @@ describe('Text', () => {
expect(text.content).to.equal('one\ntwo');
});
});

describe('append', () => {
it('should append the text', () => {
const text = new Text('one\n');
text.append('two');
expect(text.content).to.equal('one\ntwo');
});

it('should not start a new line', () => {
const text = new Text('one\ntwo');
text.append('three');
expect(text.content).to.equal('one\ntwothree');
});
});

describe('appendLine', () => {
it('should append the line with a line ending', () => {
const text = new Text('one\n');
text.appendLine('two');
expect(text.content).to.equal('one\ntwo\n');
});

it('should add a line ending to the previous line if required', () => {
const text = new Text('one');
text.appendLine('two');
expect(text.content).to.equal('one\ntwo\n');
});

it('should not prevent explicitly adding extra linebreaks', () => {
const text = new Text('one\n');
text.appendLine('\ntwo\n');
expect(text.content).to.equal('one\n\ntwo\n\n');
});
});

describe('appendLines', () => {
it('should append the lines with line endings', () => {
const text = new Text('one\n');
text.appendLines(['two', 'three']);
expect(text.content).to.equal('one\ntwo\nthree\n');
});

it('should add a line ending to the previous line if required', () => {
const text = new Text('one');
text.appendLines(['two', 'three']);
expect(text.content).to.equal('one\ntwo\nthree\n');
});
});
});

0 comments on commit a3bdac0

Please sign in to comment.