Toolkit:string prototypes
From Surebert wiki
Overview
The surebert library has a bunch of handy string prototypes. Each one of them is assigned to String.prototype as well so you can call it directly from any string once loaded.
I plan on changing the include call to be sb.include('String.prototype.md5') very soon, please be aware.
javascript code
sb.include('strings.md5'); var str = 'tree'; alert(str.md5();
In the core lib
String.prototype.toCamel
Converts all dashes to camelStyle
javascript code
var str = 'background-color'; var newString = str.toCamel(); //newString = 'backgroundColor'
Extended Prototypes
These prototypes must be loaded via sb.include to by adding a script tag loading them in, e.g.
javascript code
sb.include('strings.prototype.toggle')
or
html4strict code
<script type="text/javascript" src="/surebert/strings/prototype/numPad.js"></script>String.prototype.basename
Grabs the basename from a url
javascript code
var myString = 'http://www.google.com/logo.gif'; var newString = myString.basename(); //newString = 'logo.gif';
String.prototype.br2nl
Converts HTML line breaks "
" to new lines "\n"
javascript code
var myString = 'hello<br />there'; var newString = myString.br2nl(); //newString = "hello\nthere";
String.prototype.cleanFileName
Cleans a filename up making it safe for upload, removes spaces, swicthes to camelStyle and strips extraneos punctuation
javascript code
var myString = 'hello there,, file . jpg'; var newString = myString.cleanFileName(); //newString = 'helloThereFile.jpg'
String.prototype.escapeHTML
Checks to see if a string is empty or not
javascript code
var str = '<p>hello</p>'; var newString = str.escapeHTML(); //newString = '<p>hello</p>'
String.prototype.hilite
Hilites a string within a text block
javascript code
var myString = 'There was a dog on earth'; var newString = myString.hilite('dog'); //newString = 'There was a <u style="backgroundColor:yellow;">dog</u> on earth';
String.prototype.isNumeric
Checks to see if a string is numeric (a float or number)
javascript code
var str = '12'; var answer = str.isNumeric(); //answer = true
String.prototype.linkify
Converts all URLs in a text block into actual html links
javascript code
var myString = 'Here http://www.surebert.com is a great javascript toolkit'; var newString = myString.linkify(); //newString = 'Here <a href="http://www.surebert.com" target="_blank">::link::</a> is a great javascript toolkit';
String.prototype.ltrim
Converts all URLs in a text block into actual html links
javascript code
var myString = 'Here http://www.surebert.com is a great javascript toolkit'; var newString = myString.linkify(); //newString = 'Here <a href="http://www.surebert.com" target="_blank">::link::</a> is a great javascript toolkit';
String.prototype.md5
This string prototype allow you to easily convert any string to md5
javascript code
'hello world'.md5();
String.prototype.nl2br
Replaces all new line "\n" with HTML break returns "
"
javascript code
var myString = "hello\nworld"; var newString = myString.nl2br(); //newString = 'hello<br />world';
String.prototype.numpad
Pads all numbers under 9 with a zero on the left
javascript code
var myString = 9; var newString = myString.numpad(); //newString = '09'
String.prototype.rgb2hex
Takes an rgb string and converts it to hex color rgb(255,255,255) -> #FFFFFF
javascript code
var myString = 'rgb(255,255,255)'; var newString = myString.rgb2hex(); //newString = '#FFFFFF'
String.prototype.rtrim
Trims all white space off the right side of a string
javascript code
var myString = 'hello '; var newString = myString.rtrim(); //newString = 'hello';
String.prototype.stripHTML
Removes all HTML tags from a string
javascript code
var myString = 'hello <p>world</p> on earth'; var newString = myString.stripHTML(); //newString = 'hello world on earth'
String.prototype.stripWhiteSpace
Removes all whitespace from a string
javascript code
var myString = 'hello world on earth'; var newString = myString.stripWhitespace(); //newString = 'helloworldonearth'
String.prototype.strstr
Returns true if the substring is found in the string
javascript code
var myString = 'hello world on earth'; var answer = myString.strstr('world'); //answer = true;
String.prototype.substrCount
Returns the numbers of times a substring is found in a string
javascript code
var myString = 'hello world on earth'; var answer = myString.substrCount('world'); //answer = 1;
String.prototype.substrReplace
Mimics php substrReplace replacing part of the string with another string from an index to a length
javascript code
var myString = 'hello world'; var answer = myString.substrReplace('girl', 0, 4); answer = 'girlo world'; </script> ==[http://surebert.com/toolkit/read/String.prototype.toElement String.prototype.toElement]== Converts a string of HTML code to a sb.element for dom manipulation <source lang="javascript"> //would return div as the element with all its children var el = '<div id="joe"><p class="test">hey there</p></div>'.toElement();
String.prototype.toFile
Transfers the contents of a string to an external file. Passes the string as POST data with a key name of data. The data is escaped. Make sure the external file referenced in the url property of the params object has permissions set to writeable. There is an example file server side file log.php in the surebert extras folder. It writes to log.txt in the same folder.
javascript code
var myString = 'Here is a string'; myString.toFile({ url : '../extras/log.php', onpass : function(){ alert('you'); }, onfail : function(){ alert('bad'); }, debug :1 });
String.prototype.toNumber
Converts a numeric string into an integer or float
javascript code
var myString = '12'; var num = myString.toNumber() +2; //num = 14 //without running toNumber it would return '122' var myString = '12.4'; var num = myString.toNumber() +2; //num = 14.4 //without running toNumber it would return '12.42'
String.prototype.trim
Trims whitespace from both left and right side of a string
javascript code
var str = ' hello world '; var newString = str.trim(); //newString = 'hello world'
String.prototype.typoFix
Fixes common typos in a string
javascript code
//should alert teh Teh didn't alert('teh Teh didn;t'.typoFix());
String.prototype.ucwords
Converts all first letters of words in a string to uppercase. Great for titles.
javascript code
var myString = 'hello world'; var newString = myString.ucwords(); //newString = 'Hello World'