Skip to content

Commit

Permalink
make truncate.right not broken
Browse files Browse the repository at this point in the history
fix # 2
  • Loading branch information
jonathanong committed Feb 25, 2014
1 parent 72b550d commit 4bbb95b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
14 changes: 9 additions & 5 deletions index.js
Expand Up @@ -23,13 +23,17 @@ function truncate(str, chars, suffix, left){
if (!str || !str.length || str.length <= chars) return str;

left = left !== false;
var mod = false !== left ? 0 : 1;
var end = !mod ? chars : str.length - chars;
var new_str = str.substring(!mod ? 0 : str.length, end);
var perfect = str[end] == ' ';
var mod = left ? 0 : 1;
var end = mod ? str.length - chars : chars;
var new_str = str.substring(mod ? str.length : 0, end);
var perfect = left
? str[end] == ' '
: (str[end - 1] == ' ');

if (!perfect) {
new_str = new_str.replace(left ? /\s*[^\s|.]*$/ : /^[^\s|.]*\s*/, '');
new_str = new_str.replace(left
? /\s*[^\s|.]*$/
: /^[^\s|.]*\s*/, '');
}
return left ? new_str + suffix : suffix + new_str;
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -13,9 +13,11 @@
"mocha": "*",
"should": "*"
},
"main": "index",
"repository": {
"type": "git",
"url": "https://github.com/component/truncate.git"
},
"scripts": {
"test": "make test"
}
}
8 changes: 4 additions & 4 deletions test/index.js
Expand Up @@ -57,14 +57,14 @@ describe('truncate.right(string, 20)', function(){
})

describe('truncate.right(string, 20, \'... \')', function(){
it('should return a string equal to \'... her lover `Bill`.\'', function(){
truncate.right(killbill, 20, '... ').should.be.equal('... her lover `Bill`.');
it('should return a string equal to \'... by her lover `Bill`.\'', function(){
truncate.right(killbill, 20, '... ').should.be.equal('... by her lover `Bill`.');
});
});

describe('truncate.right(string, 20, \'… \')', function(){
it('should return a string equal to \'… her lover `Bill`.\'', function(){
truncate.right(killbill, 20, '… ').should.be.equal('… her lover `Bill`.');
it('should return a string equal to \'… by her lover `Bill`.\'', function(){
truncate.right(killbill, 20, '… ').should.be.equal('… by her lover `Bill`.');
});
});

Expand Down

0 comments on commit 4bbb95b

Please sign in to comment.