Truncating Text Strings

This function trucates a text string using either end or center ellipsis (...) to a specified maximum length. If the string is shorter than the maximum length the string will be returned unaltered.

function truncate(str, len, mode) { if (str.length < len) { return str; } else { if (mode == 'center') { return str.substr(0, Math.floor(len / 2) - 2) + '...' + str.substr(str.length - Math.ceil(len / 2) + 1); } else if (mode == 'end') { return str.substr(0, len - 3) + '...'; } else { return str; } 	} }