11
22dop . core . setPatch = function ( object , patch ) {
3- dop . util . path ( patch , null , object , dop . core . setPatchMutator ) ;
3+ if ( ! isArray ( patch ) )
4+ patch = [ patch ] ;
5+
6+ for ( var index = 0 , total = patch . length ; index < total ; ++ index )
7+ dop . util . path ( patch [ index ] , null , object , dop . core . setPatchMutator ) ;
48} ;
59
610
711dop . core . setPatchMutator = function ( destiny , prop , value , typeofValue , path ) {
8-
912 var typeofDestinyParent = dop . util . typeof ( destiny ) ,
10- typeofDestiny = dop . util . typeof ( destiny [ prop ] ) ;
11-
12- // Array mutations
13- if ( typeofValue == 'object' && typeofDestiny == 'array' && value . hasOwnProperty ( dop . cons . DOP ) ) {
13+ typeofDestiny = dop . util . typeof ( destiny [ prop ] ) ,
14+ typeInstruction ,
15+ instructionsPatchs = dop . protocol . instructionsPatchs ,
16+ mutation ;
1417
15- var mutations = value [ dop . cons . DOP ] ,
16- mutation ,
17- index = 0 ,
18- total = mutations . length ,
19- typeArrayMutation ;
18+ // console.log( prop, typeofValue, value );
19+ if ( typeofValue == 'array' ) {
20+ typeInstruction = value [ 0 ] ;
2021
21- // if (typeofDestiny!='array')
22- // dop.set(destiny, prop, []);
22+ // New object/array
23+ if ( typeInstruction === instructionsPatchs . object )
24+ dop . set ( destiny , prop , dop . util . clone ( value [ 1 ] ) ) ;
2325
24- for ( ; index < total ; ++ index ) {
25- typeArrayMutation = mutations [ index ] [ 0 ] ; // 0=swaps 1=splices
26- mutation = mutations [ index ] . slice ( 1 ) ;
26+ // Array mutations
27+ else {
28+ if ( ! isArray ( typeInstruction ) )
29+ value = [ value ] ;
2730
28- // swap
29- if ( typeArrayMutation === 0 )
30- dop . core . swap ( destiny [ prop ] , mutation ) ;
31+ for ( var index = 0 , total = value . length ; index < total ; ++ index ) {
32+ mutation = value [ index ] ;
3133
32- // length
33- else if ( typeArrayMutation === 2 )
34- dop . set ( destiny [ prop ] , 'length' , mutation [ 0 ] ) ;
34+ // Splice
35+ if ( mutation [ 0 ] === instructionsPatchs . splice )
36+ dop . core . splice ( destiny [ prop ] , mutation [ 1 ] ) ;
3537
36- // splice & set & del
37- else {
38- // We have to update the length of the array in case that is lower than before
39- if ( destiny [ prop ] . length < mutation [ 0 ] )
40- dop . getObjectTarget ( destiny [ prop ] ) . length = mutation [ 0 ] ;
41-
42- // set
43- if ( mutation . length === 3 && mutation [ 1 ] === 1 ) {
44- ( mutation [ 2 ] === undefined ) ?
45- dop . del ( destiny [ prop ] , mutation [ 0 ] )
46- :
47- dop . set ( destiny [ prop ] , mutation [ 0 ] , mutation [ 2 ] ) ;
48- }
49-
50- // splice
51- else
52- dop . core . splice ( destiny [ prop ] , mutation ) ;
38+ else if ( mutation [ 0 ] === instructionsPatchs . swaps )
39+ dop . core . swap ( destiny [ prop ] , mutation [ 1 ] ) ;
5340 }
5441 }
5542
56- // if (typeof value.length == 'number' && value.length>-1)
57- // destiny[prop].length = value.length;
58-
59- return true ; // Skiping to dont go inside of {~dop:...}
43+ return true ; // Skiping to dont go inside of [instructionPatch, ...]
6044 }
6145
62- else //if (path.length > 1) {
46+ // Delete
47+ else if ( typeofValue == 'undefined' )
48+ dop . del ( destiny , prop ) ;
49+
50+ // Set value
51+ else if ( typeofValue != 'object' )
52+ dop . set ( destiny , prop , value ) ;
53+ } ;
54+
55+
56+
57+
58+ // // Array mutations
59+ // if (typeofValue=='object' && typeofDestiny=='array' && value.hasOwnProperty(dop.cons.DOP)) {
6360
64- // Objects
65- if ( typeofValue == 'object' && typeofDestiny != 'object' ) //!destiny.hasOwnProperty(prop)
66- dop . set ( destiny , prop , { } ) ;
61+ // var mutations = value[dop.cons.DOP],
62+ // mutation,
63+ // index=0,
64+ // total=mutations.length,
65+ // typeArrayMutation;
6766
68- // Arrays
69- else if ( typeofValue == 'array' && typeofDestiny != 'array' )
70- dop . set ( destiny , prop , [ ] ) ;
67+ // // if (typeofDestiny!='array')
68+ // // dop.set(destiny, prop, []);
7169
72- // Delete
73- else if ( typeofValue == 'undefined' )
74- dop . del ( destiny , prop ) ;
70+ // for (;index<total; ++index) {
71+ // typeArrayMutation = mutations[index][0]; // 0=swaps 1=splices
72+ // mutation = mutations[index].slice(1 );
7573
76- // Set value
77- else if ( typeofValue != 'object' )
78- dop . set ( destiny , prop , value ) ;
79- //}
80- } ;
74+ // // swap
75+ // if (typeArrayMutation===0)
76+ // dop.core.swap(destiny[prop], mutation);
77+
78+ // // length
79+ // else if (typeArrayMutation===2)
80+ // dop.set(destiny[prop], 'length', mutation[0]);
81+
82+ // // splice & set & del
83+ // else {
84+ // // We have to update the length of the array in case that is lower than before
85+ // if (destiny[prop].length<mutation[0])
86+ // dop.getObjectTarget(destiny[prop]).length = mutation[0];
87+
88+ // // set
89+ // if (mutation.length===3 && mutation[1]===1) {
90+ // (mutation[2] === undefined) ?
91+ // dop.del(destiny[prop], mutation[0])
92+ // :
93+ // dop.set(destiny[prop], mutation[0], mutation[2]);
94+ // }
95+
96+ // // splice
97+ // else
98+ // dop.core.splice(destiny[prop], mutation);
99+ // }
100+ // }
101+
102+ // // if (typeof value.length == 'number' && value.length>-1)
103+ // // destiny[prop].length = value.length;
104+
105+ // return true; // Skiping to dont go inside of {~dop:...}
106+ // }
107+
108+ // else //if (path.length > 1) {
109+
110+ // // Objects
111+ // if (typeofValue=='object' && typeofDestiny!='object') //!destiny.hasOwnProperty(prop)
112+ // dop.set(destiny, prop, {});
113+
114+ // // Arrays
115+ // else if (typeofValue=='array' && typeofDestiny!='array')
116+ // dop.set(destiny, prop, []);
117+
118+ // // Delete
119+ // else if (typeofValue=='undefined')
120+ // dop.del(destiny, prop);
121+
122+ // // Set value
123+ // else if (typeofValue!='object')
124+ // dop.set(destiny, prop, value);
125+ // // }
0 commit comments