slight error, anyway, if you send an argument of false for this one it will give you short month names:
Code:
Date.prototype.MonthName=function(){
var arrMonths = arguments[0] ?
new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") :
new Array("January","February","March","April","May","June","July","August","September","October","November","December");
return arrMonths[this.getMonth()];
}
String.prototype.toWordCaps=function(){
var r="", s=this.toLowerCase(), l=s.length, w=true, t;
for (var i=0; i<l; i++) {
t=s.substr(i,1);
r+=(w)? t.toUpperCase() : t;
w=(t==" "); // you can add other options here like (t==" " || t=="-");
}
return r;
}
var str="hello my name is jiffy";
document.write(str.toWordCaps());
This will write to the page: "Hello My Name Is Jiffy", with the first letter of each word capitalised.
<script type="text/javascript"><!--
String.prototype.toWordCaps=function(){
return this.replace(/\b([a-z])/gi, function(s){return s.toUpperCase()});
}
var str="hello my name is jiffy";
document.write(str.toWordCaps());
//--></script>
Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.
That is good (I should learn RegExp some day) although there is a flaw, it doesnt toLowerCaps the rest of the letters, what would be the best way to do that?
You Should call the toLowerCase function before using the function then
It isn't flawed at all.
It does exactly what it says, it only touches the first letters of each word and leaves the rest unchanged.
I guess you are right, how would you do this in Regular Expression: ?
Code:
<script type="text/javascript"><!--
String.prototype.toInverseCase=function(){
var r="", l=this.length, t, T;
for (var i=0; i<l; i++){
t=this.substr(i,1);
T=t.toUpperCase();
r+=(t==T)?t.toLowerCase():T;
}
return r;
}
var str="Hello My Name Is Jiffy";
document.write(str.toInverseCase());
//--></script>
<script type="text/javascript"><!--
String.prototype.toInverseCase = function (){
return this.replace(/([a-z]{1})/gi, function (s){return s==s.toLowerCase()?s.toUpperCase():s.toLowerCase()});
}
var str="Hello My Name Is Jiffy";
document.write(str.toInverseCase());
//--></script>
Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.
Thanks, I believe this would be more efficient as it would never call .toLowerCase() twice for the same character:
Code:
<script type="text/javascript"><!--
String.prototype.toInverseCase = function (){
return this.replace(/([a-z]{1})/gi, function (s){var t=s.toLowerCase(); return s==t?t.toUpperCase():t;});
}
var str="Hello My Name Is Jiffy";
document.write(str.toInverseCase());
//--></script>
Correct, though I doubt it will make that much of a difference (unless you plan on running the script a good 10,000 times, of course Although that still probably wouldn’t affect the script by more than some 20 milliseconds).
Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.
Function.prototype.time =
function() {
var sTime = new Date();
this.apply(null,arguments);
var eTime = new Date();
return eTime.getTime() - sTime.getTime();
};
Say you have a function f. To time the function with arguments arg0, arg1 & arg2 do
Code:
f.time(arg0,arg1,arg2);
Because calling eTime() actually adds to the time of the function we must deduct the time it takes to call eTime() (as small as it is it will lessen miscalculation).
So if we take t1 before our function then t2 after the function and t3 after t2 we can use:
t2 - t1 - (t3 - t2) = 2*t2 - t3 - t1;
Code:
Function.prototype.time = function() {
var t1 = new Date();
this.apply(null,arguments);
var t2 = new Date();
var t3 = new Date();
return 2*t2 - t3 - t1;
};
Bookmarks