@achingbrain/chai-string
Version:
strings comparison matchers for chai
128 lines (100 loc) • 2.87 kB
Markdown
Fork of [chai-string](https://www.npmjs.com/package/chai-string)
- Published as ESM
- Compatible with Chai 5
- Removes `chai.string.XXX`, only supports `expect`/`assert` style assertions
Matchers for chai to help with common string comparison assertions.
[](https://travis-ci.org/onechiporenko/chai-string)
[](http://img.shields.io/npm/dm/chai-string.svg)
```html
<script src="chai.js"></script>
<script src="chai-string.js"></script>
```
```javascript
import * as chai from 'chai'
import string from '@achingbrain/chai-string'
chai.use(require('chai-string'))
```
* startsWith/startWith
* endsWith/endWith
* equalIgnoreCase
* equalIgnoreSpaces
* containIgnoreSpaces
* containIgnoreCase
* singleLine
* reverseOf
* palindrome
* entriesCount
* indexOf
All assertions are defined for both the BDD and TDD syntax but some aliases exist to make the assertions look good with both styles.
```javascript
var d1 = 'abcdef',
d2 = 'abc';
d1.should.startWith.d2
expect(d1).to.startsWith(d2)
assert.startsWith(d1, d2)
```
### startsWith/startWith
```javascript
assert.startsWith('abcdef', 'abc');
expect('abcdef').to.startsWith('abc');
'abcdef'.should.startWith('abc');
```
```javascript
assert.endsWith('abcdef', 'def');
expect('abcdef').to.endsWith('def');
'abcdef'.should.endWith('def');
```
```javascript
assert.equalIgnoreCase('abcdef', 'AbCdEf');
expect('abcdef').to.equalIgnoreCase('AbCdEf');
```
```javascript
assert.equalIgnoreSpaces('abcdef', 'a\nb\tc\r d ef');
expect('abcdef').to.equalIgnoreSpaces('a\nb\tc\r d ef');
```
```javascript
assert.containIgnoreSpaces('abcdefgh', 'a\nb\tc\r d ef');
expect('abcdefgh').to.containIgnoreSpaces('a\nb\tc\r d ef');
```
```javascript
assert.containIgnoreCase('abcdefgh', 'AbcDefGH');
expect('abcdefgh').to.containIgnoreCase('AbcDefGH');
'abcdef'.should.containIgnoreCase('cDe');
```
```javascript
assert.singleLine('abcdef');
expect('abcdef').to.be.singleLine();
```
```javascript
assert.reverseOf('abcdef', 'fedcba');
expect('abcdef').to.be.reverseOf('fedcba');
```
```javascript
assert.palindrome('abccba');
expect('abccba').to.be.palindrome();
```
```javascript
assert.entriesCount('abcabd', 'ab', 2);
expect('abcabd').to.have.entriesCount('ab', 2);
```
```javascript
assert.indexOf('abcabd', 'ab', 0);
expect('abcabd').to.have.indexOf('ab', 0);
```
Thanks to the [chai-datetime](https://github.com/gaslight/chai-datetime) module for giving me an idea for how to structure and test a chai plugin.