Created
August 1, 2011 12:07
-
-
Save phiggins42/1118017 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
exports.ellipsis = function ellipsis(str, chunk, elip){ | |
// summary: Ellipsis some text. Break `str` into `chunk` sized | |
// bites (bytes?), breaking only on full words within the `chunk` size | |
// | |
// str: String | |
// The string to break apart. | |
// chunk: Integer: | |
// The size of the chunk | |
// elip: String? | |
// Optional ellipsis marker. defaults to "..." | |
// | |
// returns: Array | |
// An Array containing all the broken up parts of the `str`, | |
// with `elip` delimeter included. | |
if(str.length <= chunk){ return [str]; } | |
if(!elip){ elip = "..."; } | |
var part = str.substring(0, chunk - elip.length).replace(/\w+$/, ""), | |
idx = part.length, | |
sub = [part.trim() + elip], | |
rest = str.substring(idx, str.length) | |
; | |
// recursion FTW: | |
sub.push.apply(sub, ellipsis(rest, chunk, elip)); | |
return sub; // Array | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment