Catrope has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/172929

Change subject: Explicitly bypass undefined values in oo.compare()
......................................................................

Explicitly bypass undefined values in oo.compare()

{ a: 5 } was considered a subset of { a: 5, b: 3 }, but
{ a: 5, b: undefined } was not. But there is no conceptual
difference between a key being absent and a key being present
but with an undefined value, so make these two behave the same.

This only affects asymmetrical comparisons.

Change-Id: Icc6f38d29a6fe5b07ce43b7919e1d622280055e7
---
M src/core.js
M tests/unit/core.test.js
2 files changed, 26 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/oojs/core refs/changes/29/172929/1

diff --git a/src/core.js b/src/core.js
index 1d3c9e4..b53d751 100644
--- a/src/core.js
+++ b/src/core.js
@@ -214,7 +214,8 @@
  *
  * @param {Object|undefined|null} a First object to compare
  * @param {Object|undefined|null} b Second object to compare
- * @param {boolean} [asymmetrical] Whether to check only that b contains 
values from a
+ * @param {boolean} [asymmetrical] Whether to check only that a's values are 
equal to b's
+ *  (i.e. a is a subset of b)
  * @return {boolean} If the objects contain the same values as each other
  */
 oo.compare = function ( a, b, asymmetrical ) {
@@ -228,9 +229,11 @@
        b = b || {};
 
        for ( k in a ) {
-               if ( !hasOwn.call( a, k ) ) {
-                       // Support es3-shim: Without this filter, comparing [] 
to {} will be false in ES3
+               if ( !hasOwn.call( a, k ) || a[k] === undefined ) {
+                       // Support es3-shim: Without the hasOwn filter, 
comparing [] to {} will be false in ES3
                        // because the shimmed "forEach" is enumerable and 
shows up in Array but not Object.
+                       // Also ignore undefined values, because there is no 
conceptual difference between
+                       // a key that is absent and a key that is present but 
whose value is undefined.
                        continue;
                }
 
diff --git a/tests/unit/core.test.js b/tests/unit/core.test.js
index 253a58e..dc1310f 100644
--- a/tests/unit/core.test.js
+++ b/tests/unit/core.test.js
@@ -308,7 +308,7 @@
                );
        } );
 
-       QUnit.test( 'compare', 25, function ( assert ) {
+       QUnit.test( 'compare', 26, function ( assert ) {
                var x, y, z;
 
                assert.strictEqual(
@@ -420,6 +420,12 @@
                );
 
                assert.strictEqual(
+                       oo.compare( { a: 5 }, { a: 5, b: undefined } ),
+                       true,
+                       'Missing key and undefined are treated the same'
+               );
+
+               assert.strictEqual(
                        oo.compare(
                                {
                                        foo: [ true, 42 ],
@@ -518,25 +524,28 @@
                );
        } );
 
-       QUnit.test( 'compare( Object, Object, Boolean asymmetrical )', 3, 
function ( assert ) {
+       QUnit.test( 'compare( Object, Object, Boolean asymmetrical )', 4, 
function ( assert ) {
                var x, y, z;
 
                x = {
-                       foo: [ true, 42 ]
+                       foo: [ true, 42 ],
+                       baz: undefined
                };
                y = {
                        foo: [ true, 42, 10 ],
                        bar: [ {
                                x: {},
                                y: [ 'test' ]
-                       } ]
+                       } ],
+                       baz: 1701
                };
                z = {
                        foo: [ 1, 42 ],
                        bar: [ {
                                x: {},
                                y: [ 'test' ]
-                       } ]
+                       } ],
+                       baz: 1701
                };
 
                assert.strictEqual(
@@ -556,6 +565,12 @@
                        false,
                        'A subset of B with differences (asymmetrical: true)'
                );
+
+               assert.strictEqual(
+                       oo.compare( [ undefined, 'val2' ], [ 'val1', 'val2', 
'val3' ], true ),
+                       true,
+                       'A subset of B with sparse array'
+               );
        } );
 
        QUnit.test( 'copy( source )', 14, function ( assert ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/172929
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Icc6f38d29a6fe5b07ce43b7919e1d622280055e7
Gerrit-PatchSet: 1
Gerrit-Project: oojs/core
Gerrit-Branch: master
Gerrit-Owner: Catrope <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to