Skip to content

Instantly share code, notes, and snippets.

@yoko
Created September 9, 2008 09:36
Show Gist options
  • Save yoko/9648 to your computer and use it in GitHub Desktop.
Save yoko/9648 to your computer and use it in GitHub Desktop.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>String.prototype.truncate</title>
<link rel="stylesheet" type="text/css" href="http://github.com/jquery/qunit/raw/master/qunit/qunit.css"/>
<script type="text/javascript" src="http://github.com/jquery/qunit/raw/master/qunit/qunit.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2/swfobject.js"></script>
<script type="text/javascript" src="String.prototype.truncate.js"></script>
</head>
<body>
<h1 id="qunit-header">String.prototype.truncate</h1>
<h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<script type="text/javascript">
module('String.prototype.truncate');
test('truncate', 3, function() {
var str = 'abcdefghij';
equals(str.truncate(6), 'abcdef...');
equals(str.truncate(10), 'abcdefghij', '');
equals(str.truncate(0), '...');
});
</script>
</body>
</html>
String.prototype.truncate = function(to) {
var str = this.toString();
return str.length > to ? str.slice(0, to)+'...' : str;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment