0.2.1 • Published 2 months ago

@stdlib/string-truncate-middle v0.2.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 months ago

truncateMiddle

NPM version Build Status Coverage Status

Truncate a string in the middle to a specified length.

Installation

npm install @stdlib/string-truncate-middle

Usage

var truncateMiddle = require( '@stdlib/string-truncate-middle' );

truncate( str, len[, seq] )

Truncates a string in the middle to a specified length.

var out = truncateMiddle( 'beep boop', 7 );
// returns 'be...op'

By default, the truncated string uses the replacement sequence '...'. To customize the replacement sequence, provide a seq argument:

var out = truncateMiddle( 'beep boop', 7, '!' );
// returns 'bee!oop'

out = truncateMiddle( 'beep boop', 7, '!!!' );
// returns 'be!!!op'

Examples

var truncateMiddle = require( '@stdlib/string-truncate-middle' );

var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
var out = truncateMiddle( str, 15 );
// returns 'Lorem ... elit.'

str = 'To be or not to be, that is the question';
out = truncateMiddle( str, 19, '|' );
// returns 'To be or | question'

str = 'The quick fox jumps over the lazy dog.';
out = truncateMiddle( str, 28, '...' );
// returns 'The quick fox...he lazy dog.'

str = '🐺 Wolf Brothers 🐺';
out = truncateMiddle( str, 7 );
// returns '🐺 ... 🐺'

str = '🐺 Wolf Pack 🐺';
out = truncateMiddle( str, 7, '🐺🐺🐺' );
// returns '🐺 🐺🐺🐺 🐺'

See Also


Notice

This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.

Community

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2024. The Stdlib Authors.