posted on Tuesday, March 01, 2005 4:12 PM by Andrew Scott

Blog tools for Alt. IDE's setup and some JS Native Object Expansion

A while ago read a post on Anders Norås' blog about New Year's "Cruft"! Everything must go! where he was listing what applications would honor has hard drive.  One in the List that I was very interested in was Collin Coller’s CopySourceAsHtml that enables the generation of HTML from VS.net. 

I tried to use this application but as you can see from the screen shot below I use a white on black background in vs.net. 

The result of using CopySourceAsHtml is that it directly copies the screen styles.  And I appreciate that most developers don't use white on black so I this put me off.  Also I tend to write alot of javascript and Collin's tool only works with C# (probably VB.net never tried).

Today I was reading XML Serialization - Conversion of XML Documents and Streams to Common Language Runtime Objects and Vice Versa where David Hayden had some C# that apperntly would run in Snippet Compiler, having never seen this before I decided to install it.  To my Joy it exported HTML in the same way and it also handled Javascript syntax.  As this is not going to be my primary IDE I don't mine having to use a non white on black envionment. 

 

Anyway I thought I'd celebrate this discovery by posting a small chunk of some Native Javascript Object Expansion.  That's to for example Natively a String() in Javascript doesn't have a .trim() method liek we have in C# and a host of other languages.  Using prototyping we can expand the definition of a string and by adding a method to do this for us. 

String.startsWith
String.endsWith
String.trim
String.toCharArray
String.reverse

Array.indexOf
Array.clear
Array.copy  
String.padLeft     
String.prototype.padLeft = function(cChar, nLength)
{
    if( this.length < nLength )
    {
        var nExtraLen = nLength - this.length;
        var cTemp = "";
        for( var i = 0; i < nExtraLen; i++ )
        {
            cTemp += cChar;
        }
        cTemp += this;
    }
    else{alert("String Long than pad request");}
    return cTemp;
}

String.prototype.startsWith = function(sStart)
{
    return (this.substr(0,sStart.length)==sStart);
}

String.prototype.trim = function()
{
    var b=0,e=this.length -1;
    while(this.substr(b,1) == " ") b++;
    while(this.substr(e,1) == " ") e--;
    return this.substring(b,e+1);
}

String.prototype.toCharArray = function()
{
    var arrRet = new Array();
    for(var i=0;i<this.length;i++) arrRet.push(this.substr(i,1));
    return arrRet;
}

String.prototype.reverse = function()
{
    var a = new Array();
    for(var i=0;i<this.length;i++) a.push(this.substr(i,1));
    return a.reverse().join("");
}

String.prototype.deHTML = function()
{
    return this.replace(/&lt;/g,'<' ).replace(/&gt;/g,'>' ).replace(/&amp;/g,'&' ).replace(/&lt;/g,'<' );
}
Array.prototype.indexOf = function()
{
    if(arguments.length==1)
        for(var i=0;i<this.length;i++)
            if(this[i]==arguments[0])
                return i;

    return -1;
}


Array.prototype.clear = function()
{
    return this.splice(0,this.length);
}


Array.prototype.copy = function()
{
    return this.slice(0,this.length);
}

Now playing: Alanis Morissette - Can't Not

Comments