truncate-middle
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/truncate-middle package

1.0.6 • Public • Published

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…'

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)
  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 …

Return is always the truncated string.

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i truncate-middle

Weekly Downloads

3,981

Version

1.0.6

License

MIT

Unpacked Size

86.1 kB

Total Files

9

Last publish

Collaborators

  • kahwee