8
0
Просмотр исходного кода

Renamed batchify() to diffToChanges().

Piotrek Koszuliński 9 лет назад
Родитель
Сommit
b244f197da

+ 5 - 5
packages/ckeditor5-utils/src/diff.js

@@ -9,7 +9,7 @@
 // by Sun Wu, Udi Manber, Gene Myers, Webb Miller.
 
 /**
- * Calculates the difference between two arrays or strings producing an array containing a list of actions
+ * Calculates the difference between two arrays or strings producing an array containing a list of changes
  * necessary to transform input into output.
  *
  *		diff( 'aba', 'acca' ); // [ 'EQUAL', 'INSERT', 'INSERT', 'DELETE', 'EQUAL' ]
@@ -18,7 +18,7 @@
  * @param {Array|String} a Input array or string.
  * @param {Array|String} b Output array or string.
  * @param {Function} [cmp] Optional function used to compare array values, by default === is used.
- * @return {Array} Array of actions.
+ * @returns {Array} Array of changes.
  */
 export default function diff( a, b, cmp ) {
 	// Set the comparator function.
@@ -62,12 +62,12 @@ export default function diff( a, b, cmp ) {
 		// The way we should go to get further.
 		const dir = y1 > y2 ? -1 : 1;
 
-		// Clone previous actions array (if any).
+		// Clone previous changes array (if any).
 		if ( es[ k + dir ] ) {
 			es[ k ] = es[ k + dir ].slice( 0 );
 		}
 
-		// Create actions array.
+		// Create changes array.
 		if ( !es[ k ] ) {
 			es[ k ] = [];
 		}
@@ -112,7 +112,7 @@ export default function diff( a, b, cmp ) {
 		p++;
 	} while ( fp[ delta ] !== n );
 
-	// Return the final list of edit actions.
+	// Return the final list of edit changes.
 	// We remove the first item that represents the action for the injected nulls.
 	return es[ delta ].slice( 1 );
 }

+ 14 - 14
packages/ckeditor5-utils/src/batchify.js → packages/ckeditor5-utils/src/difftochanges.js

@@ -6,31 +6,31 @@
 'use strict';
 
 /**
- * Creates a set of operations which need to be applied to the input in order to transform
- * it into the output. Can be used with strings or arrays.
+ * 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 batch = batchify( diff( input, output ), output );
+ *		const changes = diffToChanges( diff( input, output ), output );
  *
- *		batch.forEach( operation => {
- *			if ( operation.type == 'INSERT' ) {
- *				input.splice( operation.index, 0, ...operation.values );
- *			} else if ( operation.type == 'DELETE' ) {
- *				input.splice( operation.index, operation.howMany );
+ *		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
  *
- * @method utils.batchify
+ * @method utils.diffToChanges
  * @param {Array.<'EQUAL'|'INSERT'|'DELETE'>} diff Result of {@link utils.diff}.
  * @param {String|Array} output The string or array which was passed as diff's output.
- * @returns {Array.<Object>} Set of operations (insert or delete) which need to be applied to the input
+ * @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.
  */
-export default function batchify( diff, output ) {
-	const batch = [];
+export default function diffToChanges( diff, output ) {
+	const changes = [];
 	let index = 0;
 	let lastOperation;
 
@@ -70,11 +70,11 @@ export default function batchify( diff, output ) {
 
 	pushLast();
 
-	return batch;
+	return changes;
 
 	function pushLast() {
 		if ( lastOperation ) {
-			batch.push( lastOperation );
+			changes.push( lastOperation );
 			lastOperation = null;
 		}
 	}

+ 17 - 17
packages/ckeditor5-utils/tests/batchify.js → packages/ckeditor5-utils/tests/difftochanges.js

@@ -6,9 +6,9 @@
 'use strict';
 
 import diff from '/ckeditor5/utils/diff.js';
-import batchify from '/ckeditor5/utils/batchify.js';
+import diffToChanges from '/ckeditor5/utils/difftochanges.js';
 
-describe( 'batchify', () => {
+describe( 'diffToChanges', () => {
 	describe( 'equal patterns', () => {
 		test( 0,		'',				'' );
 		test( 0,		'abc',			'abc' );
@@ -50,35 +50,35 @@ describe( 'batchify', () => {
 	it( 'works with arrays', () => {
 		const input = Array.from( 'abc' );
 		const output = Array.from( 'xaby' );
-		const batch = batchify( diff( input, output ), output );
+		const changes = diffToChanges( diff( input, output ), output );
 
-		batch.forEach( operation => {
-			if ( operation.type == 'INSERT' ) {
-				input.splice( operation.index, 0, ...operation.values );
-			} else if ( operation.type == 'DELETE' ) {
-				input.splice( operation.index, operation.howMany );
+		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 );
 			}
 		} );
 
 		expect( input ).to.deep.equal( output );
-		expect( batch ).to.have.lengthOf( 3 );
+		expect( changes ).to.have.lengthOf( 3 );
 	} );
 
-	function test( expectedOperationNumber, oldStr, newStr ) {
+	function test( expectedChangeNumber, oldStr, newStr ) {
 		it( `${ oldStr } => ${ newStr }`, () => {
-			const batch = batchify( diff( oldStr, newStr ), newStr );
+			const changes = diffToChanges( diff( oldStr, newStr ), newStr );
 			const oldStrChars = Array.from( oldStr );
 
-			batch.forEach( operation => {
-				if ( operation.type == 'INSERT' ) {
-					oldStrChars.splice( operation.index, 0, ...operation.values );
-				} else if ( operation.type == 'DELETE' ) {
-					oldStrChars.splice( operation.index, operation.howMany );
+			changes.forEach( change => {
+				if ( change.type == 'INSERT' ) {
+					oldStrChars.splice( change.index, 0, ...change.values );
+				} else if ( change.type == 'DELETE' ) {
+					oldStrChars.splice( change.index, change.howMany );
 				}
 			} );
 
 			expect( oldStrChars.join( '' ) ).to.equal( newStr );
-			expect( batch ).to.have.lengthOf( expectedOperationNumber );
+			expect( changes ).to.have.lengthOf( expectedChangeNumber );
 		} );
 	}
 } );