ext-utils/object.js

92 lines
1.3 KiB
JavaScript

var cloneObj = function( o )
{
if ( o == null || typeof o != "object" ) return o;
var clone = {};
if ( o instanceof Date )
{
clone = new Date();
clone.setTime( o.getTime() );
return clone;
}
else if ( o instanceof Array )
{
clone = [];
for ( var i = 0, l = o.length; i < l; i ++ )
{
clone[i] = cloneObj( o[i] );
}
return clone;
}
else if ( o instanceof RegExp )
{
clone = new RegExp( o );
return clone;
}
else if ( o instanceof Object )
{
for ( var p in o )
{
o.hasOwnProperty( p )
&& ( clone[ p ] = cloneObj( o[ p ] ) );
}
return clone;
}
throw new Error( "Can't clone object: " + o );
};
var referenceObj = function()
{
var l = arguments.length;
if( l == 0 ) return null;
var ref = arguments[0];
if( l == 1 ) return ref;
var refd = {};
for( var i = 1; i < l; i ++ )
{
var prop = arguments[i];
refd[ prop ] = ref[ prop ];
}
return refd;
};
var LStack = function() {
this._first = null;
};
LStack.prototype.add = function( obj )
{
var p = this._first;
this._first = { "o": obj, "n": p };
};
LStack.prototype.pop = function()
{
var g = this._first;
this._first = g.n;
return g.o;
};
LStack.prototype.curr = function() {
return this._first.o;
};
LStack.prototype.empty = function() { return !this._first; }
module.exports = {
clone: cloneObj
, refObj: referenceObj
, LStack: LStack
};