log.js

The javascript method library

Welcome to the growing collection of javascript methods for it's standard objects. You may download the code in one file here.

Array

pop

Compatibility. Popped element.
if(!Array.pop) Array.prototype.pop=function()
{
  var o=this[this.length-1];
  this.length--;
  return o;
}

push

Compatibility. New length of the array
if(!Array.push) Array.prototype.push=function()
{
  for(var i=0;i!=arguments.length;i++)
  {
    this[this.length]=arguments[i];
  }
  return this.length;
}

rndItem

Returns a random item from the given array. Same type as stored in the randomly choosen array property
Array.prototype.rndItem=function Array_rndItem() 
{ 
	return this[parseInt(Math.random()*this.length,10)];
}

Number

toFixed

Compatibility. Returns a string representing a number in fixed-point notation. Requires String.LPadThe number of deciNumber of digits after the decimal point.
if(!Number.prototype.toFixed) {
  Number.prototype.toFixed=function(n) {
    if(!n) n=0;
    var i=parseInt(this);
    var d=(''+Math.round((this-i)*Math.pow(10,n))).LPad(2,'0');
    return i+'.'+d;
  }
}

String

ltrim

Removes leading whitespace from string. The trimmed string
String.prototype.ltrim=function String_ltrim() 
{  
	return this.replace(/^[ \t\r\n]+/g,'');
}

multi

Multiply a string, and optionaly put a seperator between the strings. Anything that converts to a number. Negative values are made absolute. NaN throws an exception.Optional, anything that converts to a string.The created string
String.prototype.multi=function(number,seperator)
//--#An extended version of VB's String function
//--@number;type=number@String multiply factor. Should be positive
//--@seperator;type=string;optional@optional seperator, handy for lists
{
  var a=[];
  number=Math.abs(parseInt(number,10));
  if(isNaN(number)) throw new Error('String.multi: number parameter should be an number');
  while(number-->0)
  {
    a.push(this);
  }
  return a.join(seperator||'');
}

rtrim

Removes trailing whitespace from string. The trimmed string
String.prototype.rtrim=function String_rtrim() 
{  
	return this.replace(/[ \t\r\n]+$/g,'');
}

toFixed

Pads the argument with spaces or characters specifies by s until the length nThe required length of the stringThe padding character, default a spaceThe padded string
String.prototype.LPad=function(n,s) {
  if(n<0) return;
  if(typeof s=='undefined') s=' ';
  var res=this;
  while(res.length<n) res=s+res;
  return res;
}

trim

Removes leading and trailing whitespace from string. The trimmed string
String.prototype.trim=function String_trim() 
{  
	return this.replace(/^[ \t\r\n]+|[ \t\r\n]+$/g,'');
}