Skip to content

Commit

Permalink
Refactored not to strip tag by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Serby committed May 30, 2012
1 parent 4f4ab1e commit 19e6500
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
## Usage

```js
var truncate = require('trunky').truncate;
var truncate = require('trunky').truncateWithEllipsis;

truncate('Hello world', 8); // Hello…
truncate('<b>Hello</b> world', 8); // Hello…
Expand All @@ -23,7 +23,7 @@ truncate('Hello world', 8, ' etc.'); // Hello etc.
```

## Credits
[Paul Serby](https://github.com/serby/) follow me on [twitter](http://twitter.com/PabloSerbo)
[Paul Serby](https://github.com/serby/) follow me on [twitter](http://twitter.com/serby)

## Licence
Licenced under the [New BSD License](http://opensource.org/licenses/bsd-license.php)
15 changes: 10 additions & 5 deletions lib/trunky.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,26 @@ function truncateWithEllipsis(value, length, ellipsis) {
ellipsis = '\u2026';
}

var response = stripTags(value).trim()
var response = value
, position;


if (value.length > length) {
response = response.substring(0, length);
if (response.length > length) {
response = value.substring(0, length);
position = response.lastIndexOf(' ');
if (position <= 0) {
position = length;
}
return response.substring(0, position) + ellipsis;
} else {
return value;
return response;
}
}

function strippedTruncateWithEllipsis(value, length, ellipsis) {
return truncateWithEllipsis(stripTags(value), length, ellipsis);
}

module.exports.stripTags = stripTags;
module.exports.truncateWithEllipsis = truncateWithEllipsis;
module.exports.truncateWithEllipsis = truncateWithEllipsis;
module.exports.strippedTruncateWithEllipsis = strippedTruncateWithEllipsis;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
],
"name": "trunky",
"description": "String truncation",
"version": "0.1.0",
"version": "0.2.0",
"tags": ["string", "truncation", "text", "manipulation"],
"repository": {
"type": "git",
Expand Down
40 changes: 34 additions & 6 deletions test/trunky.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,14 @@ describe('trunky', function() {
trunky.truncateWithEllipsis('This is a test', 10).should.equal('This is a\u2026');
});

it('should truncate HTML simple string', function() {
trunky.truncateWithEllipsis('<p>This</p> <strong>is a</strong> <b>test</b>', 10).should.equal('This is a\u2026');
it('should truncate HTML without stripping ', function() {
trunky.truncateWithEllipsis('<p>This</p> <strong>is a</strong> <b>test</b>', 10).should.equal('<p>This</p\u2026');
});

it('should truncate a simple string with a custom ellipsis string', function() {
trunky.truncateWithEllipsis('This is a test', 10, '...').should.equal('This is a...');
});

it('should truncate HTML simple string with a custom ellipsis string', function() {
trunky.truncateWithEllipsis('<p>This</p> <strong>is a</strong> <b>test</b>', 10, '...').should.equal('This is a...');
});

it('should truncate at the desired length if no word breaks are found', function () {
trunky.truncateWithEllipsis('abcdefghijklmnopqrstuvwxyz', 10).should.have.lengthOf(11);
});
Expand All @@ -57,4 +53,36 @@ describe('trunky', function() {
});

});

describe('#strippedTruncateWithEllipsis()', function() {

it('should leave an empty string unchanged', function() {
trunky.strippedTruncateWithEllipsis('', 0).should.equal('');
});

it('should not truncate a string shorter than the desired length', function() {
trunky.strippedTruncateWithEllipsis('This is a test', 20).should.equal('This is a test');
});

it('should truncate a simple string', function() {
trunky.strippedTruncateWithEllipsis('This is a test', 10).should.equal('This is a\u2026');
});

it('should truncate HTML simple string', function() {
trunky.strippedTruncateWithEllipsis('<p>This</p> <strong>is a</strong> <b>test</b>', 10).should.equal('This is a\u2026');
});

it('should truncate a simple string with a custom ellipsis string', function() {
trunky.strippedTruncateWithEllipsis('This is a test', 10, '...').should.equal('This is a...');
});

it('should truncate HTML simple string with a custom ellipsis string', function() {
trunky.strippedTruncateWithEllipsis('<p>This</p> <strong>is a</strong> <b>test</b>', 10, '...').should.equal('This is a...');
});

it('should return plain text when HTML is given but too short for truncation', function() {
trunky.strippedTruncateWithEllipsis('<p>This</p> <strong>is a</strong> test', 20).should.equal('This is a test');
});

});
});

0 comments on commit 19e6500

Please sign in to comment.