Skip to content

Instantly share code, notes, and snippets.

@AMHOL
Created June 7, 2013 15:34
Show Gist options
  • Save AMHOL/5730159 to your computer and use it in GitHub Desktop.
Save AMHOL/5730159 to your computer and use it in GitHub Desktop.
jQuery rails-like methods for string manip
$.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