Created
June 7, 2013 15:34
-
-
Save AMHOL/5730159 to your computer and use it in GitHub Desktop.
jQuery rails-like methods for string manip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.truncate = function(text, length) { | |
if ( length === undefined ) length = 255; | |
if ( text.length <= length ) return text; | |
return text.substring(0, length) + '...'; | |
}; | |
$.number_with_delimiter = function(number, delimiter) { | |
var part, new_number = []; | |
if ( delimiter === undefined ) delimiter = ','; | |
number = number.toString().split('').reverse(); | |
while( (part = number.splice(0, 3)) && number.length ) { | |
new_number.push(part.reverse().join('')); | |
} | |
new_number.push(part.reverse().join('')); | |
return new_number.reverse().join(','); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment