瀏覽代碼

Added support for objects to `collection.has`.

Piotr Jasiun 7 年之前
父節點
當前提交
05aed74b6c
共有 2 個文件被更改,包括 41 次插入19 次删除
  1. 22 14
      packages/ckeditor5-utils/src/collection.js
  2. 19 5
      packages/ckeditor5-utils/tests/collection.js

+ 22 - 14
packages/ckeditor5-utils/src/collection.js

@@ -210,24 +210,32 @@ export default class Collection {
 	}
 	}
 
 
 	/**
 	/**
-	 * Returns a boolean indicating whether the collection contains an item with the specified id or index.
+	 * Returns a boolean indicating whether the collection contains an item. Item id or index can be used instead of the
+	 * item.
 	 *
 	 *
-	 * @param {String|Number} idOrIndex The item id or index in the collection.
+	 * @param {Object|String|Number} itemOrIdOrIndex The item, item id or item index in the collection.
 	 * @returns {Boolean} `true` if the collection contains the item, `false` otherwise.
 	 * @returns {Boolean} `true` if the collection contains the item, `false` otherwise.
 	 */
 	 */
-	has( idOrIndex ) {
-		if ( typeof idOrIndex == 'string' ) {
-			return this._itemMap.has( idOrIndex );
-		} else if ( typeof idOrIndex == 'number' ) {
-			return !!this._items[ idOrIndex ];
-		}
+	has( itemOrIdOrIndex ) {
+		if ( typeof itemOrIdOrIndex == 'number' ) {
+			return !!this._items[ itemOrIdOrIndex ];
+		} else if ( typeof itemOrIdOrIndex == 'string' ) {
+			return this._itemMap.has( itemOrIdOrIndex );
+		} else { // Object
+			const idProperty = this._idProperty;
+			const id = itemOrIdOrIndex[ idProperty ];
+
+			if ( typeof id != 'string' ) {
+				/**
+				 * This item's id should be a string.
+				 *
+				 * @error collection-has-invalid-id
+				 */
+				throw new CKEditorError( 'collection-has-invalid-id: This item\'s id should be a string.' );
+			}
 
 
-		/**
-		 * Index or id must be given.
-		 *
-		 * @error collection-has-invalid-arg
-		 */
-		throw new CKEditorError( 'collection-has-invalid-arg: Index or id must be given.' );
+			return this._itemMap.has( id );
+		}
 	}
 	}
 
 
 	/**
 	/**

+ 19 - 5
packages/ckeditor5-utils/tests/collection.js

@@ -322,13 +322,13 @@ describe( 'Collection', () => {
 	} );
 	} );
 
 
 	describe( 'has()', () => {
 	describe( 'has()', () => {
-		it( 'should return true if collection contains item', () => {
+		it( 'should return true if collection contains item with given id', () => {
 			collection.add( getItem( 'foo' ) );
 			collection.add( getItem( 'foo' ) );
 
 
 			expect( collection.has( 'foo' ) ).to.equal( true );
 			expect( collection.has( 'foo' ) ).to.equal( true );
 		} );
 		} );
 
 
-		it( 'should return false if collection does not contain item', () => {
+		it( 'should return false if collection does not contain item with given id', () => {
 			collection.add( getItem( 'foo' ) );
 			collection.add( getItem( 'foo' ) );
 
 
 			expect( collection.has( 'bar' ) ).to.equal( false );
 			expect( collection.has( 'bar' ) ).to.equal( false );
@@ -350,10 +350,24 @@ describe( 'Collection', () => {
 			expect( collection.has( 2 ) ).to.equal( false );
 			expect( collection.has( 2 ) ).to.equal( false );
 		} );
 		} );
 
 
-		it( 'should throw if neither string or number given', () => {
+		it( 'should return true if collection contains item', () => {
+			const item = getItem( 'foo' );
+
+			collection.add( item );
+
+			expect( collection.has( item ) ).to.equal( true );
+		} );
+
+		it( 'should return false if collection does not contains item', () => {
+			collection.add( getItem( 'foo' ) );
+
+			expect( collection.has( getItem( 'bar' ) ) ).to.equal( false );
+		} );
+
+		it( 'should throw if an object without id is given', () => {
 			expect( () => {
 			expect( () => {
-				collection.has( true );
-			} ).to.throw( CKEditorError, /^collection-has-invalid-arg/ );
+				collection.has( {} );
+			} ).to.throw( CKEditorError, /^collection-has-invalid-id/ );
 		} );
 		} );
 	} );
 	} );