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

prevOpen Source→string-trim-spaces-onlynext

string-trim-spaces-only5.2.0

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

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

Installation

Quick Take

Examples

  • 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

API — trimSpaces()

The main function trimSpaces() is imported like this:

It’s a function which takes two input arguments:

Input argumentTypeObligatoryDescription
input
Type: String
Obligatory: yes
inputStringyesInput string you want to trim some way
opts
Type: Plain object
Obligatory: no
optsPlain objectnoAn Optional Options Object.

The Optional Options Object has the following shape:

KeyTypeDefaultDescription
classicTrim
Type: Boolean
Default: false
classicTrimBooleanfalseIf set to true, trimming becomes the same as String.trim().
cr
Type: Boolean
Default: false
crBooleanfalseTrim the carriage returns (CR)?
lf
Type: Boolean
Default: false
lfBooleanfalseTrim the line breaks (LF)?
tab
Type: Boolean
Default: false
tabBooleanfalseTrim tabs?
space
Type: Boolean
Default: true
spaceBooleantrueTrim spaces?
nbsp
Type: Boolean
Default: false
nbspBooleanfalseTrim raw non-breaking spaces?

Here are all defaults in one place for copying:

The function will return a plain object:

KeyTypeDescription
res
Type: String or zero or more characters
resString or zero or more charactersResult string after trimming.
ranges
Type: Array of zero or more arrays
rangesArray of zero or more arraysIf anything was trimmed, each slice range is added into this array.

ranges from the output can be further used in any Ranges ecosystem libraries to enable composable string operations.

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:

opts.classicTrim

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.

Changelog

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