Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts

    Array Functionality

    An old script I recently found reasons to bring to life again...
    Code:
    /*\
     * Array Functionality
     * script: array.js
     * for: browsers not implementing the full array functionality of ECMA262-3
     * by: liorean <http://liorean.web-graphics.com/>
     * thanks to: beetle
    \*/
    
    if(typeof Array.prototype.copy=='undefined')
        Array.prototype.copy=function(a){
            var
                i=0,
                b=[];
            for(i;i<this.length;i++)
                b[i]=(typeof this[i].copy!='undefined')?
                    this[i].copy():
                    this[i];
            return b
        };
    
    if(typeof Array.prototype.concat=='undefined')
        Array.prototype.concat=function(a){
            var
                i=0,
                b=this.copy();
            for(i;i<a.length;i++)
                b[b.length]=a[i];
            return b
        };
        
    if(typeof Array.prototype.pop=='undefined')
        Array.prototype.pop=function(){
            var
                b=this[this.length-1];
            this.length--;
            return b
        };
    
    if(typeof Array.prototype.push=='undefined')
        Array.prototype.push=function(){
            var
                i=0,
                b=this.length,
                a=arguments;
            for(i;i<a.length;i++)
                this[b+i]=a[i];
            return this.length
        };
    
    if(typeof Array.prototype.shift=='undefined')
        Array.prototype.shift=function(){
            var
                i=0,
                b=this[0];
            for(i;i<this.length-1;i++)
                this[i]=this[i+1];
            this.length--;
            return b
        };
    
    if(typeof Array.prototype.slice=='undefined')
        Array.prototype.slice=function(a,c){
            var
                i=0,
                b,
                d=[];
            if(!c)
                c=this.length;
            if(c<0)
                c=this.length+c;
            if(a<0)
                a=this.length-a;
            if(c<a){
                b=a;
                a=c;
                c=b
            }
            for(i;i<c-a;i++)
                d[i]=this[a+i];
            return d
        };
    
    if(typeof Array.prototype.splice=='undefined')
        Array.prototype.splice=function(a,c){
            var
                i=0,
                e=arguments,
                d=this.copy(),
                f=a;
            if(!c)
                c=this.length-a;
            for(i;i<e.length-2;i++)
                this[a+i]=e[i+2];
            for(a;a<this.length-c;a++)
                this[a+e.length-2]=d[a-c];
            this.length-=c-e.length+2;
            return d.slice(f,f+c)
        };
    
    if(typeof Array.prototype.unshift=='undefined')
        Array.prototype.unshift=function(a){
            var
                b;
            this.reverse();
            b=this.push(a);
            this.reverse();
            return b
        };
    Last edited by liorean; 05-20-2003 at 05:16 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nice

    I'd probably want the copy() method to handle multiple dimensional arrays
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Sorry, but the only way to do that requires ECMA262-3 compliancy. This means older browasers like ie5.0 are screwed. (If I remember correctly, instanceof operator is an ECMA262-3 feature.)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What's wrong with doing it this way?
    Code:
    Array.prototype.copy = function()
    {
    	var
    		i=0,
    		b=[];
    	for(i;i<this.length;i++) {
    		b[i] = ( typeof this[i].copy != 'undefined' )?
    			this[i].copy():
    			this[i];
    	}
    	return b;
    };
    Besides, according to this page, instanceof was introduced in IE5.0 (JScript5.0) -- and this page says it was implemented in Javascript 1.4 - but don't get me lying as to which browsers support 1.4.
    Last edited by beetle; 05-20-2003 at 04:32 PM.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Oh, great, more version confusion (See my discussion with Brothercake regarding the in operator in <http://codingforums.com/showthread.p...on+in+document>). In fact there's no browser supporting JavaScript 1.4, only the ONE server platform. NN4 used JavaScript 1.2 and 1.3, never 1.4. Oh, and the Sourceforge project that is based on the Netscape JavaScript engine, whatever it's name is.

    Mozilla has a slightly rewritten version of the 1.4 engine that is numbered 1.5.

    I'm a bit surprised that IE5 has it, but then again, Microsoft was beginning to gain on Netscape at that time.



    Well, your version has better support anyway. I'll update the script.

    (Lovely use of recursion, even if it isn't tail recursion, by the way.)
    Last edited by liorean; 05-20-2003 at 05:16 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here are two methods I had to add to Array:
    Code:
     
    Array.prototype.swap = function(index1,index2)
      { var temp = this[index1];
        this[index1] = this[index2];
        this[index2] = temp;
        return;
      }
    
    Array.prototype.shuffle = function()
      { for(var i=0; i<this.length; i++)
          { ind1 = Math.floor(Math.random()*this.length);
            ind2 = Math.floor(Math.random()*this.length);
            this.swap(ind1,ind2);
          }
        return;
      }
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #8
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My Array extensions:
    Code:
    Array.prototype.insert = function(newValue,position) {
        if (position && position>-1)
            this.splice(position,0,newValue);
        else
            this.push(newValue);
    }
    
    Array.prototype.insertBefore = function(newValue,beforeWhat) {
        targetIndex = this.locate(beforeWhat);
        if (targetIndex == -1)
            this.push(newValue);
        else
            this.insert(newValue,targetIndex);
    }
    
    Array.prototype.insertAfter = function(newValue,afterWhat) {
        targetIndex = this.locate(afterWhat);
        if (targetIndex == -1)
            this.push(newValue);
        else
            this.insert(newValue,targetIndex+1);
    }
    
    Array.prototype.dele = function(toDelete) {
        var tmp = this.splice(this.locate(toDelete),1);
    }
    
    Array.prototype.sum = function() {
        var i,mySum=0;
        for (i=this.length; i>0; i--)
            mySum += parseInt(this[i-1],10);
        return mySum;
    }
    
    Array.prototype.avg = function() {
        var s = this.sum();
        return s/this.length;
    }
    
    Array.prototype.max = function() {
        var m = -1;
        if (typeof this[0] == "number") m = this[0];
        for (i=this.length; i>0; i--) {
            if (typeof this[i] == "number")
                m = Math.max(m,this[i]);
        }
        return m;
    }
    
    Array.prototype.min = function() {
        var m = -1;
        if (typeof this[0] == "number") m = this[0];
        for (i=this.length; i>0; i--) {
            if (typeof this[i] == "number")
                m = Math.min(m,this[i]);
        }
        return m;
    }
    
    Array.prototype.search = function(searchFor) {
        var i=0;
        var j=0;
        var results=Array(0);
        var start = 0;
        found = -1;
        for (i=0; i<this.length; i++) {
            found = this.locate(searchFor,start);
            if (found>-1) {
                results.push(this[found]);
                start = found+1;
            }
        }
        return results;
    }
    
    Array.prototype.locate = function(what, start){
        var f = 0;
        if (start)
            startWhere = start 
        else
            startWhere = 0;
        for(f=startWhere; f<this.length; f++){
            if(this[f].toString().substr(0,what.length) == what.toString())
                return f;
        }
        return -1;
    }

  • #9
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Opera 6 and 7 support JavaScript 1.4... in case anyone cares.

    Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.

  • #10
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    opera 6 only supports a limited subset
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #11
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Originally posted by Skyzyx
    Opera 6 and 7 support JavaScript 1.4... in case anyone cares.
    Not really. They support about the same level of JavaScript as 1.4, but it's only their own approximation. I know a few details of 1.4 that no browser today supports.
    Last edited by liorean; 11-24-2003 at 05:17 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •