This repository has been archived by the owner on Jul 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbatch-find-and-replace.jsx
1327 lines (1210 loc) · 46.2 KB
/
batch-find-and-replace.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! batch-find-and-replace.jsx - v0.1.4 - 2014-07-15 */
// Source: src/bfnr_head.jsx
/*************************************************
*
* This script batch processes InDesign FNR
*
****************************************************
* Copyright (c) 2013 - 2014
* Fabian "fabiantheblind" Morón Zirfas
* Permission is hereby granted, free of charge, to any
* person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, subject to
* the following conditions:
* The above copyright notice and this permission notice
* shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTIO
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* see also http://www.opensource.org/licenses/mit-license.php
*
* Btw: It's awesome that you are reading the source code
* feel free to ask me anything about it
*
****************************************************
* there are some js libraries used
* ----------------------------------------------
* a toml (Tom's Obvious, Minimal Language) parser https://github.com/mojombo/toml
* by JonAbrams https://github.com/JonAbrams/tomljs
* ----------------------------------------------
* ----------------------------------------------
* JsonDiffPatch by benjamine to get the toml into
* our structure
* Diff & Patch for JavaScript objects and arrays
* (ie. any JSON serializable structure)
* https://github.com/benjamine/JsonDiffPatch
**************************************************/
// Source: src/submodules/tomljs/toml.js
// Generated by CoffeeScript 1.5.0
// minimal edit to make it usable by extendscript
var getVal;
var TOML = {};
TOML.parse = function(text) {
var addKey, equalsIndex, isArray, key, keygroup, line, lineNum, obj, processArray, strTokens, val, value, _i, _len, _ref;
obj = {};
text = text.replace(/^\s\s*/gm, "").replace(/\s\s*$/gm, "");
strTokens = [];
text = text.replace(/"(?:\\[^\n]|[^"\n])*"/g, function(str) {
str = str.slice(0, str.length - 1).slice(1);
str = str.replace(/\\n/g, "\n").replace(/\\t/g, "\t").replace(/\\0/g, "\0").replace(/\\r/g, "\r").replace(/\\\\/g, "\\").replace(/\\"/g, "\"");
strTokens.push(str);
return "string" + (strTokens.length - 1);
});
text = text.replace(/\s*#.*$/gm, "");
keygroup = obj;
_ref = text.split("\n");
for (lineNum = _i = 0, _len = _ref.length; _i < _len; lineNum = ++_i) {
line = _ref[lineNum];
if (line === "") {
continue;
}
if (line.match(/^\[[_a-zA-Z](\w|\.)*]$/)) {
keygroup = obj;
addKey = function(keys, parent) {
var key;
if (keys.length === 0) {
keygroup = parent;
return;
}
if (!parent) {
parent = obj;
}
key = keys.shift();
if (!parent[key]) {
parent[key] = {};
}
return addKey(keys, parent[key]);
};
addKey(line.replace(/^\[/, "").replace(/]$/, "").split("."), keygroup);
} else if (!line.match(/^[_a-zA-z]\w*\s*=\s*[string\d|\[\d]|true|false/)) {
throw "Invalid statement on line " + lineNum;
} else {
equalsIndex = line.indexOf("=");
key = line.slice(0, equalsIndex).replace(/\s*$/, "");
value = line.slice(equalsIndex + 1).replace(/^\s*/, "");
if (value.match(isArray = /^\[.*]$/)) {
processArray = function(arrayStr, parent) {
var newArray, topArray, val, valueMatch, _ref1, _ref2;
if (parent == null) {
parent = null;
}
while (!arrayStr.match(/^\s*$/)) {
arrayStr = arrayStr.replace(/^\s*/, "").replace(/\s*$/, "");
if (arrayStr[0] === "[") {
if (parent) {
newArray = [];
arrayStr = arrayStr.slice(1);
_ref1 = processArray(arrayStr, newArray), arrayStr = _ref1[0], newArray = _ref1[1];
parent.push(newArray);
} else {
_ref2 = processArray(arrayStr.slice(1), []), arrayStr = _ref2[0], topArray = _ref2[1];
return topArray;
}
} else if (arrayStr[0] === "]") {
if (!parent) {
throw "Could not parse array on line " + lineNum + ". Check for extra ] characters.";
}
return [arrayStr.replace(/]\s*,?\s*/, ""), parent];
} else {
valueMatch = arrayStr.match(/[^,\]]+/);
if (!valueMatch) {
throw "Could not parse array on line " + lineNum + ". Check the number of opening and closing brackets.";
}
val = getVal(valueMatch[0].replace(/\s*$/, ""), strTokens);
if (val === null) {
// debugger;
throw "Could not parse array on line " + lineNum + ". Check for extra commas.";
}
parent.push(val);
arrayStr = arrayStr.replace(/[^,\]]+/, "");
if (arrayStr[0] === ",") {
arrayStr = arrayStr.slice(1);
}
}
}
throw "Could not parse array on line " + lineNum + ". Check for missing closing brackets.";
};
val = processArray(value);
} else {
val = getVal(value, strTokens);
if (val == null) {
throw "Invalid value with assignment on line " + lineNum;
}
}
keygroup[key] = val;
}
}
return obj;
};
getVal = function(valStr, strTokens) {
var date;
if (valStr.match(/^true|false$/)) {
return /true/.test(valStr);
} else if (valStr.match(/^-?\d*\.?\d+$/)) {
return parseFloat(valStr.match(/^\d*\.?\d+$/)[0]);
} else if (valStr.match(/string\d+/)) {
return strTokens[parseInt(valStr.match(/\d+/)[0], 10)];
} else if ((date = new Date(valStr)).toString() !== "Invalid Date") {
return date;
}
return null;
};
// Source: src/submodules/JsonDiffPatch/src/jsondiffpatch.js
/*
* Json Diff Patch
* ---------------
* https://github.com/benjamine/JsonDiffPatch
* by Benjamin Eidelman - [email protected]
* minimal edits to make it available to extendscript
*/
var jdp = {};
if (typeof jsondiffpatch != 'undefined'){
jdp = jsondiffpatch;
}
var jsondiffpatch = jdp;
jdp.version = '0.0.7';
jdp.config = {
textDiffMinLength: 60,
detectArrayMove: true,
includeValueOnArrayMove: false
};
var sequenceDiffer = {
diff: function(array1, array2, objectHash, objectInnerDiff) {
var commonHead = 0, commonTail = 0, index, index1;
var len1 = array1.length;
var len2 = array2.length;
var diff;
var hashCache1 = [];
var hashCache2 = [];
var areTheSame = typeof objectHash == 'function' ?
function(value1, value2, index1, index2) {
if (value1 === value2)
return true;
if (typeof value1 != 'object' || typeof value2 != 'object')
return false;
var hash1, hash2;
if (typeof index1 == 'number') {
hash1 = hashCache1[index1];
if (typeof hash1 == 'undefined') {
hashCache1[index1] = hash1 = objectHash(value1);
}
} else {
hash1 = objectHash(value1);
}
if (typeof index2 == 'number') {
hash2 = hashCache2[index2];
if (typeof hash2 == 'undefined') {
hashCache2[index2] = hash2 = objectHash(value2);
}
} else {
hash2 = objectHash(value2);
}
return hash1 === hash2;
} :
function(value1, value2) {
return value1 === value2;
};
var areTheSameByIndex = function(index1, index2) {
return areTheSame(array1[index1], array2[index2], index1, index2);
};
var tryObjectInnerDiff = function(index1, index2) {
if (!objectInnerDiff) {
return;
}
if (typeof array1[index1] != 'object' || typeof array2[index2] != 'object') {
return;
}
var result = objectInnerDiff(array1[index1], array2[index2]);
if (typeof result == 'undefined') {
return;
}
if (!diff) {
diff = { _t: 'a' };
}
diff[index2] = result;
};
// separate common head
while (commonHead < len1 && commonHead < len2 &&
areTheSameByIndex(commonHead, commonHead)) {
tryObjectInnerDiff(commonHead, commonHead);
commonHead++;
}
// separate common tail
while (commonTail + commonHead < len1 && commonTail + commonHead < len2 &&
areTheSameByIndex(len1 - 1 - commonTail, len2 - 1 - commonTail)) {
tryObjectInnerDiff(len1 - 1 - commonTail, len2 - 1 - commonTail);
commonTail++;
}
if (commonHead + commonTail === len1) {
if (len1 === len2) {
// arrays are identical
return diff;
}
// trivial case, a block (1 or more) was added at array2
diff = diff || { _t: 'a' };
for (index = commonHead; index < len2 - commonTail; index++) {
diff[index] = [array2[index]];
}
return diff;
} else if (commonHead + commonTail === len2) {
// trivial case, a block (1 or more) was removed from array1
diff = diff || { _t: 'a' };
for (index = commonHead; index < len1 - commonTail; index++) {
diff['_'+index] = [array1[index], 0, 0];
}
return diff;
}
// diff is not trivial, find the LCS (Longest Common Subsequence)
var lcs = this.lcs(
array1.slice(commonHead, len1 - commonTail),
array2.slice(commonHead, len2 - commonTail),
{
areTheSameByIndex: function(index1, index2) {
return areTheSameByIndex(index1 + commonHead, index2 + commonHead);
}
});
diff = diff || { _t: 'a' };
var removedItems = [];
for (index = commonHead; index < len1 - commonTail; index++) {
if (lcs.indices1.indexOf(index - commonHead) < 0) {
// removed
diff['_'+index] = [array1[index], 0, 0];
removedItems.push(index);
}
}
var removedItemsLength = removedItems.length;
for (index = commonHead; index < len2 - commonTail; index++) {
var indexOnArray2 = lcs.indices2.indexOf(index - commonHead);
if (indexOnArray2 < 0) {
// added, try to match with a removed item and register as position move
var isMove = false;
if (jdp.config.detectArrayMove) {
if (removedItemsLength > 0) {
for (index1 = 0; index1 < removedItemsLength; index1++) {
if (areTheSameByIndex(removedItems[index1], index)) {
// store position move as: [originalValue, newPosition, 3]
diff['_' + removedItems[index1]].splice(1, 2, index, 3);
if (!jdp.config.includeValueOnArrayMove) {
// don't include moved value on diff, to save bytes
diff['_' + removedItems[index1]][0] = '';
}
tryObjectInnerDiff(removedItems[index1], index);
removedItems.splice(index1, 1);
isMove = true;
break;
}
}
}
}
if (!isMove) {
// added
diff[index] = [array2[index]];
}
} else {
// match, do inner diff
tryObjectInnerDiff(lcs.indices1[indexOnArray2] + commonHead, lcs.indices2[indexOnArray2] + commonHead);
}
}
return diff;
},
getArrayIndexBefore: function(d, indexAfter) {
var index, indexBefore = indexAfter;
for (var prop in d) {
if (d.hasOwnProperty(prop)) {
if (isArray(d[prop])) {
if (prop.slice(0, 1) === '_') {
index = parseInt(prop.slice(1), 10);
} else {
index = parseInt(prop, 10);
}
if (d[prop].length === 1) {
if (index < indexAfter) {
// this item was inserted before
indexBefore--;
} else {
if (index === indexAfter) {
// the item is new
return -1;
}
}
} else if (d[prop].length === 3) {
if (d[prop][2] === 0) {
if (index <= indexAfter) {
// this item was removed before
indexBefore++;
}
} else {
if (d[prop][2] === 3) {
if (index <= indexAfter) {
// this item was moved from a position before
indexBefore++;
}
if (d[prop][1] > indexAfter) {
// this item was moved to a position before
indexBefore--;
} else {
if (d[prop][1] === indexAfter) {
// the items was moved from other position
return index;
}
}
}
}
}
}
}
}
return indexBefore;
},
patch: function(array, d, objectInnerPatch, path) {
var index, index1;
var numerically = function(a, b) {
return a - b;
};
var numericallyBy = function(name) {
return function(a, b) {
return a[name] - b[name];
};
};
// first, separate removals, insertions and modifications
var toRemove = [];
var toInsert = [];
var toModify = [];
for (index in d) {
if (index !== '_t') {
if (index[0] == '_') {
// removed item from original array
if (d[index][2] === 0 || d[index][2] === 3) {
toRemove.push(parseInt(index.slice(1), 10));
} else {
throw new Error('only removal or move can be applied at original array indices, invalid diff type: ' + d[index][2]);
}
} else {
if (d[index].length === 1) {
// added item at new array
toInsert.push({
index: parseInt(index, 10),
value: d[index][0]
});
} else {
// modified item at new array
toModify.push({
index: parseInt(index, 10),
diff: d[index]
});
}
}
}
}
// remove items, in reverse order to avoid sawing our own floor
toRemove = toRemove.sort(numerically);
for (index = toRemove.length - 1; index >= 0; index--) {
index1 = toRemove[index];
var indexDiff = d['_' + index1];
var removedValue = array.splice(index1, 1)[0];
if (indexDiff[2] === 3) {
// reinsert later
toInsert.push({
index: indexDiff[1],
value: removedValue
});
}
}
// insert items, in reverse order to avoid moving our own floor
toInsert = toInsert.sort(numericallyBy('index'));
var toInsertLength = toInsert.length;
for (index = 0; index < toInsertLength; index++) {
var insertion = toInsert[index];
array.splice(insertion.index, 0, insertion.value);
}
// apply modifications
var toModifyLength = toModify.length;
if (toModifyLength > 0) {
if (typeof objectInnerPatch != 'function') {
throw new Error('to patch items in the array an objectInnerPatch function must be provided');
}
for (index = 0; index < toModifyLength; index++) {
var modification = toModify[index];
objectInnerPatch(array, modification.index.toString(), modification.diff, path);
}
}
return array;
},
lcs: function(array1, array2, options) {
// http://en.wikipedia.org/wiki/Longest_common_subsequence_problem
options.areTheSameByIndex = options.areTheSameByIndex || function(index1, index2) {
return array1[index1] === array2[index2];
};
var matrix = this.lengthMatrix(array1, array2, options);
var result = this.backtrack(matrix, array1, array2, array1.length, array2.length);
if (typeof array1 == 'string' && typeof array2 == 'string') {
result.sequence = result.sequence.join('');
}
return result;
},
lengthMatrix: function(array1, array2, options) {
var len1 = array1.length;
var len2 = array2.length;
var x, y;
// initialize empty matrix of len1+1 x len2+1
var matrix = [len1 + 1];
for (x = 0; x < len1 + 1; x++) {
matrix[x] = [len2 + 1];
for (y = 0; y < len2 + 1; y++) {
matrix[x][y] = 0;
}
}
matrix.options = options;
// save sequence lengths for each coordinate
for (x = 1; x < len1 + 1; x++) {
for (y = 1; y < len2 + 1; y++) {
if (options.areTheSameByIndex(x - 1, y - 1)) {
matrix[x][y] = matrix[x - 1][y - 1] + 1;
} else {
matrix[x][y] = Math.max(matrix[x - 1][y], matrix[x][y - 1]);
}
}
}
return matrix;
},
backtrack: function(lenghtMatrix, array1, array2, index1, index2) {
if (index1 === 0 || index2 === 0) {
return {
sequence: [],
indices1: [],
indices2: []
};
}
if (lenghtMatrix.options.areTheSameByIndex(index1 - 1, index2 - 1)) {
var subsequence = this.backtrack(lenghtMatrix, array1, array2, index1 - 1, index2 - 1);
subsequence.sequence.push(array1[index1 - 1]);
subsequence.indices1.push(index1 - 1);
subsequence.indices2.push(index2 - 1);
return subsequence;
}
if (lenghtMatrix[index1][index2 - 1] > lenghtMatrix[index1 - 1][index2]) {
return this.backtrack(lenghtMatrix, array1, array2, index1, index2 - 1);
} else {
return this.backtrack(lenghtMatrix, array1, array2, index1 - 1, index2);
}
}
};
jdp.sequenceDiffer = sequenceDiffer;
jdp.dateReviver = function(key, value){
var a;
if (typeof value === 'string') {
a = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)(Z|([+\-])(\d{2}):(\d{2}))$/.exec(value);
if (a) {
return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6]));
}
}
return value;
};
var diff_match_patch_autoconfig = function(){
var dmp;
if (jdp.config.diff_match_patch) {
dmp = new jdp.config.diff_match_patch.diff_match_patch();
}
if (typeof diff_match_patch != 'undefined') {
if (typeof diff_match_patch == 'function') {
dmp = new diff_match_patch();
}
else if (typeof diff_match_patch == 'object' && typeof diff_match_patch.diff_match_patch == 'function') {
dmp = new diff_match_patch.diff_match_patch();
}
}
if (dmp) {
jdp.config.textDiff = function(txt1, txt2){
return dmp.patch_toText(dmp.patch_make(txt1, txt2));
};
jdp.config.textPatch = function(txt1, patch){
var results = dmp.patch_apply(dmp.patch_fromText(patch), txt1);
for (var i = 0; i < results[1].length; i++) {
if (!results[1][i]) {
throw new Error('text patch failed');
}
}
return results[0];
};
return true;
}
};
var isArray = jdp.isArray = (typeof Array.isArray == 'function') ?
// use native function
Array.isArray :
// use instanceof operator
function(a) {
return typeof a == 'object' && a instanceof Array;
};
var isDate = jdp.isDate = function(d){
return d instanceof Date || Object.prototype.toString.call(d) === '[object Date]';
};
var arrayDiff = function(o, n){
return sequenceDiffer.diff(o, n, jdp.config.objectHash, jdp.diff);
};
var objectDiff = function(o, n){
var odiff, pdiff, prop, addPropDiff;
addPropDiff = function(name){
pdiff = diff(o[name], n[name]);
if (typeof pdiff != 'undefined') {
if (typeof odiff == 'undefined') {
odiff = {};
}
odiff[name] = pdiff;
}
};
for (prop in n) {
if (n.hasOwnProperty(prop)) {
addPropDiff(prop);
}
}
for (prop in o) {
if (o.hasOwnProperty(prop)) {
if (typeof n[prop] == 'undefined') {
addPropDiff(prop);
}
}
}
return odiff;
};
var diff = jdp.diff = function(o, n){
var ntype, otype, nnull, onull, d;
if (o === n) {
return;
}
if ((o !== o) && (n !== n)) {
return; // o and n are both NaN
}
ntype = typeof n;
otype = typeof o;
nnull = n === null;
onull = o === null;
// handle Date objects
if (otype == 'object' && isDate(o)){
otype = 'date';
}
if (ntype == 'object' && isDate(n)){
ntype = 'date';
if (otype == 'date'){
// check if equal dates
if (o.getTime() === n.getTime()){
return;
}
}
}
if (nnull || onull || ntype == 'undefined' || ntype != otype ||
ntype == 'number' ||
otype == 'number' ||
ntype == 'boolean' ||
otype == 'boolean' ||
ntype == 'string' ||
otype == 'string' ||
ntype == 'date' ||
otype == 'date' ||
((ntype === 'object') && (isArray(n) != isArray(o)))) {
// value changed
d = [];
if (typeof o != 'undefined') {
if (typeof n != 'undefined') {
var longText = (ntype == 'string' && otype == 'string' && Math.min(o.length, n.length) > jdp.config.textDiffMinLength);
if (longText && !jdp.config.textDiff) {
diff_match_patch_autoconfig();
}
if (longText && jdp.config.textDiff) {
// get changes form old value to new value as a text diff
d.push(jdp.config.textDiff(o, n), 0, 2);
}
else {
// old value changed to new value
d.push(o);
d.push(n);
}
}
else {
// old value has been removed
d.push(o);
d.push(0, 0);
}
}
else {
// new value is added
d.push(n);
}
return d;
}
else {
if (isArray(n)) {
// diff 2 arrays
return arrayDiff(o, n);
}
else {
// diff 2 objects
return objectDiff(o, n);
}
}
};
var objectGet = function(obj, key){
if (isArray(obj)) {
return obj[parseInt(key, 10)];
}
return obj[key];
};
jdp.getByKey = objectGet;
var objectSet = function(obj, key, value){
if (isArray(obj) && obj._key) {
var getKey = obj._key;
if (typeof obj._key != 'function') {
getKey = function(item){
return item[obj._key];
};
}
for (var i = 0; i < obj.length; i++) {
if (getKey(obj[i]) === key) {
if (typeof value == 'undefined') {
obj.splice(i, 1);
i--;
}
else {
obj[i] = value;
}
return;
}
}
if (typeof value != 'undefined') {
obj.push(value);
}
return;
}
if (typeof value == 'undefined') {
if (isArray(obj)) {
obj.splice(key, 1);
} else {
delete obj[key];
}
}
else {
obj[key] = value;
}
};
var textDiffReverse = function(td){
if (!jdp.config.textDiffReverse){
jdp.config.textDiffReverse = function(d){
var i, l, lines, line, lineTmp, header = null, headerRegex = /^@@ +\-(\d+),(\d+) +\+(\d+),(\d+) +@@$/, lineHeader, lineAdd, lineRemove;
var diffSwap = function() {
// swap
if (lineAdd !== null) {
lines[lineAdd] = '-' + lines[lineAdd].slice(1);
}
if (lineRemove !== null) {
lines[lineRemove] = '+' + lines[lineRemove].slice(1);
if (lineAdd !== null) {
lineTmp = lines[lineAdd];
lines[lineAdd] = lines[lineRemove];
lines[lineRemove] = lineTmp;
}
}
// fix header
lines[lineHeader] = '@@ -' + header[3] + ',' + header[4] + ' +' + header[1] + ',' + header[2] + ' @@';
header = null;
lineHeader = null;
lineAdd = null;
lineRemove = null;
};
lines = d.split('\n');
for (i = 0, l = lines.length; i<l; i++) {
line = lines[i];
var lineStart = line.slice(0,1);
if (lineStart==='@'){
if (header !== null) {
//diffSwap();
}
header = headerRegex.exec(line);
lineHeader = i;
lineAdd = null;
lineRemove = null;
// fix header
lines[lineHeader] = '@@ -' + header[3] + ',' + header[4] + ' +' + header[1] + ',' + header[2] + ' @@';
} else if (lineStart == '+'){
lineAdd = i;
lines[i] = '-' + lines[i].slice(1);
} else if (lineStart == '-'){
lineRemove = i;
lines[i] = '+' + lines[i].slice(1);
}
}
if (header !== null) {
//diffSwap();
}
return lines.join('\n');
};
}
return jdp.config.textDiffReverse(td);
};
var reverse = jdp.reverse = function(d){
var prop, rd;
if (typeof d == 'undefined')
{
return;
}
else if (d === null)
{
return null;
}
else if (typeof d == 'object' && !isDate(d))
{
if (isArray(d))
{
if (d.length < 3)
{
if (d.length === 1) {
// add => delete
return [d[0], 0, 0];
} else {
// modify => reverse modify
return [d[1], d[0]];
}
}
else
{
if (d[2] === 0)
{
// undefined, delete value => add value
return [d[0]];
}
else
{
if (d[2] === 2) {
return [textDiffReverse(d[0]), 0, 2];
}
else
{
throw new Error("invalid diff type");
}
}
}
}
else
{
rd = {};
if (d._t === 'a') {
for (prop in d) {
if (d.hasOwnProperty(prop) && prop !== '_t') {
var index, reverseProp = prop;
if (prop.slice(0, 1) === '_') {
index = parseInt(prop.slice(1), 10);
} else {
index = parseInt(prop, 10);
}
if (isArray(d[prop])) {
if (d[prop].length === 1) {
// add => delete
reverseProp = '_' + index;
} else {
if (d[prop].length === 2) {
// modify => reverse modify
reverseProp = sequenceDiffer.getArrayIndexBefore(d, index);
} else {
if (d[prop][2] === 0) {
// delete => add
reverseProp = index.toString();
} else {
if (d[prop][2] === 3) {
// move => reverse move
reverseProp = '_' + d[prop][1];
rd[reverseProp] = [d[prop][0], index, 3];
continue;
} else {
// other modify (eg. textDiff) => reverse modify
reverseProp = sequenceDiffer.getArrayIndexBefore(d, index);
}
}
}
}
} else {
// inner diff => reverse inner diff
reverseProp = sequenceDiffer.getArrayIndexBefore(d, index);
}
rd[reverseProp] = reverse(d[prop]);
}
}
rd._t = 'a';
} else {
for (prop in d) {
if (d.hasOwnProperty(prop)) {
rd[prop] = reverse(d[prop]);
}
}
}
return rd;
}
} else if (typeof d === 'string' && d.slice(0,2) === '@@'){
return textDiffReverse(d);
}
return d;
};
var patch = jdp.patch = function(o, pname, d, path) {
var p, nvalue, subpath = '', target;
if (typeof pname != 'string') {
path = d;
d = pname;
pname = null;
}
else {
if (typeof o != 'object') {
pname = null;
}
}
if (path) {
subpath += path;
}
subpath += '/';
if (pname !== null) {
subpath += pname;
}
if (typeof d == 'object') {
if (isArray(d)) {
// changed value
if (d.length < 3) {
nvalue = d[d.length - 1];
if (pname !== null) {
objectSet(o, pname, nvalue);
}
return nvalue;
}
else {
if (d[2] === 0) {
// undefined, delete value
if (pname !== null) {
objectSet(o, pname);
}
else {
return;
}
}
else
{
if (d[2] === 2) {
// text diff
if (!jdp.config.textPatch) {
diff_match_patch_autoconfig();
}
if (!jdp.config.textPatch) {
throw new Error("textPatch function not found");
}
try {
nvalue = jdp.config.textPatch(objectGet(o, pname), d[0]);
}
catch (text_patch_err) {
throw new Error('cannot apply patch at "' + subpath + '": ' + text_patch_err);
}
if (pname !== null) {
objectSet(o, pname, nvalue);
}
return nvalue;
}
else
{
if (d[2] === 3) {
// position move
// TODO: remove from current position, to insert later at new position
throw new Error("Not implemented diff type: " + d[2]);
} else {
throw new Error("invalid diff type: " + d[2]);
}
}
}
}
}
else {
if (d._t == 'a') {
// array diff
target = pname === null ? o : objectGet(o, pname);
if (typeof target != 'object' || !isArray(target)) {
throw new Error('cannot apply patch at "' + subpath + '": array expected');
}