Truncates a string to a given length, then append either a horizontal ellipsis html entity (...) or other given suffix
-
-
Save atomantic/6160612 to your computer and use it in GitHub Desktop.
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
/** | |
* shorten a string, adding a suffix in place of excessive characters | |
* default suffix is an html encoded ellipsis '…' | |
* | |
* @param {number} len The lenth of the string to keep (not counting suffix) | |
* @param {string} suffix The suffix to append (e.g. '...<a>read more</a>') | |
*/ | |
String.prototype.trunc = function(len,suffix) { | |
return this.length > len ? this.slice(0, len) + (suffix||'…') : this.toString(); | |
}; |
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
String.prototype.trunc=function(a,b){return this.length>a?this.slice(0,a)+(b||"…"):this.toString()}; |
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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
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
{ | |
"name": "String.prototype.truncate", | |
"description": "truncate a string, adding a suffix in place of excessive characters. Default suffix is an html encoded ellipsis '…'", | |
"keywords": [ | |
"string", | |
"truncate", | |
"ellipsis", | |
"ellipsize", | |
"short" | |
] | |
} |
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
<!DOCTYPE html> | |
<title>String prototype truncate method</title> | |
<ul> | |
<li>exists: <span id="proto"></span></li> | |
<li>0: <span id="zero"></span></li> | |
<li>1: <span id="one"></span></li> | |
<li>10: <span id="ten"></span></li> | |
<li>all: <span id="beyond"></span></li> | |
<li>custom: <span id="custom"></span></li> | |
</ul> | |
<script> | |
// the func! | |
String.prototype.trunc=function(a,b){return this.length>a?this.slice(0,a)+(b||"…"):this.toString()}; | |
// a test string | |
var str = 'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using Content here, content here, making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorem ipsum will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose injected humour and the like', | |
pass = '<span style="color:green">PASS</span>', | |
fail = '<span style="color:red">FAIL</span>'; | |
document.getElementById( "proto" ).innerHTML = (typeof str.trunc==='function' ? pass : fail); | |
var zero = str.trunc(0), | |
one = str.trunc(1), | |
ten = str.trunc(10), | |
beyond = str.trunc(5000), | |
custom = str.trunc(1,'...<a>read more</a>'); | |
document.getElementById( "zero" ).innerHTML = (zero === '…' ? pass : fail) + ': ' + zero; | |
document.getElementById( "one" ).innerHTML = (one === 'I…' ? pass : fail) + ': ' + one; | |
document.getElementById( "ten" ).innerHTML = (ten === 'It is a lo…' ? pass : fail) + ': ' + ten; | |
document.getElementById( "beyond" ).innerHTML = (beyond === str ? pass : fail) + ': ' + beyond; | |
document.getElementById( "custom" ).innerHTML = (custom === 'I...<a>read more</a>' ? pass : fail) + ': ' + custom; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since UTF-8 characters are allowed in 140byt.es challenges, you can use "…" as a single char. On the other hand, I would advise against overwriting existent prototypes, as it could introduce errors: