From 3e523631daa48a910b5335c747b3e5d080966e6d Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sat, 11 May 2019 16:14:46 -0400 Subject: [PATCH] refactor(types): make array and documentarray both inherit from CoreMongooseArray class Re: #7700 --- lib/types/array.js | 10 ++++++++-- lib/types/core_array.js | 15 +++++++++++++++ lib/types/documentarray.js | 13 +------------ 3 files changed, 24 insertions(+), 14 deletions(-) create mode 100644 lib/types/core_array.js diff --git a/lib/types/array.js b/lib/types/array.js index e59ca81b967..e7d6a64448f 100644 --- a/lib/types/array.js +++ b/lib/types/array.js @@ -4,6 +4,7 @@ 'use strict'; +const CoreMongooseArray = require('./core_array'); const EmbeddedDocument = require('./embedded'); const Document = require('../document'); const ObjectId = require('./objectid'); @@ -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; @@ -44,7 +51,6 @@ function MongooseArray(values, path, doc) { } arr[arrayPathSymbol] = path; - arr.isMongooseArray = true; arr.validators = []; arr[arrayAtomicsSymbol] = {}; arr[arraySchemaSymbol] = void 0; diff --git a/lib/types/core_array.js b/lib/types/core_array.js new file mode 100644 index 00000000000..82d9ff9b0d0 --- /dev/null +++ b/lib/types/core_array.js @@ -0,0 +1,15 @@ +'use strict'; + +/*! + * ignore + */ + +class CoreMongooseArray extends Array { + get isMongooseArray() { + return true; + } + + remove() {} +} + +module.exports = CoreMongooseArray; \ No newline at end of file diff --git a/lib/types/documentarray.js b/lib/types/documentarray.js index b75adaea084..3997092bde8 100644 --- a/lib/types/documentarray.js +++ b/lib/types/documentarray.js @@ -4,6 +4,7 @@ * Module dependencies. */ +const CoreMongooseArray = require('./core_array'); const Document = require('../document'); const MongooseArray = require('./array'); const ObjectId = require('./objectid'); @@ -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 *