Skip to content

Instantly share code, notes, and snippets.

@phiggins42
Created August 1, 2011 12:07
Show Gist options
  • Save phiggins42/1118017 to your computer and use it in GitHub Desktop.
Save phiggins42/1118017 to your computer and use it in GitHub Desktop.
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