Javascript String trim



  • <font face="courier new,courier">function Trim(TRIM_VALUE){
    if(TRIM_VALUE.length < 1){
    return"";
    }
    TRIM_VALUE = RTrim(TRIM_VALUE);
    TRIM_VALUE = LTrim(TRIM_VALUE);
    if(TRIM_VALUE==""){
    return "";
    }
    else{
    return TRIM_VALUE;
    }
    } //End Function

    function RTrim(VALUE){
    var w_space = String.fromCharCode(32);
    var v_length = VALUE.length;
    var strTemp = "";
    if(v_length < 0){
    return"";
    }
    var iTemp = v_length -1;

    while(iTemp > -1){
    if(VALUE.charAt(iTemp) == w_space){
    }
    else{
    strTemp = VALUE.substring(0,iTemp +1);
    break;
    }
    iTemp = iTemp-1;

    } //End While
    return strTemp;

    } //End Function

    function LTrim(VALUE){
    var w_space = String.fromCharCode(32);
    if(v_length < 1){
    return"";
    }
    var v_length = VALUE.length;
    var strTemp = "";

    var iTemp = 0;

    while(iTemp < v_length){
    if(VALUE.charAt(iTemp) == w_space){
    }
    else{
    strTemp = VALUE.substring(iTemp,v_length);
    break;
    }
    iTemp = iTemp + 1;
    } //End While
    return strTemp;
    } //End Function</font>

     
    I'd like to know if the person who wrote this wrote any more code that is being used in the intranet of the whole company.

    Sure, it works, but WTF?

    (No indentation was lost in my post. There wasn't any, for starters.)

     



  • A VBScriptophile wrote this monstrosity.



  • I really love the w_space variable and how it is initialized. Very enterprisey.



  • @ammoQ said:

    I really love the w_space variable and how it is initialized. Very enterprisey.

    Pfft. I would have used:

    String.fromCharCode(" ".charCodeAt(0))

    Just to be sure you get the right charcode, see. 



  • Clearly the proper way to do this is: 

    var mktrim = function(l,r) {return function(s) {if(l) s = s.replace(/^(\s*)(.*?)(\s*)$/,function(x,a,b,c) {return (l?a:'')+b+(r?c:'');}}
    var Trim = mktrim(true,true);
    var LTrim = mktrim(true,false);
    var RTrim = mktrim(false,true);



  • It was written by an idiot. I hate when I have to work with code like this, people that put in pointless conditional statements.

    "... if the result of the function is a zero-length string, then return a zero-length string. Otherwise, return what the function sent back. And now, for my next trick..."



  • Shall I continue with my frustrations?

        <font face="courier new,courier"><%If Trim(Request("BCGI")) = "" Then%>
        <div name="tabsWorkflow" class="topTab" id="tabsWorkflow" onbeforechange="top.callFunction('DoStuff');" onTabFocus="(this.clickIdx==0)? top.blnUnlocked=true:top.blnUnlocked=false;">
      <%Else%>
        <div name="tabsWorkflow" class="topTab" id="tabsWorkflow" onbeforechange="top.callFunction('DoStuff');" onTabFocus="(this.clickIdx==0)? top.blnUnlocked=true:top.blnUnlocked=false;">
      <%End If%></font>
     

    Besides the useless If, notice the use of assigments inside a ternary operator:

        <font face="courier new,courier">(this.clickIdx==0)? top.blkUnlocked=true:top.blkUnlocked=false;</font>

    Rather than:

         <font face="courier new,courier">if (this.clickId == 0) top.blnUnlocked = true; else top.blkUnlocked = false;</font>

     Or what they probably meant:
         <font face="courier new,courier">top.blkUnlocked = (this.clickIdx == 0);</font>

    Anyway, they'd probably write it as: 

         <font face="courier new,courier">top.blkUnlocked=((this.clickIdx==0)?true:false)</font>


Log in to reply