ObjectUtil.js revision 2d7019e19039fd083850f76e54b6b26a7b06a8f2
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek/*
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek *
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek * Copyright © 2011 ForgeRock AS. All rights reserved.
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek *
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek * The contents of this file are subject to the terms
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek * of the Common Development and Distribution License
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek * (the License). You may not use this file except in
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek * compliance with the License.
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek *
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek * You can obtain a copy of the License at
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek * http://forgerock.org/license/CDDLv1.0.html
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek * See the License for the specific language governing
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek * permission and limitations under the License.
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek *
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek * When distributing Covered Code, include this CDDL
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek * Header Notice in each file and include the License file
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek * at http://forgerock.org/license/CDDLv1.0.html
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek * If applicable, add the following below the CDDL Header,
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek * with the fields enclosed by brackets [] replaced by
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek * your own identifying information:
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek * "Portions Copyrighted [year] [name of copyright owner]"
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek */
aea1d5c0ca9bb1470759b024c8b97b6c1f577193Pavel Březina
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek/*global define*/
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozekdefine("app/util/ObjectUtil",
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek [],
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek function () {
aea1d5c0ca9bb1470759b024c8b97b6c1f577193Pavel Březina var obj = {};
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek obj.copyObject = function (o) {
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek var result, oneAttribute;
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek if (!o) {
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek return null;
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek }
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek
if (o instanceof Date) {
result = new Date();
result.setTime(o.getTime());
return result;
}
if (o instanceof Array) {
result = [];
// for (var i = 0, var len = o.length; i < len; ++i) {
// result[i] = obj.copyObject(o[i]);
// }
return result;
}
if (o instanceof Object) {
result = {};
for (oneAttribute in o) {
if (o.hasOwnProperty(oneAttribute)) {
obj.copyObject[oneAttribute] = obj.copyObject(o[oneAttribute]);
}
}
return result;
}
throw new Error("Can't copy the object");
};
obj.patchObject = function (original, patch, mode) {
var oneAttribute, result = {};
for (oneAttribute in patch) {
if (!(original.hasOwnProperty(oneAttribute) && original[oneAttribute] instanceof Array && patch[oneAttribute] instanceof Array)) {
original[oneAttribute] = patch[oneAttribute];
} //else {
// for (var i = 0, var len = patch[oneAttribute].length; i < len; ++i) {
// original[oneAttribute][i] = obj.copyObject(o[i]);
// }
//}
}
return result;
};
return obj;
});