@@ -1112,9 +1112,11 @@ Document.prototype.$set = function $set(path, val, type, options) {
1112
1112
return this ;
1113
1113
}
1114
1114
1115
+ options = Object . assign ( { } , options , { _skipMinimizeTopLevel : false } ) ;
1116
+
1115
1117
for ( let i = 0 ; i < len ; ++ i ) {
1116
1118
key = keys [ i ] ;
1117
- const pathName = prefix + key ;
1119
+ const pathName = prefix ? prefix + key : key ;
1118
1120
pathtype = this . $__schema . pathType ( pathName ) ;
1119
1121
const valForKey = path [ key ] ;
1120
1122
@@ -1126,20 +1128,15 @@ Document.prototype.$set = function $set(path, val, type, options) {
1126
1128
pathtype === 'nested' &&
1127
1129
this . _doc [ key ] != null ) {
1128
1130
delete this . _doc [ key ] ;
1129
- // Make sure we set `{}` back even if we minimize re: gh-8565
1130
- options = Object . assign ( { } , options , { _skipMinimizeTopLevel : true } ) ;
1131
- } else {
1132
- // Make sure we set `{_skipMinimizeTopLevel: false}` if don't have overwrite: gh-10441
1133
- options = Object . assign ( { } , options , { _skipMinimizeTopLevel : false } ) ;
1134
1131
}
1135
1132
1136
1133
if ( utils . isNonBuiltinObject ( valForKey ) && pathtype === 'nested' ) {
1137
- this . $set ( prefix + key , path [ key ] , constructing , Object . assign ( { } , options , { _skipMarkModified : true } ) ) ;
1138
- $applyDefaultsToNested ( this . $get ( prefix + key ) , prefix + key , this ) ;
1134
+ this . $set ( pathName , valForKey , constructing , Object . assign ( { } , options , { _skipMarkModified : true } ) ) ;
1135
+ $applyDefaultsToNested ( this . $get ( pathName ) , pathName , this ) ;
1139
1136
continue ;
1140
1137
} else if ( strict ) {
1141
1138
// Don't overwrite defaults with undefined keys (gh-3981) (gh-9039)
1142
- if ( constructing && path [ key ] === void 0 &&
1139
+ if ( constructing && valForKey === void 0 &&
1143
1140
this . $get ( pathName ) !== void 0 ) {
1144
1141
continue ;
1145
1142
}
@@ -1149,20 +1146,19 @@ Document.prototype.$set = function $set(path, val, type, options) {
1149
1146
}
1150
1147
1151
1148
if ( pathtype === 'real' || pathtype === 'virtual' ) {
1152
- const p = path [ key ] ;
1153
- this . $set ( prefix + key , p , constructing , options ) ;
1154
- } else if ( pathtype === 'nested' && path [ key ] instanceof Document ) {
1155
- this . $set ( prefix + key ,
1156
- path [ key ] . toObject ( { transform : false } ) , constructing , options ) ;
1149
+ this . $set ( pathName , valForKey , constructing , options ) ;
1150
+ } else if ( pathtype === 'nested' && valForKey instanceof Document ) {
1151
+ this . $set ( pathName ,
1152
+ valForKey . toObject ( { transform : false } ) , constructing , options ) ;
1157
1153
} else if ( strict === 'throw' ) {
1158
1154
if ( pathtype === 'nested' ) {
1159
- throw new ObjectExpectedError ( key , path [ key ] ) ;
1155
+ throw new ObjectExpectedError ( key , valForKey ) ;
1160
1156
} else {
1161
1157
throw new StrictModeError ( key ) ;
1162
1158
}
1163
1159
}
1164
- } else if ( path [ key ] !== void 0 ) {
1165
- this . $set ( prefix + key , path [ key ] , constructing , options ) ;
1160
+ } else if ( valForKey !== void 0 ) {
1161
+ this . $set ( pathName , valForKey , constructing , options ) ;
1166
1162
}
1167
1163
}
1168
1164
@@ -2225,16 +2221,17 @@ Document.prototype[documentModifiedPaths] = Document.prototype.modifiedPaths;
2225
2221
2226
2222
Document . prototype . isModified = function ( paths , modifiedPaths ) {
2227
2223
if ( paths ) {
2228
- if ( ! Array . isArray ( paths ) ) {
2229
- paths = paths . indexOf ( ' ' ) === - 1 ? [ paths ] : paths . split ( ' ' ) ;
2230
- }
2231
-
2232
2224
const directModifiedPathsObj = this . $__ . activePaths . states . modify ;
2233
2225
if ( directModifiedPathsObj == null ) {
2234
2226
return false ;
2235
2227
}
2228
+
2229
+ if ( typeof paths === 'string' ) {
2230
+ paths = [ paths ] ;
2231
+ }
2232
+
2236
2233
for ( const path of paths ) {
2237
- if ( Object . prototype . hasOwnProperty . call ( directModifiedPathsObj , path ) ) {
2234
+ if ( directModifiedPathsObj [ path ] != null ) {
2238
2235
return true ;
2239
2236
}
2240
2237
}
@@ -2668,14 +2665,14 @@ function _getPathsToValidate(doc) {
2668
2665
const modifiedPaths = doc . modifiedPaths ( ) ;
2669
2666
for ( const subdoc of subdocs ) {
2670
2667
if ( subdoc . $basePath ) {
2671
- // Remove child paths for now, because we'll be validating the whole
2672
- // subdoc
2673
2668
const fullPathToSubdoc = subdoc . $__fullPathWithIndexes ( ) ;
2674
2669
2675
- for ( const p of paths ) {
2676
- if ( p == null || p . startsWith ( fullPathToSubdoc + '.' ) ) {
2677
- paths . delete ( p ) ;
2678
- }
2670
+ // Remove child paths for now, because we'll be validating the whole
2671
+ // subdoc.
2672
+ // The following is a faster take on looping through every path in `paths`
2673
+ // and checking if the path starts with `fullPathToSubdoc` re: gh-13191
2674
+ for ( const modifiedPath of subdoc . modifiedPaths ( ) ) {
2675
+ paths . delete ( fullPathToSubdoc + '.' + modifiedPath ) ;
2679
2676
}
2680
2677
2681
2678
if ( doc . $isModified ( fullPathToSubdoc , modifiedPaths ) &&
@@ -3348,12 +3345,13 @@ Document.prototype.$__reset = function reset() {
3348
3345
resetArrays . add ( array ) ;
3349
3346
}
3350
3347
} else {
3351
- if ( subdoc . $parent ( ) === this ) {
3348
+ const parent = subdoc . $parent ( ) ;
3349
+ if ( parent === this ) {
3352
3350
this . $__ . activePaths . clearPath ( subdoc . $basePath ) ;
3353
- } else if ( subdoc . $ parent( ) != null && subdoc . $ parent( ) . $isSubdocument ) {
3351
+ } else if ( parent != null && parent . $isSubdocument ) {
3354
3352
// If map path underneath subdocument, may end up with a case where
3355
3353
// map path is modified but parent still needs to be reset. See gh-10295
3356
- subdoc . $ parent( ) . $__reset ( ) ;
3354
+ parent . $__reset ( ) ;
3357
3355
}
3358
3356
}
3359
3357
}
@@ -4070,6 +4068,7 @@ function applyGetters(self, json, options) {
4070
4068
path = paths [ i ] ;
4071
4069
4072
4070
const parts = path . split ( '.' ) ;
4071
+
4073
4072
const plen = parts . length ;
4074
4073
const last = plen - 1 ;
4075
4074
let branch = json ;
0 commit comments