Converting a collection to a JScript object in ASP

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.

Converting A Collection To A Plain JScript Object Conversion Functions In Classic ASP.

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
*
*/
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;
}

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.
/*  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;
}

Converting Post Variables To JScript Object In Classic ASP.

The same holds for post variables…
/*  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;
}

Converting Post Variables To JScript Object In Classic ASP.

…and query string variables.
/*  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;
}