Skip to content

Commit

Permalink
feat(text): contains
Browse files Browse the repository at this point in the history
  • Loading branch information
seb-cr committed May 22, 2023
1 parent c65adca commit 484612f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ export class Text {
return this.content.split('\n');
}

/**
* Returns `true` if the text contains `value`.
*
* @param value The string or RegExp to search for.
*/
contains(value: string | RegExp): boolean {
return typeof value === 'string'
? this.content.includes(value)
: value.exec(this.content) !== null;
}

/**
* Replace every occurrance of `searchValue` with `replaceValue`.
*
Expand Down
26 changes: 26 additions & 0 deletions tests/text.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,32 @@ describe('Text', () => {
});
});

describe('contains', () => {
context('with a string', () => {
it('should return true if text contains the string', () => {
const text = new Text('one\ntwo\nthree');
expect(text.contains('two')).to.be.true;
});

it('should return false if text contains the string', () => {
const text = new Text('one\ntwo\nthree');
expect(text.contains('blah')).to.be.false;
});
});

context('with a regex', () => {
it('should return true if text matches the regex', () => {
const text = new Text('one\ntwo\nthree');
expect(text.contains(/two/)).to.be.true;
});

it('should return false if text matches the regex', () => {
const text = new Text('one\ntwo\nthree');
expect(text.contains(/blah/)).to.be.false;
});
});
});

describe('replaceAll', () => {
it('should work with a string', () => {
const text = new Text('one. two. three');
Expand Down

0 comments on commit 484612f

Please sign in to comment.