Skip to content

Commit

Permalink
fix: make CoreMongooseArray#includes() handle fromIndex parameter
Browse files Browse the repository at this point in the history
Fix #8203
  • Loading branch information
vkarpov15 committed Sep 29, 2019
1 parent 6c91dea commit 98b5a73
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/types/core_array.js
Expand Up @@ -457,8 +457,9 @@ class CoreMongooseArray extends Array {
* @memberOf MongooseArray
*/

includes(obj) {
return this.indexOf(obj) !== -1;
includes(obj, fromIndex) {
const ret = this.indexOf(obj, fromIndex);
return ret !== -1;
}

/**
Expand All @@ -471,11 +472,14 @@ class CoreMongooseArray extends Array {
* @memberOf MongooseArray
*/

indexOf(obj) {
indexOf(obj, fromIndex) {
if (obj instanceof ObjectId) {
obj = obj.toString();
}
for (let i = 0, len = this.length; i < len; ++i) {

fromIndex = fromIndex == null ? 0 : fromIndex;
const len = this.length;
for (let i = fromIndex; i < len; ++i) {
if (obj == this[i]) {
return i;
}
Expand Down
2 changes: 2 additions & 0 deletions test/types.array.test.js
Expand Up @@ -162,6 +162,8 @@ describe('types array', function() {
assert.equal(user.pets.includes(tobi.id), true);
assert.equal(user.pets.includes(loki.id), true);
assert.equal(user.pets.includes(jane.id), true);
assert.equal(user.pets.includes(tobi.id, 1), false);
assert.equal(user.pets.includes(loki.id, 1), true);
done();
});
});
Expand Down

0 comments on commit 98b5a73

Please sign in to comment.