Skip to content

Commit 84b16b5

Browse files
committed
Merge pull request #31 from palpalani/master
Add truncate filter for characters and words.
2 parents 7244ded + bcef538 commit 84b16b5

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
(function(){
2+
'use strict';
3+
4+
angular.module('app.filters').filter('truncateCharacters', function () {
5+
return function (input, chars, breakOnWord) {
6+
if (isNaN(chars)) {
7+
return input;
8+
}
9+
if (chars <= 0) {
10+
return '';
11+
}
12+
if (input && input.length > chars) {
13+
input = input.substring(0, chars);
14+
15+
if (!breakOnWord) {
16+
var lastspace = input.lastIndexOf(' ');
17+
// Get last space
18+
if (lastspace !== -1) {
19+
input = input.substr(0, lastspace);
20+
}
21+
} else {
22+
while (input.charAt(input.length-1) === ' ') {
23+
input = input.substr(0, input.length - 1);
24+
}
25+
}
26+
return input + '...';
27+
}
28+
return input;
29+
};
30+
});
31+
})();

angular/filters/truncate_words.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
(function(){
2+
'use strict';
3+
4+
angular.module('app.filters').filter('truncateWords', function () {
5+
return function (input, words) {
6+
if (isNaN(words)) {
7+
return input;
8+
}
9+
if (words <= 0) {
10+
return '';
11+
}
12+
if (input) {
13+
var inputWords = input.split(/\s+/);
14+
if (inputWords.length > words) {
15+
input = inputWords.slice(0, words).join(' ') + '...';
16+
}
17+
}
18+
return input;
19+
};
20+
});
21+
})();

0 commit comments

Comments
 (0)