Skip to content

truncateMiddle()

truncateMiddle(input, maxLength, options?): string

Defined in: utils/string/truncateMiddle.ts:36

Truncate a string in the middle, preserving the start and end.

Useful for displaying long IDs, file paths, or URLs where both beginning and end segments matter.

Parameters

input

string

The string to truncate.

maxLength

number

Maximum total length of the result.

options?

TruncateMiddleOptions = {}

Optional configuration.

Returns

string

Truncated string, or the original string if it fits.

Examples

ts
truncateMiddle('abcdefghijklmnop', 10); // 'abcde…mnop'
truncateMiddle('short', 10);            // 'short'
ts
// Custom ellipsis and very small maxLength
truncateMiddle('abcdef', 4, { ellipsis: '...' }); // 'abcd'

Released under the MIT License.