Sfoglia il codice sorgente

Does not throw from get() so we don't need to introduce has().

Piotrek Koszuliński 10 anni fa
parent
commit
93d8d03439

+ 2 - 8
packages/ckeditor5-utils/src/collection.js

@@ -58,16 +58,10 @@ CKEDITOR.define( [ 'emittermixin', 'ckeditorerror', 'utils' ], function( Emitter
 		 * Gets one item from the collection.
 		 *
 		 * @param {Number} index The index to take the item from.
-		 * @returns {Model} The requested item.
+		 * @returns {Model} The requested item or `null` if such item does not exist.
 		 */
 		get( index ) {
-			var model = this._models[ index ];
-
-			if ( !model ) {
-				throw new Error( 'Index not found' );
-			}
-
-			return model;
+			return this._models[ index ] || null;
 		}
 
 		/**

+ 2 - 14
packages/ckeditor5-utils/src/namedcollection.js

@@ -77,22 +77,10 @@ CKEDITOR.define( [ 'emittermixin', 'ckeditorerror', 'utils' ], function( Emitter
 		 * Gets one item from the collection.
 		 *
 		 * @param {String} name The name of the item to take.
-		 * @returns {Model} The requested item.
+		 * @returns {Model} The requested item or `null` if such item does not exist.
 		 */
 		get( name ) {
-			var model = this._models.get( name );
-
-			if ( !model ) {
-				/**
-				 * Model not found.
-				 *
-				 * @error namedcollection-get
-				 * @param {String} name Name of the model.
-				 */
-				throw new CKEditorError( 'namedcollection-get: Model not found.', { name: name } );
-			}
-
-			return model;
+			return this._models.get( name ) || null;
 		}
 
 		/**

+ 2 - 4
packages/ckeditor5-utils/tests/mvc/collection/collection.js

@@ -36,13 +36,11 @@ describe( 'add', function() {
 } );
 
 describe( 'get', function() {
-	it( 'should throw an error on invalid index', function() {
+	it( 'should return null if index does not exist', function() {
 		var box = getCollection();
 		box.add( getItem() );
 
-		expect( function() {
-			box.get( 1 );
-		} ).to.throw( Error, 'Index not found' );
+		expect( box.get( 1 ) ).to.be.null;
 	} );
 } );
 

+ 1 - 5
packages/ckeditor5-utils/tests/mvc/collection/namedcollection.js

@@ -63,14 +63,10 @@ describe( 'add', function() {
 
 describe( 'get', function() {
 	it( 'should throw an error on invalid name', function() {
-		var CKEditorError = modules.ckeditorerror;
 		var box = getCollection();
-
 		box.add( getItem( 'foo' ) );
 
-		expect( function() {
-			box.get( 'bar' );
-		} ).to.throw( CKEditorError, /^namedcollection-get/ );
+		expect( box.get( 'bar' ) ).to.be.null;
 	} );
 } );