Skip to content

Instantly share code, notes, and snippets.

@enjalot
Created June 21, 2013 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enjalot/5833357 to your computer and use it in GitHub Desktop.
Save enjalot/5833357 to your computer and use it in GitHub Desktop.
shortening
{"description":"shortening","endpoint":"","display":"div","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"thumbnail":"http://i.imgur.com/MfOSjSI.png"}
var display = d3.select("#display");
var length = 14;
var ellipsis = "…";
var abcs = "abcdefghijklmnopqrstuvwxyz".split('');
var letterScale = d3.scale.ordinal()
.domain(d3.range(26))
.range(abcs)
var data = d3.range(100).splice(5, 39)
.map(function(d) {
var s = '';
for(var j = 0; j < d; j++) {
s += letterScale(j);
}
return s
})
display.selectAll("p")
.data(data)
.enter()
.append("p")
.text(function(d) { return shorten(d, length) + " | " + d.length + " " + d })
function shorten(s, length) {
if(!s) return "";
var len = s.length;
if(len <= length) return s;
var diff = len - length;
var hlen = Math.floor(len/2);
var hdiff = Math.floor(diff/2);
var left = s.slice(0, hlen - hdiff);
var right = s.slice(hlen + hdiff + (len & 1), len);
return left + ellipsis + right;
}
#display {
overflow: scroll;
}
p {
font-family: 'Courier New', monospace;
line-height: 7px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment