Converting a collection to a JScript object in ASP

By fritz

Enumerators are collections of data that can be iterated through. Although useful, they are not compliant with ECMAScript standards, and therefore I prefer not to use them. Some objects in the ASP environment however, such as Request.Form, are provided as Enumerators, and need to be converted them to standard JScript objects. Here's a couple of routines to do so.

I keep all the functions below in a generic include, coll2obj.asp, and use it across projects.

This is a generic collection-to-object conversion, which doesn't seem to work on some built-in collections.

/*	coll2obj
 *	ansforms a VB collection thing in a JS-friendly object
 *
 */
function coll2obj( coll )
{
	var enm	= new Enumerator( coll );
	var obj	= new Object();
	while( !enm.atEnd() )
	{
		obj[enm.item().name]	= enm.item().value;
		enm.moveNext();
	}
	return obj;
}

The function below converts the Request.ServerVariables collection into a simple JSCript file object.

/*	env2obj
 *	transforms Request.ServerVariables VB collection thing in a JS-friendly object
 *
 */
function env2obj()
{
	var enm	= new Enumerator(Request.ServerVariables);
	var obj	= new Object()
	var ei, en;
	while(!enm.atEnd())
	{
		ei	= enm.item();
		if (Request.ServerVariables.Item( ei ).Count > 1)
		{
			obj[ei]	= new Array();
			en	= new Enumerator(Request.ServerVariables( ei ))
			while( !en.atEnd() )
			{
				obj[ei][ obj[ei].length ]	= en.item();
				en.moveNext();
			}
			en	= null;
		}
		else
		{
			obj[ei]	= new String(Request.ServerVariables(ei));
		}
		enm.moveNext();
	}
	return obj;
}

This converts post variables to a JScript object.

/*	post2obj
 *	transforms Request.Form VB collection thing in a JS-friendly object
 *
 */
function post2obj()
{
	var enm	= new Enumerator( Request.Form );
	var obj	= new Object()
	var ei, en;
	while( !enm.atEnd() )
	{
		ei	= enm.item();
		if(  Request.Form.Item( ei ).Count > 1 )
		{
			obj[ei]	= new Array();
			en	= new Enumerator(Request.Form( ei ));
			while( !en.atEnd() )
			{
				obj[ei][ obj[ei].length ]	= en.item();
				en.moveNext();
			}
			en	= null;
		}
		else
		{
			obj[ei]	= new String(Request.Form( ei ) );
			obj[ei]	= obj[ei].replace( /^s+|s+$/, '' );
		}
		enm.moveNext();
	}
	return obj;
}

This function converts the query string variables collection into a JScript object.

/*	query2obj
 *	transforms Request.QueryString VB collection thing in a JS-friendly object
 *
 */
function query2obj()
{
	var enm	= new Enumerator( Request.QueryString );
	var obj	= new Object()
	var ei, en;
	while( !enm.atEnd() )
	{
		ei	= enm.item();
		if(  Request.QueryString.Item( ei ).Count > 1 )
		{
			obj[ei]	= new Array();
			en	= new Enumerator(Request.QueryString( ei ));
			while( !en.atEnd() )
			{
				obj[ei][ obj[ei].length ]	= en.item();
				en.moveNext();
			}
			en	= null;
		}
		else
		{
			obj[ei]	= new String(Request.QueryString( ei ) );
			obj[ei]	= obj[ei].replace( /^s+|s+$/, '' );
		}
		enm.moveNext();
	}
	return obj;
}

Leave a Reply