[ Launch: shortening ] 5833357 by enjalot
-
-
Save enjalot/5833357 to your computer and use it in GitHub Desktop.
shortening
This file contains 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
{"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"} |
This file contains 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
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; | |
} |
This file contains 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
#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