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

Quick Take

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

assert.deepEqual(trimSpaces("  aaa   "), {
  res: "aaa",
  ranges: [
    [0, 2],
    [5, 8],

assert.deepEqual(trimSpaces("   \t  zz   \n    "), {
  res: "\t  zz   \n",
  ranges: [
    [0, 3],
    [12, 16],


trimSpaces(str, [opts])

In other words, it's a function which takes two input arguments, second-one being optional (marked by square brackets).

API - Function's Input

Input argument Key value's type Obligatory? Description
input String yes Input string you want to trim some way
opts Plain object no An Optional Options Object. See below for its API.

If input arguments are supplied have any other types, an error will be thrown.

Optional Options Object

An Optional Options Object's key Type of its value Default Description
classicTrim Boolean false If set to true, trimming becomes the same as String.trim().
cr Boolean false Should we trim the carriage returns (CR)
lf Boolean false Should we trim the line breaks (LF)
tab Boolean false Should we trim tabs
space Boolean true Should we trim spaces
nbsp Boolean false Should we trim raw non-breaking spaces

Here is the default options object in one place:

classicTrim: false,
cr: false,
lf: false,
tab: false,
space: true,
nbsp: false

API - Function's Output

Since v.2, the output is a plain object:

Key name Key value's type Description
res String or zero or more characters Result string after trimming.
ranges Array of zero or more arrays If we trimmed anything, each slice range will be added into this array.


String.trim() returns string but sometimes you need just ranges of what would be trimmed, to merge them into compiled ranges array and to process later, along everything else. In those cases, use opts.classicTrim. If you need just string value, it's not worth to use this function as a substitute for String.trim() for performance reasons.


See it in the monorepo opens in a new tab, on GitHub.


To report bugs or request features or assistance, raise an issue on GitHub opens in a new tab.

Any code contributions welcome! All Pull Requests will be dealt promptly.


MIT opens in a new tab

Copyright © 2010–2021 Roy Revelt and other contributors

Related packages:

📦 ranges-apply 6.0.1
Take an array of string index ranges, delete/replace the string according to them
📦 ranges-push 6.0.1
Gather string index ranges
📦 string-collapse-leading-whitespace 6.0.1
Collapse the leading and trailing whitespace of a string
📦 string-strip-html 9.0.1
Strips HTML tags from strings. No parser, accepts mixed sources
📦 string-apostrophes 2.0.1
Comprehensive, HTML-entities-aware tool to typographically-correct the apostrophes and single/double quotes
📦 string-process-comma-separated 3.0.1
Extracts chunks from possibly comma or whatever-separated string
📦 string-overlap-one-on-another 3.0.1
Lay one string on top of another, with an optional offset