Skip to content

Commit 484612f

Browse files
committed
feat(text): contains
1 parent c65adca commit 484612f

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

src/text.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ export class Text {
5353
return this.content.split('\n');
5454
}
5555

56+
/**
57+
* Returns `true` if the text contains `value`.
58+
*
59+
* @param value The string or RegExp to search for.
60+
*/
61+
contains(value: string | RegExp): boolean {
62+
return typeof value === 'string'
63+
? this.content.includes(value)
64+
: value.exec(this.content) !== null;
65+
}
66+
5667
/**
5768
* Replace every occurrance of `searchValue` with `replaceValue`.
5869
*

tests/text.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,32 @@ describe('Text', () => {
2323
});
2424
});
2525

26+
describe('contains', () => {
27+
context('with a string', () => {
28+
it('should return true if text contains the string', () => {
29+
const text = new Text('one\ntwo\nthree');
30+
expect(text.contains('two')).to.be.true;
31+
});
32+
33+
it('should return false if text contains the string', () => {
34+
const text = new Text('one\ntwo\nthree');
35+
expect(text.contains('blah')).to.be.false;
36+
});
37+
});
38+
39+
context('with a regex', () => {
40+
it('should return true if text matches the regex', () => {
41+
const text = new Text('one\ntwo\nthree');
42+
expect(text.contains(/two/)).to.be.true;
43+
});
44+
45+
it('should return false if text matches the regex', () => {
46+
const text = new Text('one\ntwo\nthree');
47+
expect(text.contains(/blah/)).to.be.false;
48+
});
49+
});
50+
});
51+
2652
describe('replaceAll', () => {
2753
it('should work with a string', () => {
2854
const text = new Text('one. two. three');

0 commit comments

Comments
 (0)