public
Last active

Was reading http://jspro.com/raw-javascript/intelligent-string-abbreviation/ and decided to try and clean up the example. I then made another version which separated specific functionality into individual functions. Finally I made a third variation which made dependencies external.

  • Download Gist
1.abbreviate.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/**
* Shorten a string by a specified number of characters and end the string
* using either a user specified suffix or a default suffix.
* Ensures string is not abbreviated prematurely (e.g. half way through a word)
*
* @param {string} user specified string
* @param {number} the maximum allowed characters before abbreviating
* @param {string} the characters used to end the string
* @return {string} the abbreviated string
*/
function abbreviate (user_str, user_max, user_suffix) {
var startend_spaces = /^\s+|\s+$/g;
var linebreak_spaces = /[\r\n]*\s*[\r\n]+/g;
var tab_spaces = /[ \t]+/g;
var ending_space = /[ ]$/g;
var abbr = '';
var suffix = (typeof user_suffix !== 'undefined' ? user_suffix : '...');
var max = user_max - suffix.length;
var counter = 0;
var string = user_str;
var len;
 
// if after cleaning up all extraneous white space, if the length of the
// string is less than the `max` value then just return the string
if ((string = string.replace(startend_spaces, '').replace(linebreak_spaces, ' ').replace(tab_spaces, ' ')).length <= max) {
return string;
}
 
string = string.split(' ');
len = string.length;
 
while (counter < len) {
// individual characters of a string can be accessed via bracket notation
if ((abbr + string[counter]).length < max) {
abbr += string[counter] + ' ';
} else {
break;
}
 
counter++;
}
 
return abbr.replace(ending_space, '') + suffix;
}
 
// Example usage:
console.log(abbreviate('this is my very long string that probably should be cropped at some point soon', 11, '!!!'));
console.log(abbreviate('this is my very long string that probably should be cropped at some point soon', 20));
2.more-functional-version.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
/**
* Shorten a string by a specified number of characters and end the string
* using either a user specified suffix or a default suffix.
* Ensures string is not abbreviated prematurely (e.g. half way through a word)
*
* @param {string} user specified string
* @param {number} the maximum allowed characters before abbreviating
* @param {string} the characters used to end the string
* @return {string} the abbreviated string
*/
function abbreviate (user_str, user_max, user_suffix) {
/**
* Code Structure:
* - Variables
* - Functions
* - clean_string
* - is_too_short
* - crop
* - Initialisation
*/
var suffix = (typeof user_suffix !== 'undefined' ? user_suffix : '...');
var max = user_max - suffix.length;
var string = user_str;
 
function clean_string(){
var startend_spaces = /^\s+|\s+$/g;
var linebreak_spaces = /[\r\n]*\s*[\r\n]+/g;
var tab_spaces = /[ \t]+/g;
 
string = string.replace(startend_spaces, '').replace(linebreak_spaces, ' ').replace(tab_spaces, ' ');
}
 
function is_too_short(){
return string.length <= max;
}
 
function crop(){
var abbr = '';
var counter = 0;
var len;
var ending_space = /[ ]$/g;
 
string = string.split(' ');
len = string.length;
 
while (counter < len) {
// individual characters of a string can be accessed via bracket notation
if ((abbr + string[counter]).length < max) {
abbr += string[counter] + ' ';
} else {
break;
}
 
counter++;
}
 
return abbr.replace(ending_space, '') + suffix;
}
 
clean_string(); // remove all extraneous white space
 
if (is_too_short()) {
return string;
}
 
return crop();
}
 
// Example usage:
console.log(abbreviate('this is my very long string that probably should be cropped at some point soon', 11, '!!!'));
console.log(abbreviate('this is my very long string that probably should be cropped at some point soon', 20));
3.evenmore-functional.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
/**
* Shorten a string by a specified number of characters and end the string
* using either a user specified suffix or a default suffix.
* Ensures string is not abbreviated prematurely (e.g. half way through a word)
*
* Dependencies:
* - clean_string
* - is_too_short
* - crop
*
* @param user_str {string} user specified string
* @param user_max {number} the maximum allowed characters before abbreviating
* @param user_suffix {string} the characters used to end the string
* @return {string} the abbreviated string
*/
function abbreviate (user_str, user_max, user_suffix) {
var suffix = (typeof user_suffix !== 'undefined' ? user_suffix : '...');
var max = user_max - suffix.length;
var string = clean_string(user_str); // remove all extraneous white space
 
if (shorter_than(string, max)) {
return string;
}
 
return crop(string, max, suffix);
}
 
/**
* Removes any unnecessary spaces from the specified string
*
* @param string {string} user specified string
* @return {string} the string cleared of unnecessary spaces
*/
function clean_string (string) {
var startend_spaces = /^\s+|\s+$/g;
var linebreak_spaces = /[\r\n]*\s*[\r\n]+/g;
var tab_spaces = /[ \t]+/g;
 
return string.replace(startend_spaces, '').replace(linebreak_spaces, ' ').replace(tab_spaces, ' ');
}
 
/**
* Checks if the string length is less or equal to the maximum allowed
*
* @param string {string} user specified string
* @param max {number} the maximum allowed characters before abbreviating
* @return {boolean} whether the length of the string exceeds the maximum
*/
function shorter_than (string, max) {
return string.length <= max;
}
 
/**
* Crops the string so its length is under the max allowed, and also
* adds the specified suffix to the end of the string
* Ensures string is not abbreviated prematurely (e.g. half way through a word)
*
* @param string {string} user specified string
* @param max {number} the maximum allowed characters before abbreviating
* @param suffix {string} the characters used to end the string
* @return {string} the abbreviated string
*/
function crop (string, max, suffix) {
var abbr = '';
var counter = 0;
var len;
var ending_space = /[ ]$/g;
 
string = string.split(' ');
len = string.length;
 
while (counter < len) {
// individual characters of a string can be accessed via bracket notation
if ((abbr + string[counter]).length < max) {
abbr += string[counter] + ' ';
} else {
break;
}
 
counter++;
}
 
return abbr.replace(ending_space, '') + suffix;
}
 
// Example usage:
console.log(abbreviate('this is my very long string that probably should be cropped at some point soon', 11, '!!!'));
console.log(abbreviate('this is my very long string that probably should be cropped at some point soon', 20));

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.