Skip to content

Commit

Permalink
refactor: clean up someCondition re: #9121
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jul 26, 2021
1 parent 77fc985 commit 3a900de
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
19 changes: 4 additions & 15 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ function $applyDefaultsToNested(val, path, doc) {
const len = pieces.length;

if (type.defaultValue === void 0) {
//continue;
continue;
}

let cur = val;
Expand Down Expand Up @@ -1010,6 +1010,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
key = keys[i];
const pathName = prefix + key;
pathtype = this.$__schema.pathType(pathName);
const valForKey = path[key];

// On initial set, delete any nested keys if we're going to overwrite
// them to ensure we keep the user's key order.
Expand All @@ -1027,21 +1028,9 @@ Document.prototype.$set = function $set(path, val, type, options) {
options = Object.assign({}, options, { _skipMinimizeTopLevel: false });
}

const someCondition = typeof path[key] === 'object' &&
!utils.isNativeObject(path[key]) &&
!utils.isMongooseType(path[key]) &&
path[key] != null &&
pathtype !== 'virtual' &&
pathtype !== 'real' &&
pathtype !== 'adhocOrUndefined' &&
!(this.$__path(pathName) instanceof MixedSchema) &&
!(this.$__schema.paths[pathName] &&
this.$__schema.paths[pathName].options &&
this.$__schema.paths[pathName].options.ref);

if (someCondition) {
if (utils.isNonBuiltinObject(valForKey) && pathtype === 'nested') {
$applyDefaultsToNested(path[key], prefix + key, this);
this.$set(prefix + key, path[key], constructing, { ...options, _skipMarkModified: true });
this.$set(prefix + key, path[key], constructing, Object.assign({}, options, { _skipMarkModified: true }));
continue;
} else if (strict) {
// Don't overwrite defaults with undefined keys (gh-3981) (gh-9039)
Expand Down
12 changes: 12 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,18 @@ exports.isPOJO = function isPOJO(arg) {
return !proto || proto.constructor.name === 'Object';
};

/*!
* Determines if `arg` is an object that isn't an instance of a built-in value
* class, like Array, Buffer, ObjectId, etc.
*/

exports.isNonBuiltinObject = function isNonBuiltinObject(val) {
return typeof val === 'object' &&
!exports.isNativeObject(val) &&
!exports.isMongooseType(val) &&
val != null;
};

/*!
* Determines if `obj` is a built-in object like an array, date, boolean,
* etc.
Expand Down
4 changes: 2 additions & 2 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1710,7 +1710,7 @@ describe('document', function() {
describe('on nested paths', function() {
describe('using set(path, object)', function() {
it('overwrites the entire object', function() {
let doc = new TestDocument();
const doc = new TestDocument();

doc.init({
test: 'Test',
Expand Down Expand Up @@ -10472,7 +10472,7 @@ describe('document', function() {
const doc1 = new Subdoc({ child: { name: 'Luke', age: 19 } });
doc1.set({ child: { age: 21 } });
assert.deepEqual(doc1.toObject().child, { age: 21 });

const doc2 = new Nested({ child: { name: 'Luke', age: 19 } });
doc2.set({ child: { age: 21 } });
assert.deepEqual(doc2.toObject().child, { age: 21 });
Expand Down

0 comments on commit 3a900de

Please sign in to comment.