truncate-middle

Truncates in the middle.

Usage no npm install needed!

<script type="module">
  import truncateMiddle from 'https://cdn.skypack.dev/truncate-middle';
</script>

README

truncate-middle

There are cases where important information is at the end of the string and truncating the end isn't helpful.

Build Status Coverage Status npm version Greenkeeper badge

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&hellip;'

Specifying a front length of 5 and back length of 3:

truncateMiddle('the quick brown', 5, 3);
// Result:
//  'the q&hellip;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)
  1. str (string): String to be truncated
  2. frontLen (number): Number of characters to be remained in front.
  3. backLen (number): Number of characters to be remained at the back.
  4. truncateStr (string): String that is replaced the truncated portion. Defaults to &hellip;

Return is always the truncated string.

License

MIT