Skip to content

Instantly share code, notes, and snippets.

@atomantic
Forked from 140bytes/LICENSE.txt
Last active December 20, 2015 16:19
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 atomantic/6160612 to your computer and use it in GitHub Desktop.
Save atomantic/6160612 to your computer and use it in GitHub Desktop.

140byt.es - String.prototype.trunc

Truncates a string to a given length, then append either a horizontal ellipsis html entity (...) or other given suffix

/**
* 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||'&hellip;') : this.toString();
};
String.prototype.trunc=function(a,b){return this.length>a?this.slice(0,a)+(b||"&hellip;"):this.toString()};
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.
{
"name": "String.prototype.truncate",
"description": "truncate a string, adding a suffix in place of excessive characters. Default suffix is an html encoded ellipsis '&hellip;'",
"keywords": [
"string",
"truncate",
"ellipsis",
"ellipsize",
"short"
]
}
<!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||"&hellip;"):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 === '&hellip;' ? pass : fail) + ': ' + zero;
document.getElementById( "one" ).innerHTML = (one === 'I&hellip;' ? pass : fail) + ': ' + one;
document.getElementById( "ten" ).innerHTML = (ten === 'It is a lo&hellip;' ? 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>
@atk
Copy link

atk commented Aug 6, 2013

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:

''.trunc||(String.prototype.trunc=function(a,b){return this.length>a?this.slice(0,a)+(b||"…"):''+this});

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