Fix ve.batchSplice() to behave in line with docs

* Actually return the spliced data like the docs claim we do
* Remove false claim that offset can be negative
* Add that data=[] && remove=0 is invalid; native splice() doesn't allow
  this, and there is a case where we call native splice() directly
* Add tests

Change-Id: I90e77c1b22ea1c36cb61e89ea47831885a0b1cb9
This commit is contained in:
Catrope 2012-10-29 18:38:48 -07:00 committed by Trevor Parscal
parent 82a15d66fc
commit 6ef9fa78ff
2 changed files with 39 additions and 7 deletions

View file

@ -537,3 +537,32 @@ QUnit.test( 'getOpeningHtmlTag', 5, function ( assert ) {
} );
}() );
QUnit.test( 'batchSplice', 8, function ( assert ) {
var actual = [ 'a', 'b', 'c', 'd', 'e' ], expected = actual.slice( 0 ), bigArr = [],
actualRet, expectedRet, i;
actualRet = ve.batchSplice( actual, 1, 1, [] );
expectedRet = expected.splice( 1, 1 );
deepEqual( expectedRet, actualRet, 'removing 1 element (return value)' );
deepEqual( expected, actual, 'removing 1 element (array)' );
actualRet = ve.batchSplice( actual, 3, 2, [ 'w', 'x', 'y', 'z' ] );
expectedRet = expected.splice( 3, 2, 'w', 'x', 'y', 'z' );
deepEqual( expectedRet, actualRet, 'replacing 2 elements with 4 elements (return value)' );
deepEqual( expected, actual, 'replacing 2 elements with 4 elements (array)' );
actualRet = ve.batchSplice( actual, 0, 0, [ 'f', 'o', 'o' ] );
expectedRet = expected.splice( 0, 0, 'f', 'o', 'o' );
deepEqual( expectedRet, actualRet, 'inserting 3 elements (return value)' );
deepEqual( expected, actual, 'inserting 3 elements (array)' );
for ( i = 0; i < 2100; i++ ) {
bigArr[i] = i;
}
actualRet = ve.batchSplice( actual, 2, 3, bigArr );
expectedRet = expected.splice.apply( expected, [2, 3].concat( bigArr.slice( 0, 1050 ) ) );
expected.splice.apply( expected, [1052, 0].concat( bigArr.slice( 1050 ) ) );
deepEqual( expectedRet, actualRet, 'replacing 3 elements with 2100 elements (return value)' );
deepEqual( expected, actual, 'replacing 3 elements with 2100 elements (array)' );
} );

View file

@ -515,30 +515,33 @@
* @static
* @method
* @param {Array} arr Array to remove from and insert into. Will be modified
* @param {Number} offset Offset in arr to splice at. May be negative; see the 'index'
* parameter for Array.prototype.splice()
* @param {Number} offset Offset in arr to splice at. This may NOT be negative, unlike the
* 'index' parameter in Array.prototype.splice
* @param {Number} remove Number of elements to remove at the offset. May be zero
* @param {Array} data Array of items to insert at the offset
* @param {Array} data Array of items to insert at the offset. May not be empty if remove=0
*/
ve.batchSplice = function ( arr, offset, remove, data ) {
// We need to splice insertion in in batches, because of parameter list length limits which vary
// cross-browser - 1024 seems to be a safe batch size on all browsers
var index = 0, batchSize = 1024, toRemove = remove;
var index = 0, batchSize = 1024, toRemove = remove, spliced, removed = [];
if ( data.length === 0 ) {
// Special case: data is empty, so we're just doing a removal
// The code below won't handle that properly, so we do it here
arr.splice( offset, remove );
return;
return arr.splice( offset, remove );
}
while ( index < data.length ) {
// Call arr.splice( offset, remove, i0, i1, i2, ..., i1023 );
// Only set remove on the first call, and set it to zero on subsequent calls
arr.splice.apply(
spliced = arr.splice.apply(
arr, [index + offset, toRemove].concat( data.slice( index, index + batchSize ) )
);
if ( toRemove > 0 ) {
removed = spliced;
}
index += batchSize;
toRemove = 0;
}
return removed;
};
/**