Skip to content

Commit

Permalink
fix: avoid using subclassed array constructor when doing map()
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Apr 25, 2019
1 parent 65195b0 commit ff4cd66
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ exports.get = function (path, o, special, map) {
// reading a property from the array items
var paths = parts.slice(i);

return obj.map(function (item) {
// Need to `concat()` to avoid `map()` calling a constructor of an array
// subclass
return [].concat(obj).map(function (item) {
return item
? exports.get(paths, item, special || lookup, map)
: map(undefined);
Expand Down
15 changes: 15 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1846,6 +1846,21 @@ describe('mpath', function(){
done();
});

it('get() underneath subclassed array', function(done) {
class MyArray extends Array {}

const obj = {
arr: new MyArray()
};
obj.arr.push({ test: 2 });

const arr = mpath.get('arr.test', obj);
assert.equal(arr.constructor.name, 'Array');
assert.ok(!(arr instanceof MyArray));

done();
});

it('ignores setting a nested path that doesnt exist', function(done){
var o = doc();
assert.doesNotThrow(function(){
Expand Down

0 comments on commit ff4cd66

Please sign in to comment.