Browse Source

Merge pull request #79 from ckeditor/t/55

Editor should throw if config.creator is not set
Piotr Jasiun 10 năm trước cách đây
mục cha
commit
f188e7158f

+ 1 - 1
packages/ckeditor5-utils/src/document/element.js

@@ -10,7 +10,7 @@ CKEDITOR.define( [
 	'document/nodelist',
 	'document/range',
 	'document/position'
-], ( Node, NodeList, Range, Position ) => {
+], ( Node, NodeList ) => {
 	/**
 	 * Tree data model element.
 	 *

+ 18 - 0
packages/ckeditor5-utils/src/utils.js

@@ -89,6 +89,24 @@ CKEDITOR.define( [ 'utils-lodash', 'lib/lodash/lodash-ckeditor' ], ( lodashInclu
 				// Compared array is longer so it is a suffix of the other array.
 				return utils.compareArrays.EXTENSION;
 			}
+		},
+
+		/**
+		 * Returns `nth` (starts from `0` of course) item of an `iterable`.
+		 *
+		 * @param {Number} index
+		 * @param {Iterable.<*>} iterable
+		 * @returns {*}
+		 */
+		nth( index, iterable ) {
+			for ( let item of iterable ) {
+				if ( index === 0 ) {
+					return item;
+				}
+				index -= 1;
+			}
+
+			return null;
 		}
 	};
 

+ 28 - 0
packages/ckeditor5-utils/tests/utils/utils.js

@@ -150,6 +150,34 @@ describe( 'utils', () => {
 		} );
 	} );
 
+	describe( 'nth', () => {
+		it( 'should return 0th item', () => {
+			expect( utils.nth( 0, getIterator() ) ).to.equal( 11 );
+		} );
+
+		it( 'should return the last item', () => {
+			expect( utils.nth( 2, getIterator() ) ).to.equal( 33 );
+		} );
+
+		it( 'should return null if out of range (bottom)', () => {
+			expect( utils.nth( -1, getIterator() ) ).to.be.null;
+		} );
+
+		it( 'should return null if out of range (top)', () => {
+			expect( utils.nth( 3, getIterator() ) ).to.be.null;
+		} );
+
+		it( 'should return null if iterator is empty', () => {
+			expect( utils.nth( 0, [] ) ).to.be.null;
+		} );
+
+		function *getIterator() {
+			yield 11;
+			yield 22;
+			yield 33;
+		}
+	} );
+
 	describe( 'Lo-Dash extensions', () => {
 		// Ensures that the required Lo-Dash extensions are available in `utils`.
 		it( 'should be exposed in utils', () => {