Bug 848137 - Begin break out of Utils.js into a ContentUtil.jsm module. r=mbrubeck

This commit is contained in:
Sam Foster 2013-08-23 13:58:55 -07:00
parent c7b9f2067f
commit e5aa0369f2
5 changed files with 63 additions and 42 deletions

View File

@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Components.utils.import("resource:///modules/ContentUtil.jsm");
let Util = {
/*
* General purpose utilities
@ -37,44 +39,6 @@ let Util = {
Services.io.offline = false;
},
// Pass several objects in and it will combine them all into the first object and return it.
// NOTE: Deep copy is not supported
extend: function extend() {
// copy reference to target object
let target = arguments[0] || {};
let length = arguments.length;
if (length === 1) {
return target;
}
// Handle case when target is a string or something
if (typeof target != "object" && typeof target != "function") {
target = {};
}
for (let i = 1; i < length; i++) {
// Only deal with non-null/undefined values
let options = arguments[i];
if (options != null) {
// Extend the base object
for (let name in options) {
let copy = options[name];
// Prevent never-ending loop
if (target === copy)
continue;
if (copy !== undefined)
target[name] = copy;
}
}
}
// Return the modified object
return target;
},
/*
* Timing utilties
*/
@ -540,3 +504,11 @@ Util.Timeout.prototype = {
}
};
// Mixin the ContentUtil module exports
{
for (let name in ContentUtil) {
let copy = ContentUtil[name];
if (copy !== undefined)
Util[name] = copy;
}
}

View File

@ -1,10 +1,10 @@
"use strict";
load('Util.js');
load("Util.js");
function run_test() {
do_print("Testing Util.extend");
do_print("Check if function is defined");
do_check_true(!!Util.extend);
@ -14,7 +14,7 @@ function run_test() {
let nullRes = Util.extend(null);
do_check_true(nullRes && typeof nullRes == "object");
do_print("Simple extend");
let simpleExtend = {a: 1, b: 2};
let simpleExtendResult = Util.extend(simpleExtend, {b: 3, c: 4});

View File

@ -1,5 +1,6 @@
[DEFAULT]
head =
head =
tail =
firefox-appdir = metro
[test_util_extend.js]

View File

@ -0,0 +1,47 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
this.EXPORTED_SYMBOLS = ["ContentUtil"];
this.ContentUtil = {
// Pass several objects in and it will combine them all into the first object and return it.
// NOTE: Deep copy is not supported
extend: function extend() {
// copy reference to target object
let target = arguments[0] || {};
let length = arguments.length;
if (length === 1) {
return target;
}
// Handle case when target is a string or something
if (typeof target != "object" && typeof target != "function") {
target = {};
}
for (let i = 1; i < length; i++) {
// Only deal with non-null/undefined values
let options = arguments[i];
if (options != null) {
// Extend the base object
for (let name in options) {
let copy = options[name];
// Prevent never-ending loop
if (target === copy)
continue;
if (copy !== undefined)
target[name] = copy;
}
}
}
// Return the modified object
return target;
}
};

View File

@ -5,6 +5,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
EXTRA_JS_MODULES += [
'ContentUtil.jsm',
'CrossSlide.jsm',
'View.jsm',
'colorUtils.jsm',