Skip to content

Instantly share code, notes, and snippets.

@shimondoodkin
Created July 8, 2010 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shimondoodkin/467478 to your computer and use it in GitHub Desktop.
Save shimondoodkin/467478 to your computer and use it in GitHub Desktop.
// var sys = require('sys'); // enable for debugging this module
function replace(a, b)
{
if (!b) return a;
var key;
for (key in b) a[key] = b[key];
return a;
} this.replace=replace;
function add(a, b)
{
if (!b) return a;
var key;
for (key in b)
if(typeof a[key] === 'undefined' || a[key]===null)
a[key] = b[key];
return a;
} this.add=add;
function extend(a, b)
{
if (!b) return a;
var key;
for (key in b)
{
if(typeof a[key] === 'undefined')
{
if(typeof b[key] === 'object')
{
if( b[key] instanceof Array ) // http://javascript.crockford.com/remedial.html
a[key] = extend([], b[key]);
else if(b[key]===null)
a[key] = null;
else
a[key] = extend({}, b[key]);
}
else
a[key] = b[key];
}
else if(typeof a[key] === 'object' && a[key] !== null)
a[key] = extend(a[key], b[key]);
else
a[key] = b[key];
}
return a;
} this.extend=extend;
function extenduptolevel(a, b, levels)
{
if (!b) return a;
var key;
for (key in b)
{
if(typeof a[key] === 'undefined')
{
if(typeof b[key] === 'object' && levels>-1)
{
if( b[key] instanceof Array ) // http://javascript.crockford.com/remedial.html
a[key] = extend([], b[key],levels-1);
else if(b[key]===null)
a[key] = null;
else
a[key] = extend({}, b[key],levels-1);
}
else
a[key] = b[key];
}
else if(typeof a[key] === 'object' && a[key] !== null && levels>-1)
a[key] = extend(a[key], b[key],levels-1);
else
a[key] = b[key];
}
return a;
} this.extenduptolevel=extenduptolevel;
function clone(obj)
{
if (typeof obj === 'object')
{
if (obj ===null ) return null;
if (obj instanceof Array ) return extend([], obj);
return extend({}, obj);
}
return obj;
} this.clone=clone;
function cloneextend(cloneobj,exteddata)
{
return extend(clone(cloneobj),exteddata);
} this.cloneextend=cloneextend;
function cloneuptolevel(obj,level) // clone only numlevels levels other levels leave references
{
if (typeof obj === 'object')
{
if (obj ===null ) return null;
if (obj instanceof Array ) return extenduptolevel([], obj,level);
return extenduptolevel({}, obj,level);
}
return obj;
} this.cloneuptolevel=cloneuptolevel;
//main_model.prep_subitems[operation] is an object with circular references inside
var arrselections=_.clone(main_model.prep_subitems[operation])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment