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