Skip to content

Instantly share code, notes, and snippets.

@madrobby
Last active December 12, 2015 04:38
Show Gist options
  • Save madrobby/4716024 to your computer and use it in GitHub Desktop.
Save madrobby/4716024 to your computer and use it in GitHub Desktop.
String.prototype.midTruncate = function(length, seperator){
var left, right;
length = length || 30, left = Math.ceil(length/2), right = Math.floor(length/2);
seperator = seperator || '…';
if (this.length<=(length+1)) return this.toString();
return this.slice(0,left).strip()+seperator+this.slice(-right,this.length).strip();
};
@madrobby
Copy link
Author

madrobby commented Feb 5, 2013

"abcdefghijklmnopqrstuvwxyz".midTruncate(5)
// => "abc…yz"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment