From 63f8d7b9e5d4946dfe8a3f2564a9f1db25e28755 Mon Sep 17 00:00:00 2001 From: Ori Livneh Date: Mon, 28 Oct 2013 15:58:02 -0700 Subject: [PATCH] mw.Map: add ability to map over an existing object other than 'window' I don't love the fact that mw.Map features so prominently in mediawiki.js, but since it's there to stay it might as well be useful for a wider range of use cases. This patch makes it possible to pass an existing value-bearing object to be mapped over to the Map constructor. The effect is the same as constructing the object and then overriding its 'values' attribute. But doing that feels like you're sneaking around the back to hack the API. Making it part of the official API makes it OK to do. Change-Id: Id9f8d9569de8889fc3ffa24a6e7f4afca1aeabee --- resources/mediawiki/mediawiki.js | 8 ++++---- tests/qunit/suites/resources/mediawiki/mediawiki.test.js | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/resources/mediawiki/mediawiki.js b/resources/mediawiki/mediawiki.js index 03248ba740..28fdcc48a5 100644 --- a/resources/mediawiki/mediawiki.js +++ b/resources/mediawiki/mediawiki.js @@ -74,11 +74,11 @@ var mw = ( function ( $, undefined ) { * @class mw.Map * * @constructor - * @param {boolean} [global=false] Whether to store the values in the global window - * object or a exclusively in the object property 'values'. + * @param {Object|boolean} [values] Value-bearing object to map, or boolean + * true to map over the global object. Defaults to an empty object. */ - function Map( global ) { - this.values = global === true ? window : {}; + function Map( values ) { + this.values = values === true ? window : ( values || {} ); return this; } diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.test.js index 502b55b7bd..3ce52e34dd 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.test.js @@ -43,7 +43,7 @@ assert.strictEqual( window.mw, window.mediaWiki, 'mw alias to mediaWiki' ); } ); - QUnit.test( 'mw.Map', 27, function ( assert ) { + QUnit.test( 'mw.Map', 28, function ( assert ) { var arry, conf, funky, globalConf, nummy, someValues; conf = new mw.Map(); @@ -102,6 +102,8 @@ 'lorem': 'ipsum' }, 'Map.get returns multiple values correctly as an object' ); + assert.deepEqual( conf, new mw.Map( conf.values ), 'new mw.Map maps over existing values-bearing object' ); + assert.deepEqual( conf.get( ['foo', 'notExist'] ), { 'foo': 'bar', 'notExist': null -- 2.20.1