Edit:
This is unpractical for the index page. The majority of the functions require a long paragraph of explanation. My index page would exceed the maximum amount of characters and run out of room. If the function name sounds interesting enough, just click away and read the author's post and explanation.
String.prototype.toStudlyCaps=function(){
var val=this; var results=this.match(/\b(\w+)\b/g); var expr="";
for(i=0;i<results.length;i++){
expr=eval("/\\b"+results[i]+"\\b/");
val=val.replace(expr,results[i].substr(0,1).toUpperCase()+results[i].substr(1,results[i].length).toLowerCase());
}
return val;
}
alert("hi how Are you?".toStudlyCaps()); // alerts "Hi How Are You?"
Just in case you are interested I thought you might like to see how this could be simplified. Instead of using an eval (which is rarely a good solution, and in your case it would have been better to create an new RegExp()) you can use a regular expression and call a function in the replacement argument, like so:
Code:
function wordify(str){
return str.replace(/\b\w/g, function(l){ return l.toUpperCase(); } );
}
Array.prototype.mergeData=function(){
this.sort();
var n=this.length, i=n;
while (i>1) if (this[--i]===this[i-1]) this[i]=this[--n];
this.length=n;
return this;
}
Due to the recent change done to the forums Editing posts, I can't edit the index of this thread any more.
Can I get a dedicated moderator to edit my 2nd post of this thread and add the following to my list:
Code:
[*]#89[post=586096]Array.prototype.mergeData[/post] by BigMoosie (revised)
Edit: Added.
Last edited by David Harrison; 06-25-2006 at 09:47 PM.
An Object doesn't have the normal Array prototype functions like join and push, pop, splice etc which makes it hard to work with.
Object.prototype.getKeys returns a list of all its keys in an array.
Object.prototype.getValues returns a list of all its values in an array.
Code:
<script type="text/javascript">
Object.prototype.getKeys=function(){
var myKeys=new Array();
for(var i in this){
if(i in Object)continue;
myKeys[myKeys.length]=i;
}
return myKeys;
}
Object.prototype.getValues=function(){
var myValues=new Array();
for(var i in this){
if(i in Object)continue;
myValues[myValues.length]=this[i];
}
return myValues;
}
</script>
<script type="text/javascript">
var myObject={"key1":"value1", "key2":"value2"};
alert(myObject.getKeys());
alert(myObject.getValues());
</script>
Based on PHP's base64_encode and base64_decode functions, I present to you:
String.prototype.base64_encode and String.prototype.base64_decode in JavaScript.
String.prototype.toXMLDocument For turning a String into an XMLDocument (full example included)
Note that it will even serve IE users with ActiveX disabled because it also checks for XML Data Island support. Read up on IE's XML tag.
Code:
<script type="text/javascript">
String.prototype.toXMLDocument=function(){
var d;
/* Firefox */
try{
d = (new DOMParser()).parseFromString(this, "text/xml");
return d;
}catch(e){};
/* XML Data Islands support (IE5+) */;
try{
d = document.createElement("xml");
d.appendChild(document.createTextNode(this));
document.documentElement.appendChild(d)
document.documentElement.removeChild(d)
d=d.XMLDocument;
if(d.documentElement)return d;
}catch(e){};
/* ActiveX enabled browsers */;
try{
d = new ActiveXObject("msxml.domdocument")
d.loadXML(this);
if(d.documentElement)return d;
}catch(e){};
/* Safari */;
try{
var uriURL = "data:text/xml;charset=utf-8," + encodeURIComponent(this);
d = new XMLHttpRequest();
d.open("GET", uriURL, false);
d.send(null);
d=d.responseXML;
if(d.documentElement)return d;
}catch(e){};
return new Object();//null object
}//func
</script>
<!-- defining code above, example usage below -->
<script type="text/javascript">
var str='<?xml version="1.0" encoding="utf-8"?>';
str+='\n<xmlcontainer>';
str+='\n<tagy>This is the value of the tag TAGY<\/tagy>';
str+='\n<\/xmlcontainer>';
var xmldoc=str.toXMLDocument();
alert(xmldoc.documentElement.getElementsByTagName("tagy")[0].firstChild.data)
</script>
XML Data Islands can also load external XMLDocuments.
Code:
var x=document.createElement("xml");
x.src="myfile.xml";
x.onreadystatechange=function(){
alert(x.readyState+":"+x.XMLDocument.readyState);
if(x.XMLDocument.readyState==4)
alert(x.XMLDocument.documentElement.tagName);
}
document.documentElement.appendChild(x);
document.documentElement.removeChild(x);
I'll hopefully have a cross-browser prototype up soon.
Last edited by Kor; 07-04-2007 at 10:01 AM.
Reason: User''s demand
Ultimater!
I agree wit u. These prototypes have been of great help to some of the developers who actually are not too much into javascript.
great work n cheers!
String.prototype.repeat is based on PHP's function: str_repeat. The power of the following technique is its high performance and minute coding when compared to unprofessional loops.
Code:
<script type="text/javascript">
String.prototype.repeat=function(a){
return new Array(a+1).join(this)
};
alert("-=".repeat(10))//-=-=-=-=-=-=-=-=-=-=
</script>
However that gives the mistaken impression of eleven joints. An array consisting of ten elements will only join nine times just-like there are only four gaps between one's five fingers. All the more reason for String.prototype.repeat.
That looks really good. I wish I could program as good as that. I turned in my assignment with the code I posted at the end of the last thread.
I am trying to learn enough to get me through this term with a decent grade so I can get my degree. This 39 year old is one old dog and it is hard for an old dog to learn new tricks.
Bookmarks