Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #8317 map(...) #8351

Merged
merged 7 commits into from
Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions lib/types/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ class CoreDocumentArray extends CoreMongooseArray {
return arr;
}

map() {
const arr = [].concat(Array.prototype.map.apply(this,arguments));
AbdelrahmanHafez marked this conversation as resolved.
Show resolved Hide resolved
return arr;
}

/**
* Wraps [`Array#push`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/push) with proper change tracking.
*
Expand Down Expand Up @@ -328,18 +333,18 @@ if (util.inspect.custom) {

function _updateParentPopulated(arr) {
const parent = arr[arrayParentSymbol];
if (parent.$__.populated != null) {
const populatedPaths = Object.keys(parent.$__.populated).
filter(p => p.startsWith(arr[arrayPathSymbol] + '.'));
if (!parent || parent.$__.populated == null) return;

for (const path of populatedPaths) {
const remnant = path.slice((arr[arrayPathSymbol] + '.').length);
if (!Array.isArray(parent.$__.populated[path].value)) {
continue;
}
const populatedPaths = Object.keys(parent.$__.populated).
filter(p => p.startsWith(arr[arrayPathSymbol] + '.'));

parent.$__.populated[path].value = arr.map(val => val.populated(remnant));
for (const path of populatedPaths) {
const remnant = path.slice((arr[arrayPathSymbol] + '.').length);
if (!Array.isArray(parent.$__.populated[path].value)) {
continue;
}

parent.$__.populated[path].value = arr.map(val => val.populated(remnant));
}
}

Expand Down
34 changes: 34 additions & 0 deletions test/types.documentarray.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,40 @@ describe('types.documentarray', function() {
assert.equal(arr.length, 1);
assert.equal(doc.docs.length, 2);
});

it('map() works and does not return a mongoose array', function() {
const personSchema = new Schema({ friends: [{ name: { type: String } }]});
const Person = mongoose.model('gh8317-map', personSchema);

const person = new Person({ friends: [{ name: 'Hafez' }] });

const friendsNames = person.friends.map(friend => friend.name);
assert.deepEqual(friendsNames.constructor, Array);

friendsNames.push('Sam');

assert.equal(friendsNames.length, 2);
assert.equal(friendsNames[1], 'Sam');
});

it('filter() copies parent and path (gh-8317)', function() {
const personSchema = new Schema({ friends: [{ name: { type: String }, age: { type: Number } }] });
const Person = mongoose.model('gh8317-filter', personSchema);

const person = new Person({ friends: [
{ name: 'Hafez', age: 25 },
{ name: 'Sam', age: 27 }
] });

const filteredFriends = person.friends.filter(friend => friend.age > 26);
assert.ok(filteredFriends.isMongooseArray);
assert.equal(filteredFriends.constructor.name, 'CoreDocumentArray');

filteredFriends.push({ name: 'John', age: 30 });

assert.equal(filteredFriends.length, 2);
assert.equal(filteredFriends[1].name, 'John');
});
});

it('cleans modified subpaths on splice() (gh-7249)', function() {
Expand Down