joon hee - StandardLibrary-1.81

Documentation | Source

NAME

Array - Class for dealing with ordered arrangements

SYNOPSIS

        var arr = new Array('element1', 'element2', 'element3');
        arr.reverse().push();

DESCRIPTION

Array is a Class for dealing with ordered arrangements of values, do not use this class for a dictionary or a hash.

This is based heavily upon the code released by Mozilla and it is based upon complete compatability with the Mozilla platform, that way existing code will continue to run with this implementation of Array.

METHODS

Inherited

From Function.prototype: apply, call, toSource, toString, valueOf

From Object.prototype: hasOwnProperty, isPrototypeOf, propertyIsEnumerable, unwatch, watch

Constructor(*@elements) Returns Array

*** Not Implementable in Standard JavaScript.

This is used to create an Array object. The length property requires a setter so we can not implement Array in pure JavaScript code.

join(String separator) Returns String

This takes each element of the Array and it adds seperator in between each of the elements to join the Array.

toString() Returns String

This function will convert an Array to a String.

toSource() Returns String

This function will convert the Array to source code - such that you can use eval(arr.toSource()).

reverse() Returns Array

Reverse the order of the arrangement of the Array and return.

sort(Function compareFunction) Returns undefined

Sorts and modifies the elements of this array.

pop() Returns Any

This function removes and returns the last element in this Array.

shift() Returns Any

This removes and returns the first element of the Array.

push(*@elements) Returns Number

This will push to the end of the array the following *@elements.

unshift(*@elements) Returns Number

This will push to the beginning of the array the *@elements.

slice(Number begin, Number end) Returns Array

This returns an Array starting at begin and ending at end.

concat(*@insertions) Returns Array

This will add a group of other Arrays or Values to the end of this Array, if an Array is passed as an argument its elements become a part of the current Arrays elements.

splice(Number index, Number howMany, *@insertions) Returns Array

Adds or removes elements from the Array.

indexOf(Any element, Number from? ) Returns Number

This gets the index of the element.

lastIndexOf(Any element, Number from?) Returns Number

This returns the last index of the element.

every(Function fun, Any thisp?) Returns Boolean

This returns true if every single value in the array matches fun().

filter(Function fun, Any thisp?) Returns Array

This returns a new Array without values that match fun().

forEach(Function fun, Any thisp?) Returns undefined

This performs a call on each element of the Array.

map(Function fun, Any thisp?) Returns Array

This returns a new Array with the results of fun() to each element in the Array.

some(Function fun, Any thisp?) Returns Boolean

This returns true if any element in the Array matches fun().

reduce( Function fun, Any initialValue? ) Returns Any

Apply fun() against two values of the array (from left-to-right) as to reduce it to a single value and return that value.

reduceRight( Function fun, Any initialValue? ) Returns Any

Apply fun() against two values of the array (from right-to-left) as to reduce it to a single value and return that value.

AUTHOR

Jhuni, <jhuni_x@yahoo.com>

COPYRIGHT

Public Domain

/*=pod

=head1 NAME

Array - Class for dealing with ordered arrangements

=head1 SYNOPSIS

	var arr = new Array('element1', 'element2', 'element3');
	arr.reverse().push();

=head1 DESCRIPTION

Array is a Class for dealing with ordered arrangements of values, do not use this class for a dictionary or a hash.

This is based heavily upon the code released by Mozilla and it is based upon complete compatability with the Mozilla platform, that way existing code will continue to run with this implementation of Array.

=head1 METHODS

=head2 Inherited

From Function.prototype:
apply, call, toSource, toString, valueOf 

From Object.prototype:
hasOwnProperty, isPrototypeOf, propertyIsEnumerable, unwatch, watch

=cut*/



/*=pod

=head2 Constructor(*@elements) Returns Array

*** Not Implementable in Standard JavaScript.

This is used to create an Array object. The length property requires a setter so we can not implement Array in pure JavaScript code.

=cut*/





/*=pod

=head2 join(String separator) Returns String

This takes each element of the Array and it adds seperator in between each of the elements to join the Array.

=cut*/

Array.prototype.join = function(separator){

	var rval = '';
	var mylen = this.length;

	if( typeof separator == 'undefined' ) {
		separator = ',';
	}

	for( var i = 0; i < mylen; i++ ) {
		rval += this[i];
		
		if( i != mylen-1 ) {
			rval += separator;		
		}
	}

	return rval;

};



/*=pod

=head2 toString() Returns String

This function will convert an Array to a String.

=cut*/

Array.prototype.toString = function(){
	return this.join(",");
};



/*=pod

=head2 toSource() Returns String

This function will convert the Array to source code - such that you can use eval(arr.toSource()).

=cut*/

Array.prototype.toSource = function() {
	return( '[' + this.join(", ") + ']' );
};



/*=pod

=head2 reverse() Returns Array

Reverse the order of the arrangement of the Array and return.

=cut*/

Array.prototype.reverse = function() {

	var start = 0;
	var end = this.length - 1;

	while( start < end ) {
	
		var endingValue = this[end];
		this[end] = this[start];	
		this[start] = endingValue;

		start++;
		end--;

	}
	
	return this;

};





/*=pod

=head2 sort(Function compareFunction) Returns undefined

Sorts and modifies the elements of this array.

=cut*/

Array.prototype.sort = function(compareFunction) {
	
	// Setup compareFunctions default sort method
	if( typeof compareFunction == 'undefined' ) {
		compareFunction = function(a,b) {
			if( a.toString() > b.toString() )  {      
				return 1; 
        	} else {      
        		return -1;  
        	}
		}
	}
	
	// Bubble sort is a nice and simple algorithm.
	var elems = this.length-1;
	
	while(1) {
		
		var swappedYet = false;
			
		for( var i = 0; i < elems; i++ ) {
			var result = compareFunction( this[i], this[i+1] );

			if( result > 0 ) {
				// Swap:
				var temp = this[i+1];
				this[i+1] = this[i];
				this[i] = temp;	
				swappedYet = true;
			}
			
		}
		
		// If there was no swapping for a whole loop then sorting is done.
		if( !swappedYet ) {
			break;
		}
		
	}
	
	
};



/*=pod

=head2 pop() Returns Any

This function removes and returns the last element in this Array.

=cut*/

Array.prototype.pop = function() {

	if( this.length == 0 ) {
		return undefined;
	}

	var end = this.length-1;
	var temp = this[end];
	
	delete this[end];
	this.length = this.length-1;
	
	return temp;

};


/*=pod

=head2 shift() Returns Any

This removes and returns the first element of the Array.

=cut*/

Array.prototype.shift = function() {

	var mylen = this.length;
	if( mylen == 0 ) {
		return undefined;
	}

	var temp = this[0];

	for( var i = 1; i < mylen; i++ ) {
		this[i-1] = this[i];
	}
	
	delete[mylen-1];
	this.length = this.length-1;

	return temp;

};


/*=pod

=head2 push(*@elements) Returns Number

This will push to the end of the array the following *@elements.

=cut*/

Array.prototype.push = function() {

	var mylen = this.length;

	var arglen = arguments.length;

	for( var i = 0; i < arglen; i++ ) {
		this[mylen+i] = arguments[i];
	}

	return this.length;

};


/*=pod

=head2 unshift(*@elements) Returns Number

This will push to the beginning of the array the *@elements.

=cut*/

Array.prototype.unshift = function() {

	var arglen = arguments.length;
	var mylen = this.length;

	for( var i = mylen-1; i >= 0; i-- ) {
		this[i+arglen]=this[i];
	}

	for( var i = 0; i < arglen; i++ ) {
		this[i] = arguments[i];
	}

	return this.length;

};


/*=pod

=head2 slice(Number begin, Number end) Returns Array

This returns an Array starting at begin and ending at end.

=cut*/

Array.prototype.slice = function(begin, end) {

	var len = this.length;
	
	if( typeof(begin) == 'undefined' || begin == null ) {
		begin = 0;	
	} else if( 0 > begin ) {
		begin += len;
	}
	
	if( typeof(end) == 'undefined' ) {
		end = len;
	} else if( 0 > end ) {
		end += len;
	}
	
	if( end > this.length ) { end = this.length; }

	// Return the sliced Array as rval
	var rval = [];
	var currentIndex = 0;

	for( var i = begin; i < end; i++ ) {
		rval[currentIndex] = this[i];
		currentIndex++;
	}	

	return rval;

};



/*=pod

=head2 concat(*@insertions) Returns Array

This will add a group of other Arrays or Values to the end of this Array, if an Array is passed as an argument its elements become a part of the current Arrays elements.

=cut*/

Array.prototype.concat = function(){

	var arglen = arguments.length;
	var mylen = this.length;
	
	var rval = new Array();

	for( var i = 0; i < mylen; i++ ) {
		rval[i] = this[i];
	}

	var arrayOffset = mylen;
	for( var i = 0; i < arglen; i++ ) {
		var currentArg = arguments[i];

		if( currentArg.constructor.toString().indexOf("Array") == -1 ) {
			rval[arrayOffset+i] = currentArg;
		} else {
			var subArrayLen = currentArg.length;
			for( var si = 0; si < subArrayLen; si++ ) {
				rval[arrayOffset+i] = currentArg[si];
				arrayOffset++;
			}
			arrayOffset--;
		}

	}

	return rval;	

};


/*=pod

=head2 splice(Number index, Number howMany, *@insertions) Returns Array

Adds or removes elements from the Array.

=cut*/

Array.prototype.splice = function(index, howMany /* insertions */) {

	if( arguments.length == 0 ) { return arguments.callee; }

	var len = this.length;
	var insertionCount = arguments.length-2; 

	if( typeof howMany == 'undefined' ) { 
		howMany = len; 
		insertionCount++; 
	}
	
	if( 0 > howMany ) { howMany = 0; }
	if( 0 > index ) { index += len; }

	/* Now perform the slice operation: */

	var rval = this.slice(index,index+howMany);
	
	for( var i = index; i < len-howMany; i++ ) {
		this[i] = this[i+howMany];
	}
	this.length -= howMany;
	
	// START INSERTIONS:
	this.length += insertionCount;
	for( var i = this.length-1; i > index; i-- ) {
		this[i] = this[i-insertionCount];
	}
	
	var argn = 0;
	for( var i = 0; i < insertionCount; i++ ) {
		this[index+i] = arguments[2+argn++];
	}
	
	return rval;

};




/*=pod

=head2 indexOf(Any element, Number from? ) Returns Number

This gets the index of the element.

=cut*/

Array.prototype.indexOf = function(elt /*, from*/) {
	
	var len = this.length;

	var from = Number(arguments[1]) || 0;
	from = (from < 0) ? Math.ceil(from) : Math.floor(from);
	
	if (from < 0) {
		from += len;
	}

	for (; from < len; from++) {
		if (from in this && this[from] === elt) {
			return from;
		}
	}
	
	return -1;
	
};



/*=pod

=head2 lastIndexOf(Any element, Number from?) Returns Number

This returns the last index of the element.

=cut*/

Array.prototype.lastIndexOf = function(elt /*, from*/)
{
	var len = this.length;

	var from = Number(arguments[1]);
	if (isNaN(from)) {
		from = len - 1;
	} else {
		from = (from < 0) ? Math.ceil(from) : Math.floor(from);
		
		if (from < 0) {
			from += len;
		} else if (from >= len) {
			from = len - 1;
		}
		
	}

	for (; from > -1; from--) {
		if (from in this && this[from] === elt) {
			return from;
		}
	}
	
	return -1;
};



/*=pod

=head2 every(Function fun, Any thisp?) Returns Boolean

This returns true if every single value in the array matches fun().

=cut*/

Array.prototype.every = function(fun /*, thisp*/)
{
	var len = this.length;
	if (typeof fun != "function") {
		throw new TypeError();
	}

	var thisp = arguments[1];
	for (var i = 0; i < len; i++)
	{
		if (i in this && !fun.call(thisp, this[i], i, this)) {
			return false;
		}
	}

	return true;
};


/*=pod

=head2 filter(Function fun, Any thisp?) Returns Array

This returns a new Array without values that match fun().

=cut*/

Array.prototype.filter = function(fun /*, thisp*/)
{
	var len = this.length;
	if (typeof fun != "function") {
		throw new TypeError();
	}

	var res = new Array();
	var thisp = arguments[1];
	
	for (var i = 0; i < len; i++) {
		if (i in this) {
			var val = this[i]; // in case fun mutates this
			if (fun.call(thisp, val, i, this)) {
			  res.push(val);
			}
		}
	}

	return res;
};


/*=pod

=head2 forEach(Function fun, Any thisp?) Returns undefined

This performs a call on each element of the Array.

=cut*/

Array.prototype.forEach = function(fun /*, thisp*/)
{
	var len = this.length;
	
	if (typeof fun != "function") {
		throw new TypeError();
	}

	var thisp = arguments[1];
	for (var i = 0; i < len; i++)
	{
		if (i in this) {
			fun.call(thisp, this[i], i, this);
		}
	}
	
};




/*=pod

=head2 map(Function fun, Any thisp?) Returns Array

This returns a new Array with the results of fun() to each element in the Array.

=cut*/

Array.prototype.map = function(fun /*, thisp*/)
{
	var len = this.length;
	if (typeof fun != "function") {
		throw new TypeError();
	}

	var res = new Array(len);
	var thisp = arguments[1];
	for (var i = 0; i < len; i++)
	{
		if (i in this) {
			res[i] = fun.call(thisp, this[i], i, this);
		}
	}

	return res;
};



/*=pod

=head2 some(Function fun, Any thisp?) Returns Boolean

This returns true if any element in the Array matches fun().

=cut*/

Array.prototype.some = function(fun /*, thisp*/)
{
	var len = this.length;
	if (typeof fun != "function") {
		throw new TypeError();
	}

	var thisp = arguments[1];
	for (var i = 0; i < len; i++) {
		if (i in this && fun.call(thisp, this[i], i, this)) {
			return true;
		}
	}

	return false;
};



/*=pod

=head2 reduce( Function fun, Any initialValue? ) Returns Any

Apply fun() against two values of the array (from left-to-right) as to reduce it to a single value and return that value.

=cut*/

Array.prototype.reduce = function(fun /*, initial*/)
{
	var len = this.length;
	if (typeof fun != "function") {
		throw new TypeError();
	}

	// 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 rv = arguments[1];
	} else {
		do {
			if (i in this) {
				rv = this[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) {
			rv = fun.call(null, rv, this[i], i, this);
		}
	}

	return rv;
};




/*=pod

=head2 reduceRight( Function fun, Any initialValue? ) Returns Any

Apply fun() against two values of the array (from right-to-left) as to reduce it to a single value and return that value.

=cut*/

Array.prototype.reduceRight = function(fun /*, initial*/)
{
	var len = this.length;
	if (typeof fun != "function") {
		throw new TypeError();
	}

	// no value to return if no initial value, empty array
	if (len == 0 && arguments.length == 1) {
		throw new TypeError();
	}

	var i = len - 1;
	if (arguments.length >= 2) {
		var rv = arguments[1];
	} else {
		do {
			if (i in this) {
				rv = this[i--];
				break;
			}

			// if array contains no values, no initial value to return
			if (--i < 0) {
				throw new TypeError();
			}
		} while (true);
	}

	for (; i >= 0; i--) {
		if (i in this) {
			rv = fun.call(null, rv, this[i], i, this);
		}
	}

	return rv;
};




/*=pod

=head1 AUTHOR

Jhuni, <jhuni_x@yahoo.com>

=head1 COPYRIGHT

Public Domain

=cut*/