allow deep copies of objects to optionally copy object properties in alphabetical order. This is useful to guarantee the order in which they would be serialized as json (which may depend on the order in which properties are added)

This commit is contained in:
Dan Mills 2008-07-16 19:36:06 -07:00
parent 610f300600
commit f1bea5c26d

View File

@ -183,7 +183,7 @@ let Utils = {
return true;
},
deepCopy: function Weave_deepCopy(thing) {
deepCopy: function Weave_deepCopy(thing, sort) {
if (typeof(thing) != "object" || thing == null)
return thing;
let ret;
@ -191,12 +191,14 @@ let Utils = {
if ("Array" == thing.constructor.name) {
ret = [];
for (let i = 0; i < thing.length; i++)
ret.push(Utils.deepCopy(thing[i]));
ret.push(Utils.deepCopy(thing[i]), sort);
} else {
ret = {};
for (let key in thing)
ret[key] = Utils.deepCopy(thing[key]);
let props = [p for (p in thing)];
if (sort)
props = props.sort();
props.forEach(function(k) ret[k] = Utils.deepCopy(thing[k], sort));
}
return ret;