//clone.js
/*global adventurejs A*/
"use strict";
/**
* A deep clone function for simple object types.
* @method adventurejs#clone
* @memberOf adventurejs
* @param {Object} source
* @returns {Object} output
*/
adventurejs.clone = function Adventurejs_clone(source) {
//this.game.log( "log", "high", "clone(, 'CopyOperations' )" );
var dest;
// Handle the 3 simple types, and null or undefined
if (null == source || "object" != typeof source) return source;
// Handle Date - not using dates
// if (source instanceof Date) {
// dest = new Date();
// dest.setTime(source.getTime());
// return dest;
// }
// Handle Array
if (source instanceof Array) {
dest = [];
for (var i = 0, len = source.length; i < len; i++) {
dest[i] = A.clone.call(this, source[i]);
}
return dest;
}
// Handle Function
if (source instanceof Function) {
var stringified = source.toString();
dest = eval("(" + stringified + ")");
return dest;
}
// Handle Object
if (source instanceof Object) {
if (source.class && adventurejs[source.class]) {
dest = new adventurejs[source.class](source.name, this.game_name);
} else {
dest = {};
}
for (var property in source) {
if (Object.prototype.hasOwnProperty.call(source, property)) {
dest[property] = A.clone.call(this, source[property]);
}
}
return dest;
}
throw new Error(
"clone.js unable to copy source because its type isn't supported."
);
};
// another cloning tool
// http://www.eslinstructor.net/jsonfn/
// might also try lodash