@@ -332,13 +332,9 @@ function diff3Merge(a, o, b, options) {
332332 } ;
333333 options = Object . assign ( defaults , options ) ;
334334
335- const aString = ( typeof a === 'string' ) ;
336- const oString = ( typeof o === 'string' ) ;
337- const bString = ( typeof b === 'string' ) ;
338-
339- if ( aString ) a = a . split ( options . stringSeparator ) ;
340- if ( oString ) o = o . split ( options . stringSeparator ) ;
341- if ( bString ) b = b . split ( options . stringSeparator ) ;
335+ if ( typeof a === 'string' ) a = a . split ( options . stringSeparator ) ;
336+ if ( typeof o === 'string' ) o = o . split ( options . stringSeparator ) ;
337+ if ( typeof b === 'string' ) b = b . split ( options . stringSeparator ) ;
342338
343339 let results = [ ] ;
344340 const regions = diff3MergeRegions ( a , o , b ) ;
@@ -385,104 +381,128 @@ function diff3Merge(a, o, b, options) {
385381 return results ;
386382}
387383
384+
388385function mergeDiff3 ( a , o , b , options ) {
389- let defaults = {
386+ const defaults = {
390387 excludeFalseConflicts : true ,
391388 stringSeparator : / \s + / ,
392389 label : { }
393390 } ;
394391 options = Object . assign ( defaults , options ) ;
395392
396- const mergeResult = diff3Merge ( a , o , b , options ) ;
393+ const aSection = '<<<<<<<' + ( options . label . a ? ` ${ options . label . a } ` : '' ) ;
394+ const oSection = '|||||||' + ( options . label . o ? ` ${ options . label . o } ` : '' ) ;
395+ const xSection = '=======' ;
396+ const bSection = '>>>>>>>' + ( options . label . b ? ` ${ options . label . b } ` : '' ) ;
397397
398+ const regions = diff3Merge ( a , o , b , options ) ;
398399 let conflict = false ;
399- let lines = [ ] ;
400+ let result = [ ] ;
400401
401- mergeResult . forEach ( result => {
402- if ( result . ok ) {
403- lines = lines . concat ( result . ok ) ;
404- } else if ( result . conflict ) {
402+ regions . forEach ( region => {
403+ if ( region . ok ) {
404+ result = result . concat ( region . ok ) ;
405+ } else if ( region . conflict ) {
405406 conflict = true ;
406- lines . push ( `<<<<<<<${ options . label . a ? ` ${ options . label . a } ` : '' } ` ) ;
407- lines = lines . concat ( result . conflict . a ) ;
408- lines . push ( `|||||||${ options . label . o ? ` ${ options . label . o } ` : '' } ` ) ;
409- lines = lines . concat ( result . conflict . o ) ;
410- lines . push ( '=======' ) ;
411- lines = lines . concat ( result . conflict . b ) ;
412- lines . push ( `>>>>>>>${ options . label . b ? ` ${ options . label . b } ` : '' } ` ) ;
407+ result = result . concat (
408+ [ aSection ] ,
409+ region . conflict . a ,
410+ [ oSection ] ,
411+ region . conflict . o ,
412+ [ xSection ] ,
413+ region . conflict . b ,
414+ [ bSection ]
415+ ) ;
413416 }
414417 } ) ;
415418
416419 return {
417420 conflict : conflict ,
418- result : lines
421+ result : result
419422 } ;
420423}
421424
425+
422426function merge ( a , o , b , options ) {
423- let defaults = {
427+ const defaults = {
424428 excludeFalseConflicts : true ,
425- stringSeparator : / \s + /
429+ stringSeparator : / \s + / ,
430+ label : { }
426431 } ;
427432 options = Object . assign ( defaults , options ) ;
428433
429- const merger = diff3Merge ( a , o , b , options ) ;
434+ const aSection = '<<<<<<<' + ( options . label . a ? ` ${ options . label . a } ` : '' ) ;
435+ const xSection = '=======' ;
436+ const bSection = '>>>>>>>' + ( options . label . b ? ` ${ options . label . b } ` : '' ) ;
437+
438+ const regions = diff3Merge ( a , o , b , options ) ;
430439 let conflict = false ;
431- let lines = [ ] ;
432- for ( let i = 0 ; i < merger . length ; i ++ ) {
433- const item = merger [ i ] ;
434- if ( item . ok ) {
435- lines = lines . concat ( item . ok ) ;
436- } else {
440+ let result = [ ] ;
441+
442+ regions . forEach ( region => {
443+ if ( region . ok ) {
444+ result = result . concat ( region . ok ) ;
445+ } else if ( region . conflict ) {
437446 conflict = true ;
438- lines = lines . concat (
439- [ '\n<<<<<<<<<\n' ] , item . conflict . a ,
440- [ '\n=========\n' ] , item . conflict . b ,
441- [ '\n>>>>>>>>>\n' ]
447+ result = result . concat (
448+ [ aSection ] ,
449+ region . conflict . a ,
450+ [ xSection ] ,
451+ region . conflict . b ,
452+ [ bSection ]
442453 ) ;
443454 }
444- }
455+ } ) ;
456+
445457 return {
446458 conflict : conflict ,
447- result : lines
459+ result : result
448460 } ;
449461}
450462
451463
452464function mergeDigIn ( a , o , b , options ) {
453- let defaults = {
454- excludeFalseConflicts : false ,
455- stringSeparator : / \s + /
465+ const defaults = {
466+ excludeFalseConflicts : true ,
467+ stringSeparator : / \s + / ,
468+ label : { }
456469 } ;
457470 options = Object . assign ( defaults , options ) ;
458471
459- const merger = diff3Merge ( a , o , b , options ) ;
472+ const aSection = '<<<<<<<' + ( options . label . a ? ` ${ options . label . a } ` : '' ) ;
473+ const xSection = '=======' ;
474+ const bSection = '>>>>>>>' + ( options . label . b ? ` ${ options . label . b } ` : '' ) ;
475+
476+ const regions = diff3Merge ( a , o , b , options ) ;
460477 let conflict = false ;
461- let lines = [ ] ;
462- for ( let i = 0 ; i < merger . length ; i ++ ) {
463- const item = merger [ i ] ;
464- if ( item . ok ) {
465- lines = lines . concat ( item . ok ) ;
478+ let result = [ ] ;
479+
480+ regions . forEach ( region => {
481+ if ( region . ok ) {
482+ result = result . concat ( region . ok ) ;
466483 } else {
467- const c = diffComm ( item . conflict . a , item . conflict . b ) ;
484+ const c = diffComm ( region . conflict . a , region . conflict . b ) ;
468485 for ( let j = 0 ; j < c . length ; j ++ ) {
469486 let inner = c [ j ] ;
470487 if ( inner . common ) {
471- lines = lines . concat ( inner . common ) ;
488+ result = result . concat ( inner . common ) ;
472489 } else {
473490 conflict = true ;
474- lines = lines . concat (
475- [ '\n<<<<<<<<<\n' ] , inner . buffer1 ,
476- [ '\n=========\n' ] , inner . buffer2 ,
477- [ '\n>>>>>>>>>\n' ]
491+ result = result . concat (
492+ [ aSection ] ,
493+ inner . buffer1 ,
494+ [ xSection ] ,
495+ inner . buffer2 ,
496+ [ bSection ]
478497 ) ;
479498 }
480499 }
481500 }
482- }
501+ } ) ;
502+
483503 return {
484504 conflict : conflict ,
485- result : lines
505+ result : result
486506 } ;
487507}
488508
0 commit comments