1
1
'use strict' ;
2
2
3
3
const {
4
- Array,
5
- ArrayPrototypeFill,
6
4
ArrayPrototypePush,
7
5
ArrayPrototypeSlice,
6
+ Int32Array,
8
7
StringPrototypeEndsWith,
9
8
} = primordials ;
10
9
@@ -26,7 +25,7 @@ function myersDiff(actual, expected, checkCommaDisparity = false) {
26
25
const actualLength = actual . length ;
27
26
const expectedLength = expected . length ;
28
27
const max = actualLength + expectedLength ;
29
- const v = ArrayPrototypeFill ( Array ( 2 * max + 1 ) , 0 ) ;
28
+ const v = new Int32Array ( 2 * max + 1 ) ;
30
29
31
30
const trace = [ ] ;
32
31
@@ -35,22 +34,24 @@ function myersDiff(actual, expected, checkCommaDisparity = false) {
35
34
ArrayPrototypePush ( trace , newTrace ) ;
36
35
37
36
for ( let diagonalIndex = - diffLevel ; diagonalIndex <= diffLevel ; diagonalIndex += 2 ) {
38
- let x ;
39
- if ( diagonalIndex === - diffLevel ||
40
- ( diagonalIndex !== diffLevel && v [ diagonalIndex - 1 + max ] < v [ diagonalIndex + 1 + max ] ) ) {
41
- x = v [ diagonalIndex + 1 + max ] ;
42
- } else {
43
- x = v [ diagonalIndex - 1 + max ] + 1 ;
44
- }
45
-
37
+ const offset = diagonalIndex + max ;
38
+ const previousOffset = v [ offset - 1 ] ;
39
+ const nextOffset = v [ offset + 1 ] ;
40
+ let x = diagonalIndex === - diffLevel || ( diagonalIndex !== diffLevel && previousOffset < nextOffset ) ?
41
+ nextOffset :
42
+ previousOffset + 1 ;
46
43
let y = x - diagonalIndex ;
47
44
48
- while ( x < actualLength && y < expectedLength && areLinesEqual ( actual [ x ] , expected [ y ] , checkCommaDisparity ) ) {
45
+ while (
46
+ x < actualLength &&
47
+ y < expectedLength &&
48
+ areLinesEqual ( actual [ x ] , expected [ y ] , checkCommaDisparity )
49
+ ) {
49
50
x ++ ;
50
51
y ++ ;
51
52
}
52
53
53
- v [ diagonalIndex + max ] = x ;
54
+ v [ offset ] = x ;
54
55
55
56
if ( x >= actualLength && y >= expectedLength ) {
56
57
return backtrack ( trace , actual , expected , checkCommaDisparity ) ;
@@ -71,10 +72,13 @@ function backtrack(trace, actual, expected, checkCommaDisparity) {
71
72
for ( let diffLevel = trace . length - 1 ; diffLevel >= 0 ; diffLevel -- ) {
72
73
const v = trace [ diffLevel ] ;
73
74
const diagonalIndex = x - y ;
74
- let prevDiagonalIndex ;
75
+ const offset = diagonalIndex + max ;
75
76
76
- if ( diagonalIndex === - diffLevel ||
77
- ( diagonalIndex !== diffLevel && v [ diagonalIndex - 1 + max ] < v [ diagonalIndex + 1 + max ] ) ) {
77
+ let prevDiagonalIndex ;
78
+ if (
79
+ diagonalIndex === - diffLevel ||
80
+ ( diagonalIndex !== diffLevel && v [ offset - 1 ] < v [ offset + 1 ] )
81
+ ) {
78
82
prevDiagonalIndex = diagonalIndex + 1 ;
79
83
} else {
80
84
prevDiagonalIndex = diagonalIndex - 1 ;
@@ -84,8 +88,11 @@ function backtrack(trace, actual, expected, checkCommaDisparity) {
84
88
const prevY = prevX - prevDiagonalIndex ;
85
89
86
90
while ( x > prevX && y > prevY ) {
87
- const value = ! checkCommaDisparity ||
88
- StringPrototypeEndsWith ( actual [ x - 1 ] , ',' ) ? actual [ x - 1 ] : expected [ y - 1 ] ;
91
+ const actualItem = actual [ x - 1 ] ;
92
+ const value =
93
+ ! checkCommaDisparity || StringPrototypeEndsWith ( actualItem , ',' ) ?
94
+ actualItem :
95
+ expected [ y - 1 ] ;
89
96
ArrayPrototypePush ( result , { __proto__ : null , type : 'nop' , value } ) ;
90
97
x -- ;
91
98
y -- ;
@@ -110,13 +117,15 @@ function printSimpleMyersDiff(diff) {
110
117
111
118
for ( let diffIdx = diff . length - 1 ; diffIdx >= 0 ; diffIdx -- ) {
112
119
const { type, value } = diff [ diffIdx ] ;
120
+ let color = colors . white ;
121
+
113
122
if ( type === 'insert' ) {
114
- message += ` ${ colors . green } ${ value } ${ colors . white } ` ;
123
+ color = colors . green ;
115
124
} else if ( type === 'delete' ) {
116
- message += `${ colors . red } ${ value } ${ colors . white } ` ;
117
- } else {
118
- message += `${ colors . white } ${ value } ${ colors . white } ` ;
125
+ color = colors . red ;
119
126
}
127
+
128
+ message += `${ color } ${ value } ${ colors . white } ` ;
120
129
}
121
130
122
131
return `\n${ message } ` ;
@@ -129,17 +138,16 @@ function printMyersDiff(diff, operator) {
129
138
130
139
for ( let diffIdx = diff . length - 1 ; diffIdx >= 0 ; diffIdx -- ) {
131
140
const { type, value } = diff [ diffIdx ] ;
132
- const previousType = ( diffIdx < ( diff . length - 1 ) ) ? diff [ diffIdx + 1 ] . type : null ;
133
- const typeChanged = previousType && ( type !== previousType ) ;
141
+ const previousType = diffIdx < diff . length - 1 ? diff [ diffIdx + 1 ] . type : null ;
134
142
135
- if ( typeChanged && previousType === 'nop' ) {
136
- // Avoid grouping if only one line would have been grouped otherwise
143
+ // Avoid grouping if only one line would have been grouped otherwise
144
+ if ( previousType === 'nop' && type !== previousType ) {
137
145
if ( nopCount === kNopLinesToCollapse + 1 ) {
138
146
message += `${ colors . white } ${ diff [ diffIdx + 1 ] . value } \n` ;
139
147
} else if ( nopCount === kNopLinesToCollapse + 2 ) {
140
148
message += `${ colors . white } ${ diff [ diffIdx + 2 ] . value } \n` ;
141
149
message += `${ colors . white } ${ diff [ diffIdx + 1 ] . value } \n` ;
142
- } if ( nopCount >= ( kNopLinesToCollapse + 3 ) ) {
150
+ } else if ( nopCount >= kNopLinesToCollapse + 3 ) {
143
151
message += `${ colors . blue } ...${ colors . white } \n` ;
144
152
message += `${ colors . white } ${ diff [ diffIdx + 1 ] . value } \n` ;
145
153
skipped = true ;
0 commit comments