How To/webdev/

== Totally random hash generator ==

Function generates random stirng containing characters: 0-9a-zA-Z.
Function takes one argument which may be Integer or String.
If argument is Integer then argument defines length of generated string.
If argument is String then given string is returned but all 'x' characters are replaced with random characters.

{{{ javascript

var hash = function(s){
	var n;
	if (typeof(s) == 'number' && s === parseInt(s, 10)){
		s = Array(s + 1).join('x');
	}
	return s.replace(/x/g, function(){
		var n = Math.round(Math.random() * 61) + 48;
		n = n > 57 ? (n + 7 > 90 ? n + 13 : n + 7) : n;
		return String.fromCharCode(n);
	});
}

}}}



{{{

>>> hash(5);
"XjJfV"

>>> hash('xxxx-xx-xxxx-2011')
"YDFb-5e-cShX-2011"

>>> hash('ABCD'); // No x letter so nothing to replace
"ABCD"

}}}

== Hash based on date - fast way ==

If you want hash based on date you may just use 

{{{
>>> hash = (+new Date());
>>> console.log(hash);
1317814876299
}}}