Skip to content

Commit

Permalink
Fixes Automattic#8317 map(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelrahmanHafez committed Nov 17, 2019
1 parent 6ea62dd commit 7bd3663
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions lib/types/documentarray.js
Expand Up @@ -180,6 +180,15 @@ class CoreDocumentArray extends CoreMongooseArray {
return arr;
}

map() {
const arr = super.map.apply(this,arguments);

arr[arrayParentSymbol] = this[arrayParentSymbol];
arr[arrayPathSymbol] = this[arrayPathSymbol];

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 +337,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.$__.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

0 comments on commit 7bd3663

Please sign in to comment.