Skip to content
This repository has been archived by the owner on May 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #4 from marianosimone/fix-ellipsis
Browse files Browse the repository at this point in the history
fix ellipsis limit and test
  • Loading branch information
andrey-p committed Sep 9, 2015
2 parents 5e7d5a3 + 0491990 commit eaf6dcb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 3 additions & 3 deletions template_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
*
*/
Handlebars.registerHelper("ellipsis", function(text, limit, options) {
if (!text) { return; }
if (!text) { return ""; }

// If we get a number, convert it to a string.
// We convert things to a string because [object Number] doesn't have the `split` method.
Expand All @@ -224,8 +224,8 @@
words = text.split(" ");

for(var i = 0; i < words.length; i++) {
count += words[i].length + 1;
if(count < limit) {
count += words[i].length + (i < words.length-1 ? 1 : 0);
if(count <= limit) {
result.push(words[i]);
}
}
Expand Down
5 changes: 4 additions & 1 deletion test/ellipsis.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
describe('Handlebars.helpers.ellipsis', function() {

var tests = [
{ text: 'The quick brown fox', len: 12, expected: 'The quick...' }
{ text: '', len: 1, expected: '' },
{ text: 'The quick brown fox', len: 12, expected: 'The quick...' },
{ text: 'This is shorter than the limit', len: 42, expected: 'This is shorter than the limit' },
{ text: 'This is as long as the limit', len: 28, expected: 'This is as long as the limit' },
];

it('should truncate words correctly', function() {
Expand Down

0 comments on commit eaf6dcb

Please sign in to comment.