Krinkle has uploaded a new change for review.
https://gerrit.wikimedia.org/r/189508
Change subject: ve.utils: Cover insertIntoArray with unit test
......................................................................
ve.utils: Cover insertIntoArray with unit test
* Cover insertIntoArray() with tests.
* Move insertIntoArray() definition to below batchSplice().
* Write function documentation for insertIntoArray().
Change-Id: Ida065a4ab4252e0eabc2245ecdb8dbb49a56ef41
---
M src/ve.utils.js
M tests/ve.test.js
2 files changed, 40 insertions(+), 22 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor
refs/changes/08/189508/1
diff --git a/src/ve.utils.js b/src/ve.utils.js
index 74b3d78..3736c3a 100644
--- a/src/ve.utils.js
+++ b/src/ve.utils.js
@@ -148,27 +148,26 @@
*
* Includes a replacement for broken implementation of
Array.prototype.splice() found in Opera 12.
*
- * @param {Array|ve.dm.BranchNode} arr Object supporting .splice() to remove
from and insert into. Will be modified
+ * @param {Array|ve.dm.BranchNode} arr Target object (must have `splice`
method, object will be modified)
* @param {number} offset Offset in arr to splice at. This may NOT be
negative, unlike the
- * 'index' parameter in Array#splice
+ * 'index' parameter in Array#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. May not be
empty if remove=0
- * @returns {Array} Array of items removed
+ * @param {Array} data Array of items to insert at the offset. Must be
non-empty if remove=0
+ * @return {Array} Array of items removed
*/
ve.batchSplice = ( function () {
var arraySplice;
- // This yields 'true' on Opera 12.15.
- function isSpliceBroken() {
- var n = 256, a = [];
+ // This yields 'false' on Opera 12.15.
+ function spliceWorks() {
+ var n = 256,
+ a = [];
a[n] = 'a';
-
a.splice( n + 1, 0, 'b' );
-
- return a[n] !== 'a';
+ return a[n] === 'a';
}
- if ( !isSpliceBroken() ) {
+ if ( spliceWorks() ) {
arraySplice = Array.prototype.splice;
} else {
// Standard Array.prototype.splice() function implemented using
.slice() and .push().
@@ -222,6 +221,20 @@
}() );
/**
+ * Insert one array into another.
+ *
+ * Shortcut for `ve.batchSplice( dst, offset, 0, src )`.
+ *
+ * @see #batchSplice
+ * @param {Array|ve.dm.BranchNode} arr Target object (must have `splice`
method)
+ * @param {number} offset Offset in arr where items will be inserted
+ * @param {Array} data Items to insert at offset
+ */
+ve.insertIntoArray = function ( dst, offset, src ) {
+ ve.batchSplice( dst, offset, 0, src );
+};
+
+/**
* Push one array into another.
*
* This is the equivalent of arr.push( d1, d2, d3, ... ) except that arguments
are
@@ -243,17 +256,6 @@
index += batchSize;
}
return length;
-};
-
-/**
- * Insert one array into another.
- *
- * This just a shortcut for `ve.batchSplice( dst, offset, 0, src )`.
- *
- * @see #batchSplice
- */
-ve.insertIntoArray = function ( dst, offset, src ) {
- ve.batchSplice( dst, offset, 0, src );
};
/**
diff --git a/tests/ve.test.js b/tests/ve.test.js
index f411252..117e97c 100644
--- a/tests/ve.test.js
+++ b/tests/ve.test.js
@@ -229,6 +229,22 @@
assert.deepEqual( expected, actual, 'replacing 3 elements with 2100
elements (array)' );
} );
+QUnit.test( 'insertIntoArray', 3, function ( assert ) {
+ var target;
+
+ target = [ 'a', 'b', 'c' ];
+ ve.insertIntoArray( target, 0, [ 'x', 'y' ] );
+ assert.deepEqual( target, [ 'x', 'y', 'a', 'b', 'c' ], 'insert at
start' );
+
+ target = [ 'a', 'b', 'c' ];
+ ve.insertIntoArray( target, 2, [ 'x', 'y' ] );
+ assert.deepEqual( target, [ 'a', 'b', 'x', 'y', 'c' ], 'insert into the
middle' );
+
+ target = [ 'a', 'b', 'c' ];
+ ve.insertIntoArray( target, 10, [ 'x', 'y' ] );
+ assert.deepEqual( target, [ 'a', 'b', 'c', 'x', 'y' ], 'insert beyond
end' );
+} );
+
QUnit.test( 'createDocumentFromHtml', function ( assert ) {
var key, doc, expectedHead, expectedBody,
cases = [
--
To view, visit https://gerrit.wikimedia.org/r/189508
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ida065a4ab4252e0eabc2245ecdb8dbb49a56ef41
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Krinkle <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits