11
22dop . core . setPatch = function ( object , patch ) {
33 dop . util . path ( patch , null , object , dop . core . setPatchMutator ) ;
4- return object ;
54} ;
65
7- // dop.core.setPatchs = function(patchs) {
8- // var collector = dop.collectFirst(), object_id;
9- // for (object_id in patchs)
10- // dop.core.setPatch(patchs[object_id].object, patchs[object_id].patch);
11- // return collector;
12- // };
6+
7+ dop . core . setPatchMutator = function ( destiny , prop , value , typeofValue , path ) {
8+
9+ 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 ) ) {
14+
15+ var mutations = value [ dop . cons . DOP ] ,
16+ mutation ,
17+ index = 0 ,
18+ total = mutations . length ,
19+ typeArrayMutation ;
20+
21+ // if (typeofDestiny!='array')
22+ // dop.set(destiny, prop, []);
23+
24+ for ( ; index < total ; ++ index ) {
25+ typeArrayMutation = mutations [ index ] [ 0 ] ; // 0=swaps 1=splices
26+ mutation = mutations [ index ] . slice ( 1 ) ;
27+
28+ // swap
29+ if ( typeArrayMutation === 0 )
30+ dop . core . swap ( destiny [ prop ] , mutation ) ;
31+
32+ // length
33+ else if ( typeArrayMutation === 2 )
34+ dop . set ( destiny [ prop ] , 'length' , mutation [ 0 ] ) ;
35+
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 ) ;
53+ }
54+ }
55+
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:...}
60+ }
61+
62+ else //if (path.length > 1) {
63+
64+ // Objects
65+ if ( typeofValue == 'object' && typeofDestiny != 'object' ) //!destiny.hasOwnProperty(prop)
66+ dop . set ( destiny , prop , { } ) ;
67+
68+ // Arrays
69+ else if ( typeofValue == 'array' && typeofDestiny != 'array' )
70+ dop . set ( destiny , prop , [ ] ) ;
71+
72+ // Delete
73+ else if ( typeofValue == 'undefined' )
74+ dop . del ( destiny , prop ) ;
75+
76+ // Set value
77+ else if ( typeofValue != 'object' )
78+ dop . set ( destiny , prop , value ) ;
79+ //}
80+ } ;
0 commit comments