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

Commit

Permalink
Removed support for index.
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Jasiun committed Jan 3, 2019
1 parent ff644b1 commit a3eebbe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 45 deletions.
34 changes: 11 additions & 23 deletions src/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,29 +210,17 @@ export default class Collection {
}

/**
* Returns a boolean indicating whether the collection contains an item. Item id or index can be used instead of the
* item.
* Returns a boolean indicating whether the collection contains an item.
*
* @param {Object|String|Number} itemOrIdOrIndex The item, item id or item index in the collection.
* @param {Object|String} itemOrId The item or its id 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 );
has( itemOrId ) {
if ( typeof itemOrId == 'string' ) {
return this._itemMap.has( itemOrId );
} 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.' );
}
const id = itemOrId[ idProperty ];

return this._itemMap.has( id );
}
Expand All @@ -242,16 +230,16 @@ export default class Collection {
* Gets index of item in the collection.
* 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.
*/
getIndex( idOrItem ) {
getIndex( itemOrId ) {
let item;

if ( typeof idOrItem == 'string' ) {
item = this._itemMap.get( idOrItem );
if ( typeof itemOrId == 'string' ) {
item = this._itemMap.get( itemOrId );
} else {
item = idOrItem;
item = itemOrId;
}

return this._items.indexOf( item );
Expand Down
22 changes: 0 additions & 22 deletions tests/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,22 +334,6 @@ describe( 'Collection', () => {
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' );

Expand All @@ -363,12 +347,6 @@ describe( 'Collection', () => {

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()', () => {
Expand Down

0 comments on commit a3eebbe

Please sign in to comment.