Converting a collection to a JScript object in ASP

classic asp
This post is too old to be of any use. It has been archived.

Some Classic ASP code snippets in JScript (from the 90s!) for dealing with form data. Enumerators are collections of data that can be iterated through with methods like .moveNext etc. They are too VB-like for my taste, so I've always avoided them. But 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.

Converting A Collection To A Plain JScript Object

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
transforms a VB collection thing in a JS-friendly object
@return {Object}
/
function coll2obj( coll ) {
var enm = new Enumerator( coll )
, obj = {}
;
while( !enm.atEnd() ) {
obj[enm.item().name] = enm.item().value;
enm.moveNext();
}
return obj;
}
view raw coll2obj.asp hosted with ❤ by GitHub

Converting Request.ServerVariables To JScript Object In Classic ASP

I'm not quite sure why the function above won't work on Request.ServerVariables, but it doesn't, so here's a function for that specific collection.

/*
transforms Request.ServerVariables VB collection
in a JS-friendly object
@return {Object}
*/
function env2obj() {
var enm = new Enumerator(Request.ServerVariables)
, obj = new Object()
, 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;
}
view raw env2obj.asp hosted with ❤ by GitHub

Converting Post Variables To JScript Object In Classic ASP

The same holds for post variables...

/ post2obj
transforms Request.Form VB collection into a JS-friendly object
@return {Object}
*/
function post2obj() {
var enm = new Enumerator( Request.Form )
, obj = new Object()
, 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;
}
view raw post2obj.asp hosted with ❤ by GitHub

Converting Post Variables To JScript Object In Classic ASP

...and query string variables.

/
transforms Request.QueryString VB collection thing in a JS-friendly object
@return {Object}
/
function query2obj() {
var enm = new Enumerator( Request.QueryString )
, obj = new Object(
, 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;
}
view raw query2obj.asp hosted with ❤ by GitHub