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. ArraypopCompatibility. Popped element.
if(!Array.pop) Array.prototype.pop=function()
{
var o=this[this.length-1];
this.length--;
return o;
}
pushCompatibility. 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;
}
rndItemReturns 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)];
}
NumbertoFixedCompatibility. 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;
}
}
StringltrimRemoves leading whitespace from string. The trimmed string
String.prototype.ltrim=function String_ltrim()
{
return this.replace(/^[ \t\r\n]+/g,'');
}
multiMultiply 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||'');
}
rtrimRemoves trailing whitespace from string. The trimmed string
String.prototype.rtrim=function String_rtrim()
{
return this.replace(/[ \t\r\n]+$/g,'');
}
toFixedPads 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;
}
trimRemoves 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,'');
}
|