JSL.array

From Projects Wiki

Jump to: navigation, search

Handles all the array related functions.

JLS.array Code

Contents

[edit] Arguments

array
The array on which the operation must be done.

[edit] Methods

[edit] JSL.array.map( func[, user_args ] )

This function will loop thru the array and call the given function for each element. The values returned by the user defined function will be used to create a new array/object that is returned by the function at the end. 3 arguments will be passed to the user defined function -

   - current_item - The value of the current element
   - index - The index we are currently at
   - full_array - The entire array
   - user_args - The data provided by the user, if any.

[edit] Arguments

func
The user function.
user_args
Custom data passed into the function
Optional Argument

[edit] Example

var result = JSL.array([4,10,65]).map(function(current_item) {
    return current_item+1;
});

Result will be [5, 11, 66]

var result = JSL.array([4,10,65]).map(function(current_item, x) {
    return current_item + x;
}, 5);

Result will be [9, 15, 70] - 5, the second argument of the map function is passed into the 1st argument function.

[edit] Code

// File /var/www/html/Sites/openjs/openjs.com/scripts/jslibrary/code/jsl_array.js, Line 38
function() {
    var func = arguments[0];
    func = JSL._makeFunc(func,"ele,i,all");
    var user_args = JSL._getUserArgs(arguments);
 
    var is_array = this.isList();
    var result = (is_array) ? [] : {};
 
    function _callUserFunction(index, user_args, result) {
        var return_value = func.apply(this, [this.array[index], index, this.array].concat(user_args));
        if(return_value != undefined) {
            if(is_array) result.push(return_value);
            else result[index] = return_value;
        }
        return result;
    }
 
    // I use 2 differnt methods of accessing the array elements based on what kind of data it is - yeah, I hate doing this.
    //      So why am I doing this? To get the small performance boost promised in http://batiste.dosimple.ch/blog/posts/2007-02-27-1/javascript-loop-benchmark.html
    if(is_array) { //This part is for the lists
        array_length = this.array.length;
        for(var index=0; index<array_length; index++) {
            result = _callUserFunction.apply(this, [index, user_args, result]);
        }
    } else { //Prossess the associative arrays/hashes
        for(var index in this.array) {
            if(this.array.hasOwnProperty && !this.array.hasOwnProperty(index)) continue; // Prototype.js library compatiability code.
            result = _callUserFunction.apply(this, [index, user_args, result]);
        }
    }
    this.array = result;
    return this;
}

[edit] JSL.array.each( func[, user_args ] )

This function will loop thru the array and call the given function for each element. 4 arguments will be passed to the user defined function -

   current_item - The value of the current element
   index - The index we are currently at
   full_array - The entire array
   user_args - The data provided by the user, if any, as an arrayIf the user function returns anything, the loop will end - returning that value to the calling code. 

[edit] Arguments

func
The user function.
user_args
Custom data passed into the function
Optional Argument

[edit] Example

JSL.array(document.getElementsByTagName("a")).each(function(ele) {
    ele.onclick = false; //Disable all links
});

[edit] Code

// File /var/www/html/Sites/openjs/openjs.com/scripts/jslibrary/code/jsl_array.js, Line 90
function() {
    var func = arguments[0];
    func = JSL._makeFunc(func,"ele,i,all");
    var is_array = this.isList();
 
    var user_args = JSL._getUserArgs(arguments);
 
 
    if(is_array) { //This part is for the lists
        var array_length = this.array.length;
        for(var index=0; index<array_length; index++) {
            var return_value = func.apply(this, [this.array[index], index, this.array].concat(user_args));
            if(return_value != undefined) return return_value;
        }
    } else {
        for(var index in this.array) {
            if(this.array.hasOwnProperty && !this.array.hasOwnProperty(index)) continue; // Prototype.js library compatiability code.
 
            var return_value = func.apply(this, [this.array[index], index, this.array].concat(user_args));
            if(return_value != undefined) return return_value;
        }
    }
}

[edit] JSL.array.filter( func[, user_args ] )

Function will loop thru the array and collects all the element for which the user function returned a true value. This array will be returned as a JSL.array object.

[edit] Arguments

func
The user function.
user_args
Custom data passed into the function .
Optional Argument

[edit] Example

JSL.array(["hello", "world", "virus", "worm", "virus", "trojan"]).filter(function(current_item, i, full_array, bad_word) {
    return current_item !== bad_word; //Removes all the elements that don't say 'virus'
}, "virus"); //"virus" is the bad_word

[edit] Code

// File /var/www/html/Sites/openjs/openjs.com/scripts/jslibrary/code/jsl_array.js, Line 125
function() {
    var func = arguments[0];
    func = JSL._makeFunc(func,"ele,i,all");
    var user_args = JSL._getUserArgs(arguments);
 
    this.map.apply(this, [function(ele, i, arr, user_args) {
        if(func(ele, i, arr, user_args)) return ele; //If the result of the user function is true, that element must be in the return array.
    }, user_args]);
 
    return this;
}

[edit] JSL.array.indexOf( )

Searches thru the array until the given element is found - and returns it index.

[edit] Arguments

The value that must be searched for.

[edit] Returns

-1 if searched element was not found - or it will return the index of the first found element.

[edit] Example

JSL.array([2,6,19,34]).indexOf(19); // Returns 2
JSL.array([2,6,19,34]).indexOf(17); // Returns -1
JSL.array({"a":2, "b":6, "c":19, "d":34}).indexOf(19); // Returns 'c'

[edit] Code

// File /var/www/html/Sites/openjs/openjs.com/scripts/jslibrary/code/jsl_array.js, Line 146
function(value) {
    //:TODO: indexOf(searchValue, fromIndex)
    var location = this.each(function(ele, index) {
        if(ele == value) return index;
    });
    if(location != undefined) return location;
    return -1;
}

[edit] JSL.array.reduce( func, initial_value )

Apply a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value. Taken from : http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:reduce

[edit] Arguments

func
The Function
initial_value
The Initial Value

[edit] Returns

The Result

[edit] Example

//Find the Total of all elements
JSL.array([0, 1, 2, 3]).reduce(function(a, b){ return a + b; }); // == 6

[edit] Code

// File /var/www/html/Sites/openjs/openjs.com/scripts/jslibrary/code/jsl_array.js, Line 164
function(func, initial_value) {
    var len = this.array.length;
 
    func = JSL._makeFunc(func,"a,b");
 
    //No value to return if no initial value and an empty array
    if (len == 0 && arguments.length == 1) throw new TypeError();
 
    var i = 0;
    if (arguments.length >= 2) {
        var return_value = arguments[1];
    } else {
        do {
            if (i in this.array) {
                return_value = this.array[i++];
                break;
            }
 
            //If array contains no values, no initial value to return
            if (++i >= len) throw new TypeError();
        } while (true);
    }
 
    for (; i < len; i++) {
        if (i in this.array)
            return_value = func.call(null, return_value, this.array[i], i, this.array);
    }
 
    return return_value;
}

[edit] JSL.array.grep( regexp )

Returns all the elements in the selected array that matches the provided regular expression

[edit] Arguments

regexp
The regular expression that should be matched against all the elements in the array.

[edit] Example

JSL.array(['hello', 'world', 'hot', 'water']).grep(/^w/);
//Returns ['world', 'water']

[edit] Code

// File /var/www/html/Sites/openjs/openjs.com/scripts/jslibrary/code/jsl_array.js, Line 203
function(regexp) {
    return this.filter(function(ele){return ele.match(regexp);});
}

[edit] JSL.array.getSize( )

Get the number of elements currently seleted.

[edit] Returns

length(Integer) - The size of the currently selected array

[edit] Code

// File /var/www/html/Sites/openjs/openjs.com/scripts/jslibrary/code/jsl_array.js, Line 212
function() {
    return this.array.length;
}

[edit] JSL.array.get( )

Returns the base element - in this case, an array.

[edit] Code

// File /var/www/html/Sites/openjs/openjs.com/scripts/jslibrary/code/jsl_array.js, Line 219
function() {
    return this.array;
}

[edit] JSL.array.isList( )

Checks wether the current array is an Numerical Array(List) - if so return true. Objects return false.

[edit] Returns

Boolean - true if its an numerical array(list) and false if its anything else

[edit] Code

// File /var/www/html/Sites/openjs/openjs.com/scripts/jslibrary/code/jsl_array.js, Line 227
function() {
    var arr_obj = this.array;
    return (arr_obj && (arr_obj.propertyIsEnumerable && !(arr_obj.propertyIsEnumerable('length'))) 
        && typeof arr_obj === 'object' && typeof arr_obj.length === 'number');
}
Personal tools