Module

utils/difftochanges

@ckeditor/ckeditor5-utils/src/difftochanges

module

Filtering

Functions

  • diffToChanges( diff, output ) → Array.<Object>

    Creates a set of changes which need to be applied to the input in order to transform it into the output. This function can be used with strings or arrays.

    const input = Array.from( 'abc' );
    const output = Array.from( 'xaby' );
    const changes = diffToChanges( diff( input, output ), output );
    
    changes.forEach( change => {
    	if ( change.type == 'insert' ) {
    		input.splice( change.index, 0, ...change.values );
    	} else if ( change.type == 'delete' ) {
    		input.splice( change.index, change.howMany );
    	}
    } );
    
    input.join( '' ) == output.join( '' ); // -> true

    Parameters

    diff : Array.<('equal' | 'insert' | 'delete')>

    Result of diff.

    output : String | Array

    The string or array which was passed as diff's output.

    Returns

    Array.<Object>

    Set of changes (insert or delete) which need to be applied to the input in order to transform it into the output.