/** * Class : JSL.array * Handles all the array related functions. * Argument: arr - The array on which the operation must be done. */ (function() { /// Constructor function _array_init(arr) { this.array = arr; //Get all the array function in the 'this' element itself - stuff like map(), each for(var i in this.array) { this[i] = this.array[i]; } this.length = arr.length; return this; } _array_init.prototype = { /** * 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. * * Argument: * func - The user function. * user_args - Custom data passed into the function [OPTIONAL] * 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. */ "map" : 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= 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; }, /** * Returns all the elements in the selected array that matches the provided regular expression * Argument: * regexp - The regular expression that should be matched against all the elements in the array. * Example: * JSL.array(['hello', 'world', 'hot', 'water']).grep(/^w/); * //Returns ['world', 'water'] */ "grep": function(regexp) { return this.filter(function(ele){return ele.match(regexp);}); }, /** * Get the number of elements currently seleted. * Return: * length(Integer) - The size of the currently selected array */ "getSize": function() { return this.array.length; }, /** * Returns the base element - in this case, an array. */ "get" : function() { return this.array; }, /** * Checks wether the current array is an Numerical Array(List) - if so return true. Objects return false. * Return: Boolean - true if its an numerical array(list) and false if its anything else */ "isList": 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'); } } window.JSL["array"] = function() { //A quick hack to make this possible - JSL.array(3,4,5).each(); var args = arguments; if(arguments.length == 1) args = arguments[0]; return new _array_init(args); } })(); /* * :TODO: * array.lastIndexOf() * array.difference(another_array) * * */