Catrope has uploaded a new change for review.
https://gerrit.wikimedia.org/r/172940
Change subject: Replace ve.getProp(), ve.setProp() and ve.isInstanceOfAny()
with OO aliases
......................................................................
Replace ve.getProp(), ve.setProp() and ve.isInstanceOfAny() with OO aliases
Were moved to OOjs in If99eb7732e
Change-Id: Iec8f1b346106f25f48b16399e42d0a8253c2a206
---
M src/ve.utils.js
M tests/ve.test.js
2 files changed, 20 insertions(+), 178 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor
refs/changes/40/172940/1
diff --git a/src/ve.utils.js b/src/ve.utils.js
index 8c88792..a57d3bf 100644
--- a/src/ve.utils.js
+++ b/src/ve.utils.js
@@ -9,22 +9,22 @@
*/
/**
- * Checks if an object is an instance of one or more classes.
- *
- * @param {Object} subject Object to check
- * @param {Function[]} classes Classes to compare with
- * @returns {boolean} Object inherits from one or more of the classes
+ * @method
+ * @inheritdoc OO#instanceOfAny
*/
-ve.isInstanceOfAny = function ( subject, classes ) {
- var i = classes.length;
+ve.instanceOfAny = OO.instanceOfAny;
- while ( classes[--i] ) {
- if ( subject instanceof classes[i] ) {
- return true;
- }
- }
- return false;
-};
+/**
+ * @method
+ * @inheritdoc OO#getProp
+ */
+ve.getProp = OO.getProp;
+
+/**
+ * @method
+ * @inheritdoc OO#setProp
+ */
+ve.setProp = OO.setProp;
/**
* @method
@@ -263,62 +263,6 @@
*/
ve.insertIntoArray = function ( dst, offset, src ) {
ve.batchSplice( dst, offset, 0, src );
-};
-
-/**
- * Get a deeply nested property of an object using variadic arguments,
protecting against
- * undefined property errors.
- *
- * `quux = getProp( obj, 'foo', 'bar', 'baz' );` is equivalent to `quux =
obj.foo.bar.baz;`
- * except that the former protects against JS errors if one of the
intermediate properties
- * is undefined. Instead of throwing an error, this function will return
undefined in
- * that case.
- *
- * @param {Object} obj
- * @param {Mixed...} [keys]
- * @returns obj[arguments[1]][arguments[2]].... or undefined
- */
-ve.getProp = function ( obj ) {
- var i, retval = obj;
- for ( i = 1; i < arguments.length; i++ ) {
- if ( retval === undefined || retval === null ) {
- // Trying to access a property of undefined or null
causes an error
- return undefined;
- }
- retval = retval[arguments[i]];
- }
- return retval;
-};
-
-/**
- * Set a deeply nested property of an object using variadic arguments,
protecting against
- * undefined property errors.
- *
- * `ve.setProp( obj, 'foo', 'bar', 'baz' );` is equivalent to `obj.foo.bar =
baz;` except that
- * the former protects against JS errors if one of the intermediate properties
is
- * undefined. Instead of throwing an error, undefined intermediate properties
will be
- * initialized to an empty object. If an intermediate property is null, or if
obj itself
- * is undefined or null, this function will silently abort.
- *
- * @param {Object} obj
- * @param {Mixed...} [keys]
- * @param {Mixed} [value]
- */
-ve.setProp = function ( obj ) {
- var i, prop = obj;
- if ( Object( obj ) !== obj ) {
- return;
- }
- for ( i = 1; i < arguments.length - 2; i++ ) {
- if ( prop[arguments[i]] === undefined ) {
- prop[arguments[i]] = {};
- }
- if ( prop[arguments[i]] === null || typeof prop[arguments[i]]
!== 'object' ) {
- return;
- }
- prop = prop[arguments[i]];
- }
- prop[arguments[arguments.length - 2]] = arguments[arguments.length - 1];
};
/**
diff --git a/tests/ve.test.js b/tests/ve.test.js
index e6b489d..97caa27 100644
--- a/tests/ve.test.js
+++ b/tests/ve.test.js
@@ -8,6 +8,12 @@
/* Tests */
+// ve.instanceOfAny: Tested upstream (OOJS)
+
+// ve.getProp: Tested upstream (OOJS)
+
+// ve.setProp: Tested upstream (OOJS)
+
// ve.cloneObject: Tested upstream (OOJS)
// ve.getObjectValues: Tested upstream (OOJS)
@@ -109,114 +115,6 @@
'tag with two attributes'
);
} );
-
-( function () {
- var plainObj, funcObj, arrObj;
- plainObj = {
- foo: 3,
- bar: {
- baz: null,
- quux: {
- whee: 'yay'
- }
- }
- };
- funcObj = function abc( d ) { return d; };
- funcObj.foo = 3;
- funcObj.bar = {
- baz: null,
- quux: {
- whee: 'yay'
- }
- };
- arrObj = ['a', 'b', 'c'];
- arrObj.foo = 3;
- arrObj.bar = {
- baz: null,
- quux: {
- whee: 'yay'
- }
- };
-
- $.each( {
- Object: plainObj,
- Function: funcObj,
- Array: arrObj
- }, function ( type, obj ) {
-
- QUnit.test( 'getProp( ' + type + ' )', 9, function ( assert ) {
- assert.deepEqual(
- ve.getProp( obj, 'foo' ),
- 3,
- 'single key'
- );
- assert.deepEqual(
- ve.getProp( obj, 'bar' ),
- { baz: null, quux: { whee: 'yay' } },
- 'single key, returns object'
- );
- assert.deepEqual(
- ve.getProp( obj, 'bar', 'baz' ),
- null,
- 'two keys, returns null'
- );
- assert.deepEqual(
- ve.getProp( obj, 'bar', 'quux', 'whee' ),
- 'yay',
- 'three keys'
- );
- assert.deepEqual(
- ve.getProp( obj, 'x' ),
- undefined,
- 'missing property returns undefined'
- );
- assert.deepEqual(
- ve.getProp( obj, 'foo', 'bar' ),
- undefined,
- 'missing 2nd-level property returns undefined'
- );
- assert.deepEqual(
- ve.getProp( obj, 'foo', 'bar', 'baz', 'quux',
'whee' ),
- undefined,
- 'multiple missing properties don\'t cause an
error'
- );
- assert.deepEqual(
- ve.getProp( obj, 'bar', 'baz', 'quux' ),
- undefined,
- 'accessing property of null returns undefined,
doesn\'t cause an error'
- );
- assert.deepEqual(
- ve.getProp( obj, 'bar', 'baz', 'quux', 'whee',
'yay' ),
- undefined,
- 'accessing multiple properties of null'
- );
- } );
-
- QUnit.test( 'setProp( ' + type + ' )', 7, function ( assert ) {
- ve.setProp( obj, 'foo', 4 );
- assert.deepEqual( 4, obj.foo, 'setting an existing key
with depth 1' );
-
- ve.setProp( obj, 'test', 'TEST' );
- assert.deepEqual( 'TEST', obj.test, 'setting a new key
with depth 1' );
-
- ve.setProp( obj, 'bar', 'quux', 'whee', 'YAY' );
- assert.deepEqual( 'YAY', obj.bar.quux.whee, 'setting an
existing key with depth 3' );
-
- ve.setProp( obj, 'bar', 'a', 'b', 'c' );
- assert.deepEqual( 'c', obj.bar.a.b, 'setting two new
keys within an existing key' );
-
- ve.setProp( obj, 'a', 'b', 'c', 'd', 'e', 'f' );
- assert.deepEqual( 'f', obj.a.b.c.d.e, 'setting new keys
with depth 5' );
-
- ve.setProp( obj, 'bar', 'baz', 'whee', 'wheee',
'wheeee' );
- assert.deepEqual( null, obj.bar.baz, 'descending into
null fails silently' );
-
- ve.setProp( obj, 'foo', 'bar', 'baz', 5 );
- assert.deepEqual( undefined, obj.foo.bar, 'descending
into a non-object fails silently' );
- } );
- } );
-
-}() );
QUnit.test( 'batchSplice', 8, function ( assert ) {
var actual = [ 'a', 'b', 'c', 'd', 'e' ], expected = actual.slice( 0 ),
bigArr = [],
--
To view, visit https://gerrit.wikimedia.org/r/172940
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Iec8f1b346106f25f48b16399e42d0a8253c2a206
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Catrope <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits