Skip to Content
  • Website
Codsen
  • Home
  • Open Source
  • Articles
  • About

prevOpen Source→string-trim-spaces-onlynext

string-trim-spaces-only5.3.1

Like String.trim() but you can choose granularly what to trim

Downloads per monthChangelogMIT Licenselibera manifesto
  • the top
  • Installation
  • Quick Take
  • Examples
  • PURPOSE
  • API — TRIMSPACES()
  • OPTS — CLASSICTRIM
  • API — DEFAULTS
  • API — VERSION
  • Changelog

No 3rd party dependencies. All dependencies and devDependencies, checked recursively, are Codsen packages.

Permalink to InstallationInstallation

Permalink to Quick TakeQuick Take

Permalink to ExamplesExamples

  • Include carriage returns in the configured trim set
  • Trim every JavaScript whitespace character
  • Trim tabs but preserve ordinary spaces at the edges
  • Include line feeds in the configured trim set
  • Preserve non-space whitespace at the trimmed boundaries
  • Include raw non-breaking spaces in the configured trim set
  • Return an empty ranges array when the value needs no trimming
  • Include tabs in the configured trim set

Purpose

trimSpaces() removes configurable whitespace from the start and end of a string. It returns both the trimmed string and the ranges removed from the original input. By default, it trims only ordinary spaces (U+0020).

import { trimSpaces } from "string-trim-spaces-only";

console.log(trimSpaces("  value   "));
// {
//   res: "value",
//   ranges: [[0, 2], [7, 10]]
// }

API — trimSpaces()

The main function trimSpaces() is imported like this:

The function accepts a required string and an optional, partial options object:

ArgumentTypeRequiredDescription
str
Type: String
strStringyesString to trim.
opts
Type: Partial options object
optsPartial options objectnoWhitespace characters to trim.

Passing a non-string first argument throws an error.

Options

KeyTypeDefaultDescription
classicTrim
Type: Boolean
Default: false
classicTrimBooleanfalseUse the same whitespace set as String.trim() and ignore the granular flags.
cr
Type: Boolean
Default: false
crBooleanfalseTrim carriage returns (U+000D).
lf
Type: Boolean
Default: false
lfBooleanfalseTrim line feeds (U+000A).
tab
Type: Boolean
Default: false
tabBooleanfalseTrim horizontal tabs (U+0009).
space
Type: Boolean
Default: true
spaceBooleantrueTrim ordinary spaces (U+0020).
nbsp
Type: Boolean
Default: false
nbspBooleanfalseTrim non-breaking spaces (U+00A0).

Here are all defaults in one place for copying:

Result

The function returns a plain object:

KeyTypeDescription
res
Type: String
resStringString remaining after the configured whitespace is removed.
ranges
Type: Range[]
rangesRange[]Zero, one, or two end-exclusive [start, end] ranges identifying the removed input slices.

Range offsets use JavaScript UTF-16 string indexes. If no characters are removed, ranges is an empty array. If both ends are trimmed, the leading range comes first.

The ranges can be combined with other Ranges ecosystem operations before the edits are applied:

import { rApply } from "ranges-apply";
import { trimSpaces } from "string-trim-spaces-only";

const input = "  value   ";
const { ranges } = trimSpaces(input);

console.log(rApply(input, ranges));
// => "value"

opts.classicTrim

Set classicTrim to true to use JavaScript’s complete String.trim() whitespace set while still receiving the removed ranges. In this mode, cr, lf, tab, space, and nbsp are ignored.

trimSpaces(" \t\n value \r\n ", { classicTrim: true });
// {
//   res: "value",
//   ranges: [[0, 4], [9, 13]]
// }

If you need only the resulting string, use String.trim() directly. Use this package when you need a custom trim set or the corresponding ranges.

API — defaults

You can import defaults:

It's a plain object:

The main function calculates the options to be used by merging the options you passed with these defaults.

API — version

You can import version:

Permalink to changelogChangelog

Open Changelog
↑ back to top
prev next

Copyright

All rights reserved © Roy Revelt 2026
All our open source packages are under MIT licenceopens in a new tab

Activities

🐛 See a bug? Raise an issueopens in a new tab
💘 Check out the Indiewebopens in a new tab and Libera manifestoopens in a new tab