Skip to content

Remove unused move feature from SnakList #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 1 addition & 155 deletions src/SnakList.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ wb.datamodel.SnakList = util.inherit( 'WbDataModelSnakList', PARENT, function( s
*
* @param {string} propertyId
* @return {wikibase.datamodel.SnakList}
* @private
*/
getFilteredSnakList: function( propertyId ) {
if( !propertyId ) {
Expand Down Expand Up @@ -94,161 +95,6 @@ wb.datamodel.SnakList = util.inherit( 'WbDataModelSnakList', PARENT, function( s
} );

return propertyIds;
},

/**
* Returns the indices of the snak list where a certain snak may be moved to. A snak may be
* moved within its property group. It may also be moved to the slots between property groups
* which involves moving the whole property group the snak belongs to.
*
* @param {wikibase.datamodel.Snak} snak
* @return {number[]}
*/
getValidMoveIndices: function( snak ) {
var self = this,
indices = [],
isGroupLast = false;

this.each( function( i, snakListSnak ) {
if( snakListSnak.getPropertyId() === snak.getPropertyId() ) {
// Detect slots within the snak's property group.
if( snakListSnak !== snak ) {
indices.push( i );
} else {
var nextSnak = self._items[i + 1];
if( nextSnak && nextSnak.getPropertyId() !== snak.getPropertyId() ) {
// Snak is the last of its group.
isGroupLast = true;
}
}
} else {
// Detect slots between property groups.
var previousSnak = self._items[i - 1],
isNewPropertyGroup = (
i !== 0
&& snakListSnak.getPropertyId() !== previousSnak.getPropertyId()
);

if(
// Since this snak's property group is not at the top of the snak list, the
// snak (with its group) may always be moved to the top:
i === 0
// The snak (with its group) may always be moved to between groups except to
// adjacent slots between property groups since the snak's property group would
// in fact not be moved.
|| isNewPropertyGroup && previousSnak.getPropertyId() !== snak.getPropertyId()
) {
indices.push( i );
}
}
} );

// Allow moving to last position if snak is not at the end already:
if( snak !== this._items[this._items.length - 1] ) {
indices.push( this._items.length );
}

return indices;
},

/**
* Moves a SnakList's Snak to a new index.
*
* @param {wikibase.datamodel.Snak} snak Snak to move within the list.
* @param {number} toIndex
* @return {wikibase.datamodel.SnakList} This SnakList object.
*
* @throws {Error} if snak is not allowed to be moved to toIndex.
*/
move: function( snak, toIndex ) {
if( this.indexOf( snak ) === toIndex ) {
return this;
}

var validIndices = this.getValidMoveIndices( snak );

if( $.inArray( toIndex, validIndices ) === -1 ) {
throw new Error( 'Tried to move snak to index ' + toIndex + ' but only the following '
+ 'indices are allowed: ' + validIndices.join( ', ' ) );
}

var previousSnak = this._items[toIndex -1],
nextSnak = this._items[toIndex + 1],
insertBefore = this._items[toIndex];

if(
previousSnak && previousSnak.getPropertyId() === snak.getPropertyId()
|| nextSnak && nextSnak.getPropertyId() === snak.getPropertyId()
) {
// Moving snak within its property group.
this._items.splice( this.indexOf( snak ), 1 );

if( insertBefore ) {
this._items.splice( toIndex, 0, snak );
} else {
this._items.push( snak );
}
} else {
// Moving the whole snak group.
var groupedSnaks = [];

for( var i = 0; i < this._items.length; i++ ) {
if( this._items[i].getPropertyId() === snak.getPropertyId() ) {
groupedSnaks.push( this._items[i] );
}
}

for( i = 0; i < groupedSnaks.length; i++ ) {
this._items.splice( this.indexOf( groupedSnaks[i] ), 1 );
if( insertBefore ) {
this._items.splice( this.indexOf( insertBefore ), 0, groupedSnaks[i] );
} else {
this._items.push( groupedSnaks[i] );
}
}
}

return this;
},

/**
* Moves a snak towards the top of the snak list by one step.
*
* @param {wikibase.datamodel.Snak} snak
* @return {wikibase.datamodel.SnakList} This SnakList object.
*/
moveUp: function( snak ) {
var index = this.indexOf( snak ),
validIndices = this.getValidMoveIndices( snak );

for( var i = validIndices.length - 1; i >= 0; i-- ) {
if( validIndices[i] < index ) {
this.move( snak, validIndices[i] );
break;
}
}

return this;
},

/**
* Moves a snak towards the bottom of the snak list by one step.
*
* @param {wikibase.datamodel.Snak} snak
* @return {wikibase.datamodel.SnakList} This SnakList object.
*/
moveDown: function( snak ) {
var index = this.indexOf( snak ),
validIndices = this.getValidMoveIndices( snak );

for( var i = 0; i < validIndices.length; i++ ) {
if( validIndices[i] > index ) {
this.move( snak, validIndices[i] );
break;
}
}

return this;
}
} );

Expand Down
155 changes: 0 additions & 155 deletions tests/SnakList.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,159 +125,4 @@ QUnit.test( 'merge()', function( assert ) {
}
} );

QUnit.test( 'getValidMoveIndices()', function( assert ) {
assert.expect( 8 );
var snaks = testSets[2],
snakList = new wb.datamodel.SnakList( snaks );

/**
* Expected indices where the individual snaks (with or without its groups) may be moved to.
* @property {number[][]}
*/
var validIndices = [
[1, 5, 6, 7],
[0, 5, 6, 7],
[0, 3, 4, 6, 7],
[0, 2, 4, 6, 7],
[0, 2, 3, 6, 7],
[0, 2, 7],
[0, 2, 5]
];

for( var i = 0; i < validIndices.length; i++ ) {
assert.deepEqual(
snakList.getValidMoveIndices( snaks[i] ),
validIndices[i],
'Verified indices example Snak #' + i + ' may be moved to.'
);
}

snakList = new wb.datamodel.SnakList(
[ new wb.datamodel.PropertyValueSnak( 'P1', new dv.StringValue( 'a' ) ) ]
);

assert.strictEqual(
snakList.getValidMoveIndices( snakList.toArray()[0] ).length,
0,
'No indices returned when SnakList does not contain more than one Snak.'
);

} );

QUnit.test( 'move()', function( assert ) {
assert.expect( 30 );
var snaks = testSets[2],
snakList;

/**
* Array of test case definitions. Test case definition structure:
* [0] => Index of element to move
* [1] => Index where to move element
* [2] => Expected result when concatenating the string values of the SnakList's Snaks.
* @property {*[][]}
*/
var testCases = [
[ 0, 1, 'bacdefg' ],
[ 0, 5, 'cdeabfg' ],
[ 0, 6, 'cdefabg' ],
[ 0, 7, 'cdefgab' ],
[ 1, 0, 'bacdefg' ],
[ 1, 5, 'cdeabfg' ],
[ 1, 6, 'cdefabg' ],
[ 1, 7, 'cdefgab' ],
[ 2, 0, 'cdeabfg' ],
[ 2, 3, 'abdcefg' ],
[ 2, 4, 'abdecfg' ],
[ 2, 6, 'abfcdeg' ],
[ 2, 7, 'abfgcde' ],
[ 3, 0, 'cdeabfg' ],
[ 3, 2, 'abdcefg' ],
[ 3, 4, 'abcedfg' ],
[ 3, 6, 'abfcdeg' ],
[ 3, 7, 'abfgcde' ],
[ 4, 0, 'cdeabfg' ],
[ 4, 2, 'abecdfg' ],
[ 4, 3, 'abcedfg' ],
[ 4, 6, 'abfcdeg' ],
[ 4, 7, 'abfgcde' ],
[ 5, 0, 'fabcdeg' ],
[ 5, 2, 'abfcdeg' ],
[ 5, 7, 'abcdegf' ],
[ 6, 0, 'gabcdef' ],
[ 6, 2, 'abgcdef' ],
[ 6, 5, 'abcdegf' ]
];

for( var i = 1; i < testCases.length; i++ ) {
snakList = new wb.datamodel.SnakList( snaks );

snakList.move( snaks[testCases[i][0]], testCases[i][1] );

assert.equal(
snakOrder( snakList ),
testCases[i][2],
'Verified moving a Snak with test set #' + i + '.'
);
}

snakList = new wb.datamodel.SnakList( snaks );
snakList.move( snaks[0], 0 );

assert.equal(
snakOrder( snakList ),
'abcdefg',
'Nothing changed when trying to move a Snak to an index it already has.'
);

assert.throws(
function() {
snakList = new wb.datamodel.SnakList( snaks );
snakList.move( 0, 4 );
},
'move() throws an error when trying to move a Snak to an invalid index.'
);
} );

QUnit.test( 'moveUp() and moveDown()', function( assert ) {
assert.expect( 14 );
var snaks = testSets[2],
snakList;

/**
* Array of test case definitions for moveUp() and moveDown() methods. Test case definition
* structure:
* [0] => Resulting order after moving the element having the same index in the SnakList up.
* [1] => Resulting order after moving the element having the same index in the SnakList down.
* @property {string[][]}
*/
var testCases = [
['abcdefg', 'bacdefg' ],
['bacdefg', 'cdeabfg' ],
['cdeabfg', 'abdcefg' ],
['abdcefg', 'abcedfg' ],
['abcedfg', 'abfcdeg' ],
['abfcdeg', 'abcdegf' ],
['abcdegf', 'abcdefg' ]
];

for( var i = 0; i < testCases.length; i++ ) {
snakList = new wb.datamodel.SnakList( snaks );

assert.equal(
snakOrder( snakList.moveUp( snaks[i] ) ),
testCases[i][0],
'Verified result of moveUp() with test set #' + i + '.'
);

snakList = new wb.datamodel.SnakList( snaks );

assert.equal(
snakOrder( snakList.moveDown( snaks[i] ) ),
testCases[i][1],
'Verified result of moveDown() with test set #' + i + '.'
);

}
} );

}( wikibase, dataValues, jQuery, QUnit ) );