Browse Source

Merge pull request #14 from ckeditor/t/13

Implemented collection.clear().
Piotrek Koszuliński 9 years ago
parent
commit
8f51e2f203

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

@@ -243,6 +243,15 @@ export default class Collection {
 	}
 
 	/**
+	 * Removes all items from the collection.
+	 */
+	clear() {
+		while ( this.length ) {
+			this.remove( 0 );
+		}
+	}
+
+	/**
 	 * Collection iterator.
 	 */
 	[ Symbol.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' );