Bladeren bron

Merge pull request #267 from ckeditor/t/266

Feature: introduce `Collection.has()` method. Closes #266.
Maciej 7 jaren geleden
bovenliggende
commit
d8183f2dae
2 gewijzigde bestanden met toevoegingen van 50 en 5 verwijderingen
  1. 22 5
      packages/ckeditor5-utils/src/collection.js
  2. 28 0
      packages/ckeditor5-utils/tests/collection.js

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

@@ -209,20 +209,37 @@ export default class Collection {
 		return item || null;
 		return item || null;
 	}
 	}
 
 
+	/**
+	 * Returns a boolean indicating whether the collection contains an item.
+	 *
+	 * @param {Object|String} itemOrId The item or its id in the collection.
+	 * @returns {Boolean} `true` if the collection contains the item, `false` otherwise.
+	 */
+	has( itemOrId ) {
+		if ( typeof itemOrId == 'string' ) {
+			return this._itemMap.has( itemOrId );
+		} else { // Object
+			const idProperty = this._idProperty;
+			const id = itemOrId[ idProperty ];
+
+			return this._itemMap.has( id );
+		}
+	}
+
 	/**
 	/**
 	 * Gets index of item in the collection.
 	 * Gets index of item in the collection.
 	 * When item is not defined in the collection then index will be equal -1.
 	 * When item is not defined in the collection then index will be equal -1.
 	 *
 	 *
-	 * @param {String|Object} idOrItem The item or its id in the collection.
+	 * @param {Object|String} itemOrId The item or its id in the collection.
 	 * @returns {Number} Index of given item.
 	 * @returns {Number} Index of given item.
 	 */
 	 */
-	getIndex( idOrItem ) {
+	getIndex( itemOrId ) {
 		let item;
 		let item;
 
 
-		if ( typeof idOrItem == 'string' ) {
-			item = this._itemMap.get( idOrItem );
+		if ( typeof itemOrId == 'string' ) {
+			item = this._itemMap.get( itemOrId );
 		} else {
 		} else {
-			item = idOrItem;
+			item = itemOrId;
 		}
 		}
 
 
 		return this._items.indexOf( item );
 		return this._items.indexOf( item );

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

@@ -321,6 +321,34 @@ describe( 'Collection', () => {
 		} );
 		} );
 	} );
 	} );
 
 
+	describe( 'has()', () => {
+		it( 'should return true if collection contains item with given id', () => {
+			collection.add( getItem( 'foo' ) );
+
+			expect( collection.has( 'foo' ) ).to.equal( true );
+		} );
+
+		it( 'should return false if collection does not contain item with given id', () => {
+			collection.add( getItem( 'foo' ) );
+
+			expect( collection.has( 'bar' ) ).to.equal( false );
+		} );
+
+		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 );
+		} );
+	} );
+
 	describe( 'getIndex()', () => {
 	describe( 'getIndex()', () => {
 		it( 'should return index of given item', () => {
 		it( 'should return index of given item', () => {
 			const item1 = { foo: 'bar' };
 			const item1 = { foo: 'bar' };