Skip to content

Commit

Permalink
Postfix param for 'words' filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernardo Loureiro authored and BernardoSilva committed Oct 31, 2015
1 parent f70a38c commit db16501
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ angular.module('truncate', [])
};
})
.filter('words', function () {
return function (input, words) {
return function (input, words, postfix) {
if (isNaN(words)) return input;
if (words <= 0) return '';
if (typeof postfix === 'undefined') postfix = '…';
if (input) {
var inputWords = input.split(/\s+/);
if (inputWords.length > words) {
input = inputWords.slice(0, words).join(' ') + '…';
input = inputWords.slice(0, words).join(' ') + postfix;
}
}
return input;
Expand Down
12 changes: 12 additions & 0 deletions test/filtersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ describe('truncate', function () {
expect(wordFilter('abc def ghhi jkl mno pqr stu vw xyz', 25)).toEqual('abc def ghhi jkl mno pqr stu vw xyz');
});

it('should trim these down without ellipsis postfix', function () {
expect(wordFilter('abc def ghhi jkl mno pqr stu vw xyz', 5, '')).toEqual('abc def ghhi jkl mno');
});

it('should trim these down with specific postfix', function () {
expect(wordFilter('abc def ghhi jkl mno pqr stu vw xyz', 5, '...')).toEqual('abc def ghhi jkl mno...');
});

it('should trim these down with blank postfix', function () {
expect(wordFilter('abc def ghhi jkl mno pqr stu vw xyz', 5, '')).toEqual('abc def ghhi jkl mno');
});

});

describe('splitcharacters', function () {
Expand Down

0 comments on commit db16501

Please sign in to comment.