@@ -58,40 +58,36 @@ export type Operation =
58
58
import { isValidIndex , replacePathIndices } from './utils' ;
59
59
60
60
const shiftIndices = function ( acceptedOp : Operation , proposedOps : Operation [ ] , isAdd : boolean = false ) : void {
61
- // shift indexes
62
61
const lastSlash = acceptedOp . path . lastIndexOf ( '/' ) ;
63
62
64
- if ( lastSlash > - 1 ) {
65
- const index = acceptedOp . path . substr ( lastSlash + 1 ) ;
66
- const arrayPath = acceptedOp . path . substr ( 0 , lastSlash + 1 ) ;
63
+ if ( lastSlash === - 1 ) return ;
67
64
68
- if ( isValidIndex ( index ) ) {
69
- const propChangesLen = proposedOps . length ;
70
- let currentIndex = 0 ;
65
+ const index = acceptedOp . path . substr ( lastSlash + 1 ) ;
66
+ const arrayPath = acceptedOp . path . substr ( 0 , lastSlash + 1 ) ;
71
67
72
- while ( currentIndex < propChangesLen ) {
73
- const proposedOp = proposedOps [ currentIndex ] ;
74
- currentIndex ++ ;
68
+ if ( ! isValidIndex ( index ) ) return ;
75
69
76
- if ( proposedOp . path . indexOf ( arrayPath ) === 0 ) {
77
- //item from the same array
78
- proposedOp . path = replacePathIndices ( proposedOp . path , arrayPath , index , isAdd ) ;
79
- }
70
+ proposedOps . forEach ( ( proposedOp ) => {
71
+ const pathOfSameArray = proposedOp . path . indexOf ( arrayPath ) === 0 ;
80
72
81
- if ( proposedOp . from && proposedOp . from . indexOf ( arrayPath ) === 0 ) {
82
- //item from the same array
83
- proposedOp . from = replacePathIndices ( proposedOp . from , arrayPath , index , isAdd ) ;
84
- }
85
- }
73
+ if ( pathOfSameArray ) {
74
+ proposedOp . path = replacePathIndices ( proposedOp . path , arrayPath , index , isAdd ) ;
86
75
}
87
- }
76
+
77
+ const hasFromOp = proposedOp . op === OpType . move || proposedOp . op === OpType . copy ? proposedOp : null ;
78
+ const fromOfSameArray = hasFromOp && hasFromOp . from && hasFromOp . from . indexOf ( arrayPath ) === 0 ;
79
+
80
+ if ( hasFromOp && fromOfSameArray ) {
81
+ hasFromOp . from = replacePathIndices ( hasFromOp . from , arrayPath , index , isAdd ) ;
82
+ }
83
+ } ) ;
88
84
} ;
89
85
90
86
const removeOperations = function (
91
87
acceptedOp : Operation ,
92
88
proposedOps : Operation [ ] ,
93
89
options : Options ,
94
- skipCondition ?: Function ,
90
+ skipCondition ?: ( acceptedOp : Operation , proposedOp : Operation ) => boolean ,
95
91
) {
96
92
const { acceptedWinsOnEqualPath} = options ;
97
93
let currentIndex = 0 ;
@@ -104,10 +100,9 @@ const removeOperations = function(
104
100
const matchesPathToPath =
105
101
( acceptedWinsOnEqualPath && acceptedOp . path === proposedOp . path ) ||
106
102
proposedOp . path . indexOf ( acceptedOp . path + '/' ) === 0 ;
103
+ const shouldSkip = ! ! ( skipCondition && skipCondition ( acceptedOp , proposedOp ) ) ;
107
104
108
- if ( skipCondition && skipCondition ( acceptedOp , proposedOp ) ) {
109
-
110
- } else if ( matchesFromToPath || matchesPathToPath ) {
105
+ if ( ! shouldSkip && ( matchesFromToPath || matchesPathToPath ) ) {
111
106
proposedOps . splice ( currentIndex , 1 ) ;
112
107
currentIndex -- ;
113
108
}
@@ -117,29 +112,10 @@ const removeOperations = function(
117
112
} ;
118
113
119
114
const removeTransformer = function ( acceptedOp : Operation , proposedOps : Operation [ ] ) : void {
120
- removeOperations ( acceptedOp , proposedOps , { acceptedWinsOnEqualPath : true } , function ( acceptedOp , proposedOp ) {
115
+ removeOperations ( acceptedOp , proposedOps , { acceptedWinsOnEqualPath : true } , function ( acceptedOp , proposedOp ) {
121
116
return ( proposedOp . op === 'add' || proposedOp . op === 'test' ) && acceptedOp . path === proposedOp . path ;
122
117
} ) ;
123
118
shiftIndices ( acceptedOp , proposedOps ) ;
124
- /*let currentIndex = 0;
125
- let proposedOp;
126
-
127
- while ((proposedOp = proposedOps[currentIndex])) {
128
- const matchesFromToPath =
129
- proposedOp.from && (proposedOp.from === acceptedOp.path || proposedOp.from.indexOf(acceptedOp.path + '/') === 0);
130
- const matchesPathToPath =
131
- acceptedOp.path === proposedOp.path || proposedOp.path.indexOf(acceptedOp.path + '/') === 0;
132
-
133
- if ((proposedOp.op === 'add' || proposedOp.op === 'test') && acceptedOp.path === proposedOp.path) {
134
- // do nothing ? (tomalec)
135
- } else if (matchesFromToPath || matchesPathToPath) {
136
- proposedOps.splice(currentIndex, 1);
137
- currentIndex--;
138
- }
139
- currentIndex++;
140
- }
141
-
142
- shiftIndices(acceptedOp, proposedOps);*/
143
119
} ;
144
120
145
121
const replaceTransformer = function ( acceptedOp : Operation , proposedOps : Operation [ ] , options : Options ) : void {
@@ -155,6 +131,7 @@ const transformAgainst = {
155
131
remove : removeTransformer ,
156
132
replace : replaceTransformer ,
157
133
add : addTransformer ,
134
+ copy : addTransformer ,
158
135
} ;
159
136
160
137
const reduceJSONPatches = function ( proposedOps : Operation [ ] , acceptedOp : Operation , options : Options ) {
0 commit comments