@@ -117,13 +117,13 @@ AV.setProduction = (production) => {
117117 */
118118AV . _getAVPath = function ( path ) {
119119 if ( ! AV . applicationId ) {
120- throw "You need to call AV.initialize before using AV." ;
120+ throw new Error ( "You need to call AV.initialize before using AV." ) ;
121121 }
122122 if ( ! path ) {
123123 path = "" ;
124124 }
125125 if ( ! _ . isString ( path ) ) {
126- throw "Tried to get a localStorage path that wasn't a String." ;
126+ throw new Error ( "Tried to get a localStorage path that wasn't a String." ) ;
127127 }
128128 if ( path [ 0 ] === "/" ) {
129129 path = path . substring ( 1 ) ;
@@ -216,7 +216,7 @@ AV._getValue = function(object, prop) {
216216AV . _encode = function ( value , seenObjects , disallowObjects ) {
217217 if ( value instanceof AV . Object ) {
218218 if ( disallowObjects ) {
219- throw "AV.Objects not allowed here" ;
219+ throw new Error ( "AV.Objects not allowed here" ) ;
220220 }
221221 if ( ! seenObjects || _ . include ( seenObjects , value ) || ! value . _hasData ) {
222222 return value . _toPointer ( ) ;
@@ -227,7 +227,7 @@ AV._encode = function(value, seenObjects, disallowObjects) {
227227 seenObjects ,
228228 disallowObjects ) ;
229229 }
230- throw "Tried to save an object with a pointer to a new, unsaved object." ;
230+ throw new Error ( "Tried to save an object with a pointer to a new, unsaved object." ) ;
231231 }
232232 if ( value instanceof AV . ACL ) {
233233 return value . toJSON ( ) ;
@@ -254,7 +254,7 @@ AV._encode = function(value, seenObjects, disallowObjects) {
254254 }
255255 if ( value instanceof AV . File ) {
256256 if ( ! value . url ( ) && ! value . id ) {
257- throw "Tried to save an object containing an unsaved file." ;
257+ throw new Error ( "Tried to save an object containing an unsaved file." ) ;
258258 }
259259 return {
260260 __type : "File" ,
@@ -264,29 +264,21 @@ AV._encode = function(value, seenObjects, disallowObjects) {
264264 } ;
265265 }
266266 if ( _ . isObject ( value ) ) {
267- var output = { } ;
268- AV . _objectEach ( value , function ( v , k ) {
269- output [ k ] = AV . _encode ( v , seenObjects , disallowObjects ) ;
270- } ) ;
271- return output ;
267+ return _ . mapObject ( value , ( v , k ) => AV . _encode ( v , seenObjects , disallowObjects ) ) ;
272268 }
273269 return value ;
274270} ;
275271
276272/**
277273 * The inverse function of AV._encode.
278- * TODO: make decode not mutate value.
279274 * @private
280275 */
281- AV . _decode = function ( key , value ) {
276+ AV . _decode = function ( value , key ) {
282277 if ( ! _ . isObject ( value ) ) {
283278 return value ;
284279 }
285280 if ( _ . isArray ( value ) ) {
286- AV . _arrayEach ( value , function ( v , k ) {
287- value [ k ] = AV . _decode ( k , v ) ;
288- } ) ;
289- return value ;
281+ return _ . map ( value , v => AV . _decode ( v ) ) ;
290282 }
291283 if ( value instanceof AV . Object ) {
292284 return value ;
@@ -297,6 +289,12 @@ AV._decode = function(key, value) {
297289 if ( value instanceof AV . Op ) {
298290 return value ;
299291 }
292+ if ( value instanceof AV . GeoPoint ) {
293+ return value ;
294+ }
295+ if ( value instanceof AV . ACL ) {
296+ return value ;
297+ }
300298 if ( value . __op ) {
301299 return AV . Op . _decode ( value ) ;
302300 }
@@ -305,9 +303,10 @@ AV._decode = function(key, value) {
305303 className = value . className ;
306304 var pointer = AV . Object . _create ( className ) ;
307305 if ( Object . keys ( value ) . length > 3 ) {
308- delete value . __type ;
309- delete value . className ;
310- pointer . _finishFetch ( value , true ) ;
306+ const v = _ . clone ( value ) ;
307+ delete v . __type ;
308+ delete v . className ;
309+ pointer . _finishFetch ( v , true ) ;
311310 } else {
312311 pointer . _finishFetch ( { objectId : value . objectId } , false ) ;
313312 }
@@ -316,10 +315,11 @@ AV._decode = function(key, value) {
316315 if ( value . __type === "Object" ) {
317316 // It's an Object included in a query result.
318317 className = value . className ;
319- delete value . __type ;
320- delete value . className ;
318+ const v = _ . clone ( value ) ;
319+ delete v . __type ;
320+ delete v . className ;
321321 var object = AV . Object . _create ( className ) ;
322- object . _finishFetch ( value , true ) ;
322+ object . _finishFetch ( v , true ) ;
323323 return object ;
324324 }
325325 if ( value . __type === "Date" ) {
@@ -331,13 +331,8 @@ AV._decode = function(key, value) {
331331 longitude : value . longitude
332332 } ) ;
333333 }
334- if ( key === "ACL" ) {
335- if ( value instanceof AV . ACL ) {
336- return value ;
337- }
338- return new AV . ACL ( value ) ;
339- }
340334 if ( value . __type === "Relation" ) {
335+ if ( ! key ) throw new Error ( 'key missing decoding a Relation' ) ;
341336 var relation = new AV . Relation ( null , key ) ;
342337 relation . targetClassName = value . className ;
343338 return relation ;
@@ -349,10 +344,12 @@ AV._decode = function(key, value) {
349344 file . id = value . objectId ;
350345 return file ;
351346 }
352- AV . _objectEach ( value , function ( v , k ) {
353- value [ k ] = AV . _decode ( k , v ) ;
347+ return _ . mapObject ( value , function ( v , k ) {
348+ if ( k === "ACL" ) {
349+ return new AV . ACL ( v ) ;
350+ }
351+ return AV . _decode ( v , k ) ;
354352 } ) ;
355- return value ;
356353} ;
357354
358355AV . _encodeObjectOrArray = function ( value ) {
0 commit comments