瀏覽代碼

Merge pull request #195 from ckeditor/t/194

Feature: Introduced `Collection#first` and `Collection#last` getters. Closes #194.
Piotr Jasiun 8 年之前
父節點
當前提交
d6a84deb62
共有 2 個文件被更改,包括 65 次插入5 次删除
  1. 22 4
      packages/ckeditor5-utils/src/collection.js
  2. 43 1
      packages/ckeditor5-utils/tests/collection.js

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

@@ -28,10 +28,10 @@ export default class Collection {
 	/**
 	 * Creates a new Collection instance.
 	 *
-	 * @param {Object} options The options object.
+	 * @param {Object} [options={}] The options object.
 	 * @param {String} [options.idProperty='id'] The name of the property which is considered to identify an item.
 	 */
-	constructor( options ) {
+	constructor( options = {} ) {
 		/**
 		 * The internal list of items in the collection.
 		 *
@@ -54,7 +54,7 @@ export default class Collection {
 		 * @private
 		 * @member {String}
 		 */
-		this._idProperty = options && options.idProperty || 'id';
+		this._idProperty = options.idProperty || 'id';
 
 		/**
 		 * A helper mapping external items of a bound collection ({@link #bindTo})
@@ -99,6 +99,24 @@ export default class Collection {
 	}
 
 	/**
+	 * Returns the first item from the collection or null when collection is empty.
+	 *
+	 * @returns {Object|null} The first item or `null` if collection is empty.
+	 */
+	get first() {
+		return this._items[ 0 ] || null;
+	}
+
+	/**
+	 * Returns the last item from the collection or null when collection is empty.
+	 *
+	 * @returns {Object|null} The last item or `null` if collection is empty.
+	 */
+	get last() {
+		return this._items[ this.length - 1 ] || null;
+	}
+
+	/**
 	 * Adds an item into the collection.
 	 *
 	 * If the item does not have an id, then it will be automatically generated and set on the item.
@@ -162,7 +180,7 @@ export default class Collection {
 	 * Gets item by its id or index.
 	 *
 	 * @param {String|Number} idOrIndex The item id or index in the collection.
-	 * @returns {Object} The requested item or `null` if such item does not exist.
+	 * @returns {Object|null} The requested item or `null` if such item does not exist.
 	 */
 	get( idOrIndex ) {
 		let item;

+ 43 - 1
packages/ckeditor5-utils/tests/collection.js

@@ -40,6 +40,48 @@ describe( 'Collection', () => {
 		} );
 	} );
 
+	describe( 'length', () => {
+		it( 'should return collection length', () => {
+			expect( collection.length ).to.equal( 0 );
+
+			collection.add( { foo: 'bar' } );
+
+			expect( collection.length ).to.equal( 1 );
+		} );
+	} );
+
+	describe( 'first', () => {
+		it( 'should return the first item from the collection', () => {
+			const item1 = { foo: 'bar' };
+			const item2 = { bar: 'biz' };
+
+			collection.add( item1 );
+			collection.add( item2 );
+
+			expect( collection.first ).to.equal( item1 );
+		} );
+
+		it( 'should return null when collection is empty', () => {
+			expect( collection.first ).to.null;
+		} );
+	} );
+
+	describe( 'last', () => {
+		it( 'should return the last item from the collection', () => {
+			const item1 = { foo: 'bar' };
+			const item2 = { bar: 'biz' };
+
+			collection.add( item1 );
+			collection.add( item2 );
+
+			expect( collection.last ).to.equal( item2 );
+		} );
+
+		it( 'should return null when collection is empty', () => {
+			expect( collection.last ).to.null;
+		} );
+	} );
+
 	describe( 'add()', () => {
 		it( 'should be chainable', () => {
 			expect( collection.add( {} ) ).to.equal( collection );
@@ -376,7 +418,7 @@ describe( 'Collection', () => {
 		} );
 
 		it( 'should remove the model by model - custom id property', () => {
-			const collection = new Collection( null, 'name' );
+			const collection = new Collection( { idProperty: 'name' } );
 			const item = getItem( 'foo', 'name' );
 
 			collection.add( item );