@@ -12,22 +12,60 @@ lineDiff.tokenize = function(value) {
12
12
}
13
13
14
14
// Merge the content and line separators into single tokens
15
- for ( let i = 0 ; i < linesAndNewlines . length ; i ++ ) {
16
- let line = linesAndNewlines [ i ] ;
17
-
18
- if ( i % 2 && ! this . options . newlineIsToken ) {
19
- retLines [ retLines . length - 1 ] += line ;
20
- } else {
21
- if ( this . options . ignoreWhitespace ) {
22
- line = line . trim ( ) ;
15
+ if ( this . options . ignoreWhitespace ) {
16
+ for ( let i = 0 ; i < linesAndNewlines . length ; i ++ ) {
17
+ let line = linesAndNewlines [ i ] ;
18
+
19
+ if ( i % 2 && ! this . options . newlineIsToken ) {
20
+ let last = retLines [ retLines . length - 1 ] ;
21
+ last . key += line ;
22
+ last . payload += line ;
23
+ } else {
24
+ retLines . push ( { key : line . trim ( ) , payload : line } ) ;
25
+ }
26
+ }
27
+ } else {
28
+ for ( let i = 0 ; i < linesAndNewlines . length ; i ++ ) {
29
+ let line = linesAndNewlines [ i ] ;
30
+
31
+ if ( i % 2 && ! this . options . newlineIsToken ) {
32
+ retLines [ retLines . length - 1 ] += line ;
33
+ } else {
34
+ retLines . push ( line ) ;
23
35
}
24
- retLines . push ( line ) ;
25
36
}
26
37
}
27
38
28
39
return retLines ;
29
40
} ;
30
41
42
+ lineDiff . removeEmpty = function ( array ) {
43
+ if ( this . options . ignoreWhitespace ) {
44
+ return array . filter ( v => v . key ) ;
45
+ }
46
+ return array . filter ( v => v ) ;
47
+ } ;
48
+
49
+ lineDiff . equals = function ( left , right ) {
50
+ if ( this . options . ignoreWhitespace ) {
51
+ // Special case handle for when one terminal is ignored (i.e. whitespace).
52
+ // For this case we merge the terminal into the prior string and drop the change.
53
+ // This is only available for string mode.
54
+ if ( left === '' ) {
55
+ return Diff . prototype . equals . call ( this , left , right . trim ( ) ) ;
56
+ }
57
+ return Diff . prototype . equals . call ( this , left . key , right . key ) ;
58
+ }
59
+ return Diff . prototype . equals . call ( this , left , right ) ;
60
+ } ;
61
+
62
+ lineDiff . join = function ( result ) {
63
+ if ( this . options . ignoreWhitespace ) {
64
+ return result . map ( v => v . payload ) . join ( '' ) ;
65
+ }
66
+ return result . join ( '' ) ;
67
+ } ;
68
+
31
69
export function diffLines ( oldStr , newStr , callback ) { return lineDiff . diff ( oldStr , newStr , callback ) ; }
32
70
export function diffTrimmedLines ( oldStr , newStr , callback ) {
33
71
let options = generateOptions ( callback , { ignoreWhitespace : true } ) ;
0 commit comments