Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge ff644b1 into 4ad23b1
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Jasiun committed Jan 3, 2019
2 parents 4ad23b1 + ff644b1 commit fba9e13
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,35 @@ export default class Collection {
return item || null;
}

/**
* Returns a boolean indicating whether the collection contains an item. Item id or index can be used instead of the
* item.
*
* @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.
*/
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.' );
}

return this._itemMap.has( id );
}
}

/**
* Gets index of item in the collection.
* When item is not defined in the collection then index will be equal -1.
Expand Down
50 changes: 50 additions & 0 deletions tests/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,56 @@ 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 on index', () => {
collection.add( getItem( 'foo' ) );
collection.add( getItem( 'bar' ) );

expect( collection.has( 0 ) ).to.equal( true );
expect( collection.has( 1 ) ).to.equal( true );
} );

it( 'should return false if collection does not contain item on index', () => {
collection.add( getItem( 'foo' ) );
collection.add( getItem( 'bar' ) );

expect( collection.has( -1 ) ).to.equal( false );
expect( collection.has( 2 ) ).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 );
} );

it( 'should throw if an object without id is given', () => {
expect( () => {
collection.has( {} );
} ).to.throw( CKEditorError, /^collection-has-invalid-id/ );
} );
} );

describe( 'getIndex()', () => {
it( 'should return index of given item', () => {
const item1 = { foo: 'bar' };
Expand Down

0 comments on commit fba9e13

Please sign in to comment.