Commit 3fe4629b 3fe4629b6bb3e0b7c6aff29f7e7580d067048fe2 by Nicolas Perriault

Merge pull request #659 from mduvall/convert-reference-checking

Change mergeObject to have option to keep references
2 parents c25c439a 258d66d0
......@@ -216,10 +216,12 @@ Checks if passed argument is an instance of native PhantomJS' ``WebPage`` object
``mergeObjects()``
-------------------------------------------------------------------------------
**Signature:** ``mergeObjects(origin, add)``
**Signature:** ``mergeObjects(origin, add[, Object opts])``
Merges two objects recursively.
Add ``opts.keepReferences`` if cloning of internal objects is not needed.
.. index:: DOM
``node()``
......
......@@ -622,14 +622,26 @@ function isPlainObject(obj) {
return (type === 'object');
}
function mergeObjectsInSlimerjs(origin, add) {
/**
* Object recursive merging utility for use in the SlimerJS environment
*
* @param Object origin the origin object
* @param Object add the object to merge data into origin
* @param Object opts optional options to be passed in
* @return Object
*/
function mergeObjectsInSlimerjs(origin, add, opts) {
"use strict";
var options = opts || {},
keepReferences = options.keepReferences;
for (var p in add) {
if (isPlainObject(add[p])) {
if (isPlainObject(origin[p])) {
origin[p] = mergeObjects(origin[p], add[p]);
} else {
origin[p] = clone(add[p]);
origin[p] = keepReferences ? add[p] : clone(add[p]);
}
} else {
origin[p] = add[p];
......@@ -643,23 +655,28 @@ function mergeObjectsInSlimerjs(origin, add) {
*
* @param Object origin the origin object
* @param Object add the object to merge data into origin
* @param Object opts optional options to be passed in
* @return Object
*/
function mergeObjects(origin, add) {
function mergeObjects(origin, add, opts) {
"use strict";
var options = opts || {},
keepReferences = options.keepReferences;
if (phantom.casperEngine === 'slimerjs') {
// Because of an issue in the module system of slimerjs (security membranes?)
// constructor is undefined.
// let's use an other algorithm
return mergeObjectsInSlimerjs(origin, add);
}
for (var p in add) {
if (add[p] && add[p].constructor === Object) {
if (origin[p] && origin[p].constructor === Object) {
origin[p] = mergeObjects(origin[p], add[p]);
} else {
origin[p] = clone(add[p]);
origin[p] = keepReferences ? add[p] : clone(add[p]);
}
} else {
origin[p] = add[p];
......
......@@ -326,7 +326,8 @@ casper.test.begin('isJsFile() tests', 5, function(test) {
});
casper.test.begin('mergeObjects() tests', 8, function(test) {
casper.test.begin('mergeObjects() tests', 10, function(test) {
/* jshint eqeqeq:false */
var testCases = [
{
obj1: {a: 1}, obj2: {b: 2}, merged: {a: 1, b: 2}
......@@ -360,13 +361,24 @@ casper.test.begin('mergeObjects() tests', 8, function(test) {
'mergeObjects() can merge objects'
);
});
var obj = {x: 1};
var obj = {x: 1},
qtruntimeobject = {foo: 'baz'};
var merged1 = utils.mergeObjects({}, {a: obj});
var merged2 = utils.mergeObjects({a: {}}, {a: obj});
merged1.a.x = 2;
test.assertEquals(obj.x, 1, 'mergeObjects() creates deep clones #1');
var merged2 = utils.mergeObjects({a: {}}, {a: obj});
merged2.a.x = 2;
test.assertEquals(obj.x, 1, 'mergeObjects() creates deep clones #2');
var refObj = {a: qtruntimeobject};
var merged3 = utils.mergeObjects({}, refObj, {keepReferences: false});
test.assertFalsy(merged3.a == refObj.a, 'disabling references should not point to same object');
var merged4 = utils.mergeObjects({}, refObj, {keepReferences: true});
test.assert(merged4.a == refObj.a, 'enabling references should point to same object');
test.done();
});
......