Bladeren bron

Changed enum constants to strings in TreeModel.

Szymon Cofalik 10 jaren geleden
bovenliggende
commit
16dc02cfa7
2 gewijzigde bestanden met toevoegingen van 18 en 34 verwijderingen
  1. 15 31
      packages/ckeditor5-utils/src/utils.js
  2. 3 3
      packages/ckeditor5-utils/tests/utils.js

+ 15 - 31
packages/ckeditor5-utils/src/utils.js

@@ -5,6 +5,13 @@
 
 'use strict';
 
+/**
+ * An index at which arrays differ. If arrays are same at all indexes, it represents how arrays are related.
+ * In this case, possible values are: 'SAME', 'PREFIX' or 'EXTENSION'.
+ *
+ * @typedef {String|Number} utils.ArrayRelation
+ */
+
 const utils = {
 	/**
 	 * Creates a spy function (ala Sinon.js) that can be used to inspect call to it.
@@ -38,7 +45,7 @@ const utils = {
 	/**
 	 * Checks if value implements iterator interface.
 	 *
-	 * @param {Mixed} value The value to check.
+	 * @param {*} value The value to check.
 	 * @returns {Boolean} True if value implements iterator interface.
 	 */
 	isIterable( value ) {
@@ -51,17 +58,15 @@ const utils = {
 	 * a flag specifying the relation is returned. Flags are negative numbers, so whenever a number >= 0 is returned
 	 * it means that arrays differ.
 	 *
-	 *   compareArrays( [ 0, 2 ], [ 0, 2 ] ); // SAME
-	 *   compareArrays( [ 0, 2 ], [ 0, 2, 1 ] ); // PREFIX
-	 *   compareArrays( [ 0, 2 ], [ 0 ] ); // EXTENSION
+	 *   compareArrays( [ 0, 2 ], [ 0, 2 ] ); // 'SAME'
+	 *   compareArrays( [ 0, 2 ], [ 0, 2, 1 ] ); // 'PREFIX'
+	 *   compareArrays( [ 0, 2 ], [ 0 ] ); // 'EXTENSION'
 	 *   compareArrays( [ 0, 2 ], [ 1, 2 ] ); // 0
 	 *   compareArrays( [ 0, 2 ], [ 0, 1 ] ); // 1
 	 *
 	 * @param {Array} a Array that is compared.
 	 * @param {Array} b Array to compare with.
-	 * @returns {Number} An index at which arrays differ, or if they do not differ, how array `a` is related to array `b`.
-	 * This is represented by one of flags: `a` is {@link utils.compareArrays#SAME same}, `a` is
-	 * a {@link utils.compareArrays#PREFIX prefix) or `a` is an {@link utils.compareArrays#EXTENSION extension}.
+	 * @returns {utils.ArrayRelation} How array `a` is related to `b`.
 	 */
 	compareArrays( a, b ) {
 		const minLen = Math.min( a.length, b.length );
@@ -76,13 +81,13 @@ const utils = {
 		// Both arrays were same at all points.
 		if ( a.length == b.length ) {
 			// If their length is also same, they are the same.
-			return utils.compareArrays.SAME;
+			return 'SAME';
 		} else if ( a.length < b.length ) {
 			// Compared array is shorter so it is a prefix of the other array.
-			return utils.compareArrays.PREFIX;
+			return 'PREFIX';
 		} else {
 			// Compared array is longer so it is an extension of the other array.
-			return utils.compareArrays.EXTENSION;
+			return 'EXTENSION';
 		}
 	},
 
@@ -105,25 +110,4 @@ const utils = {
 	}
 };
 
-/**
- * Flag for "is same as" relation between arrays.
- *
- * @type {Number}
- */
-utils.compareArrays.SAME = -1;
-
-/**
- * Flag for "is a prefix of" relation between arrays.
- *
- * @type {Number}
- */
-utils.compareArrays.PREFIX = -2;
-
-/**
- * Flag for "is a suffix of" relation between arrays.
- *
- * @type {number}
- */
-utils.compareArrays.EXTENSION = -3;
-
 export default utils;

+ 3 - 3
packages/ckeditor5-utils/tests/utils.js

@@ -85,7 +85,7 @@ describe( 'utils', () => {
 
 			let result = utils.compareArrays( a, b );
 
-			expect( result ).to.equal( utils.compareArrays.SAME );
+			expect( result ).to.equal( 'SAME' );
 		} );
 
 		it( 'should return PREFIX flag, when all n elements of first array are same as n first elements of the second array', () => {
@@ -94,7 +94,7 @@ describe( 'utils', () => {
 
 			let result = utils.compareArrays( a, b );
 
-			expect( result ).to.equal( utils.compareArrays.PREFIX );
+			expect( result ).to.equal( 'PREFIX' );
 		} );
 
 		it( 'should return EXTENSION flag, when n first elements of first array are same as all elements of the second array', () => {
@@ -103,7 +103,7 @@ describe( 'utils', () => {
 
 			let result = utils.compareArrays( a, b );
 
-			expect( result ).to.equal( utils.compareArrays.EXTENSION );
+			expect( result ).to.equal( 'EXTENSION' );
 		} );
 
 		it( 'should return index on which arrays differ, when arrays are not the same', () => {