Skip to content

Commit

Permalink
refactor(types): make array and documentarray both inherit from CoreM…
Browse files Browse the repository at this point in the history
…ongooseArray class

Re: #7700
  • Loading branch information
vkarpov15 committed May 11, 2019
1 parent 0dc8564 commit 3e52363
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
10 changes: 8 additions & 2 deletions lib/types/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

'use strict';

const CoreMongooseArray = require('./core_array');
const EmbeddedDocument = require('./embedded');
const Document = require('../document');
const ObjectId = require('./objectid');
Expand Down Expand Up @@ -35,7 +36,13 @@ const isMongooseObject = utils.isMongooseObject;
*/

function MongooseArray(values, path, doc) {
const arr = [].concat(values);
// TODO: replace this with `new CoreMongooseArray().concat()` when we remove
// support for node 4.x and 5.x, see https://i.imgur.com/UAAHk4S.png
const arr = new CoreMongooseArray();

if (Array.isArray(values)) {
values.forEach(v => { arr.push(v); });
}

const keysMA = Object.keys(MongooseArray.mixin);
const numKeys = keysMA.length;
Expand All @@ -44,7 +51,6 @@ function MongooseArray(values, path, doc) {
}

arr[arrayPathSymbol] = path;
arr.isMongooseArray = true;
arr.validators = [];
arr[arrayAtomicsSymbol] = {};
arr[arraySchemaSymbol] = void 0;
Expand Down
15 changes: 15 additions & 0 deletions lib/types/core_array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

/*!
* ignore
*/

class CoreMongooseArray extends Array {
get isMongooseArray() {
return true;
}

remove() {}
}

module.exports = CoreMongooseArray;
13 changes: 1 addition & 12 deletions lib/types/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Module dependencies.
*/

const CoreMongooseArray = require('./core_array');
const Document = require('../document');
const MongooseArray = require('./array');
const ObjectId = require('./objectid');
Expand All @@ -19,18 +20,6 @@ const arrayPathSymbol = require('../helpers/symbols').arrayPathSymbol;
const arraySchemaSymbol = require('../helpers/symbols').arraySchemaSymbol;
const documentArrayParent = require('../helpers/symbols').documentArrayParent;

/*!
* ignore
*/

class CoreMongooseArray extends Array {
get isMongooseArray() {
return true;
}

remove() {}
}

/**
* DocumentArray constructor
*
Expand Down

0 comments on commit 3e52363

Please sign in to comment.