Refactor the object keys counter function
This commit is contained in:
@@ -53,6 +53,34 @@
|
||||
</div>';
|
||||
/*jshint multistr:false */
|
||||
|
||||
$.uls = $.uls || {};
|
||||
|
||||
$.uls.utils = {};
|
||||
|
||||
/**
|
||||
* Count the number of keys in an object.
|
||||
* Works in a cross-browser way.
|
||||
* @param {Object} The object.
|
||||
*/
|
||||
$.uls.utils.objectLength = function ( obj ) {
|
||||
var count, key;
|
||||
|
||||
// Some old browsers don't support Object.keys
|
||||
if ( Object.keys ) {
|
||||
return Object.keys( obj ).length;
|
||||
}
|
||||
|
||||
count = 0;
|
||||
|
||||
for ( key in obj ) {
|
||||
if ( Object.prototype.hasOwnProperty.call( obj, key ) ) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
};
|
||||
|
||||
/**
|
||||
* ULS Public class definition
|
||||
*/
|
||||
@@ -354,26 +382,13 @@
|
||||
* @return string
|
||||
*/
|
||||
getMenuWidth: function () {
|
||||
var language,
|
||||
languagesCount = 0;
|
||||
var languagesCount;
|
||||
|
||||
if ( this.options.menuWidth ) {
|
||||
return this.options.menuWidth;
|
||||
}
|
||||
|
||||
// IE8 does not support Object.keys
|
||||
if ( Object.keys ) {
|
||||
languagesCount = Object.keys( this.options.languages ).length;
|
||||
} else {
|
||||
for ( language in this.options.languages ) {
|
||||
if ( Object.prototype.hasOwnProperty.call(
|
||||
this.options.languages,
|
||||
language
|
||||
) ) {
|
||||
languagesCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
languagesCount = $.uls.utils.objectLength( this.options.languages );
|
||||
|
||||
if ( languagesCount < 12 ) {
|
||||
return 'narrow';
|
||||
|
||||
@@ -115,8 +115,7 @@
|
||||
|
||||
render: function () {
|
||||
var $section,
|
||||
language,
|
||||
languagesCount = 0,
|
||||
languagesCount,
|
||||
lcd = this,
|
||||
regions = [],
|
||||
regionNames = {
|
||||
@@ -131,19 +130,7 @@
|
||||
PA: 'Pacific'
|
||||
};
|
||||
|
||||
// IE8 does not support Object.keys
|
||||
if ( Object.keys ) {
|
||||
languagesCount = Object.keys( this.options.languages ).length;
|
||||
} else {
|
||||
for ( language in this.options.languages ) {
|
||||
if ( Object.prototype.hasOwnProperty.call(
|
||||
this.options.languages,
|
||||
language
|
||||
) ) {
|
||||
languagesCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
languagesCount = $.uls.utils.objectLength( this.options.languages );
|
||||
|
||||
// Show the Common languages section, unless the list is very short
|
||||
if ( languagesCount > 12 ) {
|
||||
|
||||
@@ -272,4 +272,25 @@
|
||||
assert.ok( !$.uls.data.deleteLanguage( 'qqr' ), 'Deleting language qqr, which was never added, returns false.' );
|
||||
} );
|
||||
|
||||
test( '-- $.uls.utils testing', 4, function ( assert ) {
|
||||
var languages, saveObjectKeys;
|
||||
|
||||
languages = {
|
||||
mn: 'монгол',
|
||||
sah: 'саха',
|
||||
udm: 'удмурт'
|
||||
};
|
||||
|
||||
assert.strictEqual( $.uls.utils.objectLength( {} ), 0 );
|
||||
assert.strictEqual( $.uls.utils.objectLength( languages ), 3 );
|
||||
|
||||
// Simulate a browser without Object.keys
|
||||
saveObjectKeys = Object.keys;
|
||||
Object.keys = undefined;
|
||||
|
||||
assert.strictEqual( $.uls.utils.objectLength( {} ), 0 );
|
||||
assert.strictEqual( $.uls.utils.objectLength( languages ), 3 );
|
||||
|
||||
Object.keys = saveObjectKeys;
|
||||
} );
|
||||
}( jQuery ) );
|
||||
|
||||
Reference in New Issue
Block a user