Skip to content

injectWordBreaks

Adds word-break opportunities to a string so it can wrap cleanly in narrow UI containers such as side panels or table cells.

Invisible zero-width spaces (\u200B) are inserted at meaningful boundaries — camelCase splits, path separators, token edges — while protected spans (URLs, emails, HTML) and atomic numeric values (-0.1%, 12ms, 1e-3) are never broken. The visible text content is unchanged.

Available since v2.0.0

import { injectWordBreaks } from '@helpers4/string';
injectWordBreaks(str: string): string
ParameterTypeDescription
strstringThe string to process.

string — The string with word-break opportunities injected at meaningful boundaries.

Inserts ZWS at each camelCase boundary so a long identifier can wrap in a narrow column.

injectWordBreaks('getUserProfileData')
// => 'get\u200BUser\u200BProfile\u200BData'

The comma attaches to the left token so a line never starts with a comma.

injectWordBreaks('foo,bar')
// => 'foo,\u200Bbar'

Signed decimals and numbers with units (-0.1%, 12ms, -2.4E+6) are never split.

injectWordBreaks('-0.1%')
// => '-0.1%'   (unchanged — atomic value)

Slashes become wrap points so a long path can break at each component.

injectWordBreaks('path/to/my_file')
// => 'path\u200B/\u200Bto\u200B/\u200Bmy_file'

URLs are D0-protected spans — no ZWS is inserted inside or adjacent to them.

injectWordBreaks('https://example.com/foo/bar')
// => 'https://example.com/foo/bar'   (unchanged)

View source on GitHub