Frogg logo

Frogg's web tools

Hand Crafted Tools

Home > Coding > Javascript usefull codes
Welcome on Frogg's web tools | Current date :
11/12/2018

Javascript usefull codes

JavaScript is a high-level, dynamic, untyped, and interpreted programming language. It has been standardized in the ECMAScript language specification. Alongside HTML and CSS, it is one of the three core technologies of World Wide Web content production; the majority of websites employ it and it is supported by all modern Web browsers without plug-ins. JavaScript is prototype-based with first-class functions, making it a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles. It has an API for working with text, arrays, dates and regular expressions, but does not include any I/O, such as networking, storage, or graphics facilities, relying for these upon the host environment in which it is embedded. (source wikipedia)

  • All the following code are using the prototype property to be more performent.
  • Thoose codes are written by myself in pure javascript for modern browser without using any frameworks and are free to use.
  • To compare javascript function benchmarking, you can find an article here
  • To improve javascript performance, you can minify it like my JS minifier (still in beta & can be highly improved)

Javascript Dev tips

copy to clipboard You can create debugger break points (modern browser)
debugger;

copy to clipboard You can use styles in logs (modern browser)
console.log( "%cTest" , "color:red;font-weight:bold;" );

Javascript Short cuts

copy to clipboard variable declaration
var a="a";
var b="b";
var i=0;
var j=0;
copy to clipboard can be wrote
var a="a" , b="b" , i=j=0;

copy to clipboard conditionnal without else
if( 'a'=='a' ) { doSomeThing(); }
copy to clipboard can be wrote
'a'=='a' && doSomeThing();

copy to clipboard bi conditionnal without else
if( 'a'=='a' && 'b'=='b' ) { doSomeThing(); }
can be wrote copy to clipboard
'a'=='a' && 'b'=='b' && doSomeThing();

copy to clipboard conditionnal with else
if('a'=='a') { doSomeThing(); } else { doOtherThing(); }
copy to clipboard can be wrote
'a'=='a' ? doSomeThing() : doOtherThing() ;

copy to clipboard bi conditionnal with else
if('a'=='a' || 'b'=='b' ) { doSomeThing(); } else { doOtherThing(); }
copy to clipboard can be wrote
'a'=='a' || 'b'=='b' ? doSomeThing() : doOtherThing() ;

Javascript String prototypes

Function to add extra char {chr} all {n} chars in string
@param chr as Char
@param n as Integer
@return this as String copy to clipboard
String.prototype.addExtraChar=function( chr , n )
{
if( n < 1 ) return this;
return this.match( new RegExp( '.{1,'+n+'}' , 'g' ) ).join( chr );
}

Function to add Char {chr} at the start of a string to complete it from this.length to {len}
@param len as Integer
@param chr as Char
@return String copy to clipboard
String.prototype.pad=function( len , chr )
{
var tmp = this;
while (tmp.length < len)tmp = chr + tmp;
return tmp;
}

Function to escape a string for a regexp
@return this as String copy to clipboard
String.prototype.escapeRegExp=function()
{
return this.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

Function to reverse a string
@return String copy to clipboard
String.prototype.reverse=function()
{
var l=this.length-1,
	r='';
for( ; l>-1 ; l-- ) r+=this[l];
return r;
}

Function to get string after separator {sep}
@return String copy to clipboard
String.prototype.getStringAfter=function( sep )	
{
var last = this.split( sep );
return last[ last.length-1 ];		
}

Function to get file informations
@return Object copy to clipboard
String.prototype.getFileInfo=function()
{
var obj 		= {},
	fileInfo 	= this.split('/');
obj.filename 	= fileInfo.pop();
obj.path		= fileInfo.join('/');
obj.ext			= obj.filename.getStringAfter( '.' );
obj.fullname	= this;
return obj;
}

Javascript Number prototypes

Function to add Char {chr} at the start of an Integer to complete it from this.length to {len}
@param len as Integer
@param chr as Char
@return String copy to clipboard
Number.prototype.pad=function( len , chr )
{
var tmp = this.toString();
while (tmp.length < len)tmp = chr + tmp;
return tmp;
}

Javascript Array prototypes

Function to get a random value from an array
@return String or Integer copy to clipboard
Array.prototype.random = function()
{
return this[ Math.floor( ( Math.random()*this.length ) ) ];
}

Function to check if a value {val} exist in array
@param val as String or Integer
@return Boolean copy to clipboard
Array.prototype.inArray = function( val )
{
return this.indexOf( val )!=-1 ? true : false;
}

Function to remove value(s) if {items} exist in array
@param items as String or Array
@return this as Array copy to clipboard
Array.prototype.spliceItem = function( items )
{
var arr = [],
	i=l=0;
( typeof items=="object" ) ? arr = items : arr.push( items );
l=arr.length;
for( ; i < l ; i++ )
	this.indexOf( arr[i] )!==-1 && this.splice( this.indexOf( arr[i] ) , 1 );
return this;
}

Javascript Node prototypes

Function to check if node has class {css}
@param css as String
@return Boolean copy to clipboard
Node.prototype.hasClass=function( css )
{
return this.classList.contains(css);	
}

Function to add class {css} to the node
@param css as String or Array
@return this as Node copy to clipboard
Node.prototype.addClass=function( css )
{
var s,cssList = [];
typeof css=="string" ? cssList.push( css ) : cssList = css;
for( s in cssList )
	typeof cssList[s]=="string" && !this.hasClass( cssList[s] ) && this.classList.add( cssList[s] );
return this;
}

Function to remove class {css} to the node
@param css as String or Array
@return this as Node copy to clipboard
Node.prototype.removeClass=function( css )
{
var s,cssList = [];
typeof css=="string" ? cssList.push( css ) : cssList = css;
for( s in cssList )
	typeof cssList[s]=="string" && this.hasClass( cssList[s] ) && this.classList.remove( cssList[s] );
return this;		
}

Function to toggle class {css} to the node
@param css as String
@return this as Node copy to clipboard
Node.prototype.toggleClass=function( css )
{
return this.hasClass( css ) ? this.removeClass( css ) : this.addClass( css );
}

Function to get the node index
@return Integer copy to clipboard
Node.prototype.getIndex=function()
{
var i = 0,
	node = this;
while( (node = node.previousSibling) != null )	i++; 
return i+1;
}
Page created by the 29/09/2016 19:57
Generated in 0.037 sec & displayed in ... sec