Skip to content

Instantly share code, notes, and snippets.

@silasrm
Last active August 29, 2015 14:04
Show Gist options
  • Save silasrm/1a2efecc12d06648e7dd to your computer and use it in GitHub Desktop.
Save silasrm/1a2efecc12d06648e7dd to your computer and use it in GitHub Desktop.
Filtro que trunca uma string e adiciona uma string no final (por default sendo '...')
// ########################################################################### //
// ########################################################################### //
// - Truncate - //
// ########################################################################### //
// ########################################################################### //
/**
* Truncate Filter
* @Param text
* @Param length, default is 10
* @Param end, default is "..."
* @return string
*/
/**
* Usage
*
* var myText = "This is an example.";
*
* {{myText|truncate}}
* {{myText|truncate:5}}
* {{myText|truncate:25:" ->"}}
* Output
* "This is..."
* "Th..."
* "This is an e ->"
*
*/
define(
[
'app',
],
function(app) {
app
.lazy
.filter('truncate', function () {
return function (text, length, end) {
if(text == null)
return '';
if (isNaN(length))
length = 10;
if (end === undefined)
end = "...";
if (text.length <= length || text.length - end.length <= length) {
return text;
} else {
return String(text).substring(0, length-end.length) + end;
}
};
});
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment