truncate-middle
v1.0.6
Published
Truncates in the middle.
Downloads
19,898
Readme
truncate-middle
There are cases where important information is at the end of the string and truncating the end isn't helpful.
Example use cases
var book1 = "America Again: Re-becoming the Greatness We Never Weren't by Stephen Colbert (978-0446583978)";
var truncateMiddle = require('truncate-middle');
var display = truncateMiddle(book1, 30, 16, '...');
// You get 'America Again: Re-becoming the...(978-0446583978)'
Usage
Install from npm
.
npm install --save truncate-middle
Requiring in JavaScript
var truncateMiddle = require('truncate-middle');
truncateMiddle('the quick brown', 5, 3, '...');
// Result:
// 'the q...own'
Defaults:
truncateMiddle('the quick brown');
// Result:
// 'the quick brown'
Specifying a front length of 5:
truncateMiddle('the quick brown', 5);
// Result:
// 'the q…'
Specifying a front length of 5 and back length of 3:
truncateMiddle('the quick brown', 5, 3);
// Result:
// 'the q…own'
When there isn't anything to truncate:
truncateMiddle('the quick brown', 50, 50);
// Result:
// 'the quick brown'
Function signature
truncateMiddle(str, frontLen, backLen, truncateStr)
- str (string): String to be truncated
- frontLen (number): Number of characters to be remained in front.
- backLen (number): Number of characters to be remained at the back.
- truncateStr (string): String that is replaced the truncated portion. Defaults to
…
Return is always the truncated string.
License
MIT