Last active
August 29, 2015 14:04
-
-
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 '...')
This file contains hidden or 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
// ########################################################################### // | |
// ########################################################################### // | |
// - 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