From 56464687a20505364d64723d50226325789f081d Mon Sep 17 00:00:00 2001 From: Kyle Fuller Date: Fri, 12 Jun 2020 10:55:28 +0100 Subject: [PATCH] refactor: rename contains to includes (#128) --- CHANGELOG.md | 5 +++++ README.md | 6 +++--- lib/primitives/ArrayElement.js | 15 +++++++++++++-- test/primitives/ArrayElement-test.js | 20 ++++++++++++++++++++ 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8cd967c..c6f7c0d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index abeb4967..e09f0b71 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/primitives/ArrayElement.js b/lib/primitives/ArrayElement.js index 5e64c8fc..75770b2e 100644 --- a/lib/primitives/ArrayElement.js +++ b/lib/primitives/ArrayElement.js @@ -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)); } /** @@ -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 /** diff --git a/test/primitives/ArrayElement-test.js b/test/primitives/ArrayElement-test.js index 1c496051..c6ac8098 100644 --- a/test/primitives/ArrayElement-test.js +++ b/test/primitives/ArrayElement-test.js @@ -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;