Skip to content

Commit

Permalink
refactor: rename contains to includes (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylef committed Jun 12, 2020
1 parent c0cb4b0 commit 5646468
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Master

### Enhancements

- `ArrayElement`'s `contains` method has been renamed to `includes` to be
consistent with `Array.includes`.

### Bug Fixes

- Prevent throwing an error when calling `toValue()` on an element with a key
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,13 +450,13 @@ Search the entire tree to find a matching ID.
elTree.getById('some-id');
```

##### contains
##### includes

Test to see if a collection contains the value given. Does a deep equality check.
Test to see if a collection include the value given. Does a deep equality check.

```javascript
var arrayElement = minim.toElement(['a', [1, 2], 'b', 3]);
arrayElement.contains('a'); // returns true
arrayElement.includes('a'); // returns true
```

##### length
Expand Down
15 changes: 13 additions & 2 deletions lib/primitives/ArrayElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class ArrayElement extends Element {
* @memberof ArrayElement.prototype
*/
findByClass(className) {
return this.find(item => item.classes.contains(className));
return this.find(item => item.classes.includes(className));
}

/**
Expand All @@ -274,10 +274,21 @@ class ArrayElement extends Element {
* @param value
* @returns {boolean}
*/
contains(value) {
includes(value) {
return this.content.some(element => element.equals(value));
}

/**
* Looks for matching children using deep equality
* @param value
* @returns {boolean}
* @see includes
* @deprecated method was replaced by includes
*/
contains(value) {
return this.includes(value);
}

// Fantasy Land

/**
Expand Down
20 changes: 20 additions & 0 deletions test/primitives/ArrayElement-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,26 @@ describe('ArrayElement', () => {
});
});

describe('#includes', () => {
it('uses deep equality', () => {
expect(doc.get(2).includes(['not', 'there'])).to.be.false;
expect(doc.get(2).includes(['bar', 4])).to.be.true;
});

context('when given a value that is in the array', () => {
it('returns true', () => {
expect(doc.includes('foobar')).to.be.true;
});
});

context('when given a value that is not in the array', () => {
it('returns false', () => {
expect(doc.includes('not-there')).to.be.false;
});
});
});

// Deprecated functionality
describe('#contains', () => {
it('uses deep equality', () => {
expect(doc.get(2).contains(['not', 'there'])).to.be.false;
Expand Down

0 comments on commit 5646468

Please sign in to comment.