Skip to content

Instantly share code, notes, and snippets.

@fupslot
Created August 24, 2015 13:10
Show Gist options
  • Save fupslot/b48c91cc56027ccda8eb to your computer and use it in GitHub Desktop.
Save fupslot/b48c91cc56027ccda8eb to your computer and use it in GitHub Desktop.
abbriviate, ellipsis, filter, anguar
angular
.module('myProject', [])
.filter('abbreviate', function () {
// Hello World | abbreviate:6:2 //-> 'Hello...Wo'
// Hello World | abbreviate:5 //-> 'Hello...'
return function(str, num, prefix) {
if (str == null) { str = ''; }
if (num == null) { num = str.length; }
if (prefix == null) { prefix = 0; }
if (num >= str.length) { return str; }
num = num - prefix - 1;
if (num < 0) { num = 0; }
return str.substr(0, num) + '\u2026' + str.substr(-prefix, prefix);
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment