Javascript

Array Extensions


  

ForEach

Moves through each element of an a simple 1D array and passes the element to a callback function for processing. 

Array.prototype.ForEach = function(callBack){
 for(var i = 0; i < this.length; i++){
         callBack(this[i], i, this);
 }
}

Find

Moves through each element of an a simple 1D array searching for the input value and passes the array element to the callback function.This is quite a silly thing to do as you must already have the value you want to find i guess, but the basic idea is there for you to extend it to cope with multi-dimensional arrays so that you search for one particular part of an object for example. 

Array.prototype.Find = function(value, callback){
     this.ForEach(function (item, index, obj){
	if (item == value){
	            callback(item,index,obj)
	}
      }
}

String Extensions


  

stripLineBreaks

Replace line breaks in the current string

String.prototype.stripLineBreaks = function(){
 return this.replace(/\n/g,"");
}

Replace

Replace one substring with another. Its suprising how oftern you need something like this 

String.prototype.Replace = function(findString, replaceWith){
	return this.replace(new RegExp((findString + "").replace(/\\/g, "\\\\"), "gi"), replaceWith);
}

Trim

Remove leading and trailing white space 

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

Numeric Extensions


GreaterThan

Determines if the current number is greater than the input

Number.prototype.GreaterThan = function(value){
    return (this > value)?true:false;
}

LessThan

Determines if the current number is less than the input

Number.prototype.LessThan = function(value){
    return (this < value)?true:false;
}

Between

Determines if the current number is between two values

Number.prototype.Between = function(val1,val2){
    return (value >= val1 || value <= val2)?true:false;
}

Date Extensions


getUTC

Returns the current date in UTC format

Date.prototype.getUTC = function(){
	var d = new Date();
	var s ="";

	s += (d.getUTCMonth() + 1) + "/";
	s += d.getUTCDate() + "/";
	s += d.getUTCFullYear();

   return s;

}

toISODate

returns the date in the standard ISO format

Date.prototype.toISODateString = function()
{
	var year  = this.getUTCFullYear();
	var month = this.padNumber(this.getUTCMonth() + 1, 2); // UTC month is zero-based not from 1
	var day   = this.padNumber(this.getUTCDate(), 2);

	return year + "-" + month + "-" + day;
}

fromISOString

Converts the date into the required ISO format

Date.prototype.fromISOString = function(dateIn, LocalRequired)
{
	var clippedDateIn = dateIn;
	var ISOStringLength = 23;
	// if there is no timestamp, add one.
	if(clippedDateIn.length == 10){
		clippedDateIn += " 00:00:00"
	}

	if(clippedDateIn.length > ISOStringLength) clippedDateIn = clippedDateIn.substr(0,ISOStringLength);
	var dateArray = this.validateDateTime(clippedDateIn);
	if (dateArray)
	{
		if(LocalRequired){
			var currentDate = new Date();
			var timeZoneOffSet = currentDate.getTimezoneOffset()/60 // To convert to hours
			// Convert to UTC milliseconds and add the timeZoneOffSet(this could be a +/- number)
			var ms = Date.UTC(dateArray[0], dateArray[1] - 1, dateArray[2],
							  (dateArray[3]+(timeZoneOffSet)), dateArray[4], dateArray[5], dateArray[6] );
		} else {
			// Convert to UTC milliseconds so we don't end up with the wrong date being returned
			var ms = Date.UTC(dateArray[0], dateArray[1] - 1, dateArray[2],
							  dateArray[3], dateArray[4], dateArray[5], dateArray[6] );
		}

		return new Date(ms);
	}
	return null;
}

toISOTime

Returns the time in the required ISO format

Date.prototype.toISOTimeString = function()
{
	var hours   = this.padNumber(this.getUTCHours(), 2);
	var minutes = this.padNumber(this.getUTCMinutes(), 2);
	var seconds = this.padNumber(this.getUTCSeconds(), 2);
	var ms      = this.padNumber(this.getUTCMilliseconds(), 3);

	return hours + ":" + minutes + ":" + seconds;
}

daysInMonth

Returns the nubmer of days in the current month

Date.prototype.daysInMonth = function(){
	var daysofmonth   = new Array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var daysofmonthLY = new Array( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

	function LeapYear(year) {
		if ((year/4)   != Math.floor(year/4))   return false;
		if ((year/100) != Math.floor(year/100)) return true;
		if ((year/400) != Math.floor(year/400)) return false;
		return true;
	}

	if (LeapYear(this.getFullYear())) {
	  daysofmonth = daysofmonthLY;
	}

	return daysofmonth[this.getMonth()]
}

padNumber

Padds the current number with zero’s

Date.prototype.padNumber = function(number, desiredSize)
{
	var pad = "";
	var padSize =  desiredSize - number.toString().length;
	for (var i=0;i<padSize;i++) pad += "0";

	return pad + number
}

General Usage