Skip to content

Commit

Permalink
feat(text): replaceAll
Browse files Browse the repository at this point in the history
  • Loading branch information
seb-cr committed May 6, 2023
1 parent 0c3032a commit bdecd5c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,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');
});
```

Expand Down
29 changes: 29 additions & 0 deletions src/regex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Helpers for working with RegExp objects.
*/

/**
* Escapes characters in `string` so that it can be used to build a RegExp.
*
* Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
*
* @param string
*/
export function escapeRegex(string: string): string {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

/**
* Turns a search value (string or RegExp) into a global RegExp.
*
* @param input
*/
export function globalRegex(input: string | RegExp): RegExp {
if (typeof input === 'string') {
return new RegExp(escapeRegex(input), 'g');
}
if (input.global) {
return input;
}
return new RegExp(input, `${input.flags}g`);
}
13 changes: 13 additions & 0 deletions src/text.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { globalRegex } from './regex';

/**
* Class for manipulating a block of text.
*/
Expand All @@ -17,4 +19,15 @@ export class Text {
lines(): string[] {
return this.content.split('\n');
}

/**
* Replace every occurrance of `searchValue` with `replaceValue`.
*
* @param searchValue
* @param replaceValue
*/
replaceAll(searchValue: string | RegExp, replaceValue: string): void {
const pattern = globalRegex(searchValue);
this.content = this.content.replace(pattern, replaceValue);
}
}
26 changes: 26 additions & 0 deletions tests/text.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,30 @@ describe('Text', () => {
expect(text.lines()).to.deep.equal(['one', 'two', 'three']);
});
});

describe('replaceAll', () => {
it('should work with a string', () => {
const text = new Text('one. two. three');
text.replaceAll('.', ',');
expect(text.content).to.equal('one, two, three');
});

it('should work with a non-global regex', () => {
const text = new Text('one. two. three');
text.replaceAll(/./, 'z');
expect(text.content).to.equal('zzzzzzzzzzzzzzz');
});

it('should work with a global regex', () => {
const text = new Text('one. two. three');
text.replaceAll(/\w/g, 'z');
expect(text.content).to.equal('zzz. zzz. zzzzz');
});

it('should cover all lines', () => {
const text = new Text('one.\ntwo.\nthree.');
text.replaceAll('.', '!');
expect(text.content).to.equal('one!\ntwo!\nthree!');
});
});
});

0 comments on commit bdecd5c

Please sign in to comment.