Skip to content

trimStart

Trims leading characters from a string, at a configurable level of aggressiveness (see TrimMode). Defaults to 'whitespace', which behaves exactly like String.prototype.trimStart.

Unlike the native trimStart, non-breaking spaces (NBSP, FIGURE SPACE, NARROW NO-BREAK SPACE) are only stripped for 'separator' mode and above - pass 'wrappable' to preserve them, since their whole purpose is to resist being treated as a break point.

Available since v3.0.6

import { trimStart } from '@helpers4/string';
// or, from the all-in-one package (same code, one install):
import { trimStart } from 'helpers4/string';
trimStart(input: string, mode?: TrimMode): string
ParameterTypeDescription
inputstringThe string to trim.
modeTrimModeHow aggressively to trim. Defaults to `‘whitespace’`. (optional)

string — The trimmed string, or the input itself when null/undefined.

Default mode matches String.prototype.trimStart

Section titled “Default mode matches String.prototype.trimStart”

With no mode argument, behaves exactly like the native trimStart.

trimStart('   Hello')
// => 'Hello'

Preserve non-breaking spaces with ‘wrappable’ mode

Section titled “Preserve non-breaking spaces with ‘wrappable’ mode”

A leading non-breaking space (NBSP) is kept, since its purpose is to resist being trimmed.

const NBSP = String.fromCharCode(160);
trimStart(NBSP + 'Hello', 'wrappable')
// => NBSP + 'Hello' (unchanged)

Strip non-breaking spaces too with ‘separator’ mode

Section titled “Strip non-breaking spaces too with ‘separator’ mode”

A wider mode than the default (whitespace) is not needed for NBSP — separator already covers it.

const NBSP = String.fromCharCode(160);
trimStart(NBSP + 'Hello', 'separator')
// => 'Hello'

How aggressively trim/trimStart/trimEnd strip characters. The four levels are strictly nested — each one strips everything the previous level does, plus more — from narrowest to widest:

ModeStripsExample character(s)
'wrappable'Only characters where a line can break: ASCII whitespace, the breakable subset of Unicode’s Space_Separator category, and the mandatory line/paragraph separators. Preserves non-breaking spaces.space, tab, newline, EM SPACE
'separator''wrappable', plus the non-breaking subset — the full Unicode Space_Separator (Zs) category.NBSP, FIGURE SPACE, NARROW NO-BREAK SPACE
'whitespace''separator', plus U+FEFF (BOM / zero-width no-break space) — exactly what String.prototype.trim already strips. Default.everything above, plus BOM
'unicode''whitespace', plus genuinely invisible zero-width Format (Cf) characters that String.prototype.trim does not strip.ZERO WIDTH SPACE, ZERO WIDTH JOINER, WORD JOINER

Use 'wrappable' when a non-breaking space is meaningful and must survive (e.g. gluing a number to its unit before truncating). Use 'unicode' to also clean up invisible characters accidentally left over from a paste — neither of those is what String.prototype.trim does, which is exactly why this type has more than one level instead of a boolean flag.

type TrimMode = 'wrappable' | 'separator' | 'whitespace' | 'unicode'

View source on GitHub