ソースを参照

Merge pull request #14 from ckeditor/t/13

Implemented collection.clear().
Piotrek Koszuliński 9 年 前
コミット
8f51e2f203

+ 9 - 0
packages/ckeditor5-utils/src/collection.js

@@ -242,6 +242,15 @@ export default class Collection {
 		return this._items.filter( callback, ctx );
 	}
 
+	/**
+	 * Removes all items from the collection.
+	 */
+	clear() {
+		while ( this.length ) {
+			this.remove( 0 );
+		}
+	}
+
 	/**
 	 * Collection iterator.
 	 */

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

@@ -485,6 +485,22 @@ describe( 'Collection', () => {
 		} );
 	} );
 
+	describe( 'clear', () => {
+		it( 'removes all items', () => {
+			const items = [ {}, {}, {} ];
+			const spy = sinon.spy();
+
+			collection.on( 'remove', spy );
+
+			items.forEach( i => collection.add( i ) );
+
+			collection.clear();
+
+			expect( spy.callCount ).to.equal( 3 );
+			expect( collection.length ).to.equal( 0 );
+		} );
+	} );
+
 	describe( 'iterator', () => {
 		it( 'covers the whole collection', () => {
 			let item1 = getItem( 'foo' );