Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

truncate-middle

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

truncate-middle

Truncates in the middle.

  • 1.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.3K
increased by108.33%
Maintainers
1
Weekly downloads
 
Created
Source

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

FAQs

Package last updated on 07 Oct 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc