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

prevOpen Source→string-collapse-white-spacenext

string-collapse-white-space11.2.1

Replace chunks of whitespace with a single spaces

Downloads per monthChangelogMIT Licenselibera manifesto
  • the top
  • Installation
  • Quick Take
  • Examples
  • API — COLLAPSE()
  • API — DEFAULTS
  • API — VERSION
  • OPTS — CB
  • API — CBSCHEMA
  • API — TYPES
  • Changelog

Installation

Quick Take

Examples

  • Minify a chunk of CSS selector
  • Collapse a run containing tabs
  • Replace tabs inside whitespace runs with ordinary spaces
  • Keep at most one consecutive empty line
  • Collapse but preserve one leading space
  • Preserve one space around each line's content
  • Collapse but preserve one trailing space
  • Remove empty lines from multiline text
  • Inspect the ranges used to produce the collapsed result
  • Trim whitespace from the start and end of every line
  • Trim multiline content whose boundaries contain non-breaking spaces
  • Trim raw non-breaking spaces with ordinary whitespace

API — collapse()

The main function collapse() is imported like this:

It’s a function which takes two input arguments:

Input argumentTypeObligatoryDescription
str
Type: String
Obligatory: yes
strStringyesSource string to work upon
opts
Type: Something falsy or a Plain object
Obligatory: no
optsSomething falsy or a Plain objectnoThe Optional Options Object, see below for its API

The Optional Options Object has the following shape:

KeyTypeObligatoryDefaultDescription
trimStart
Type: Boolean
Obligatory: no
Default: true
trimStartBooleannotrueif false, leading whitespace will be just collapsed.
trimEnd
Type: Boolean
Obligatory: no
Default: true
trimEndBooleannotrueif false, trailing whitespace will be just collapsed.
trimLines
Type: Boolean
Obligatory: no
Default: false
trimLinesBooleannofalseif true, every line will be trimmed (all whitespace characters except line breaks CR and LF will be deleted, also non-breaking spaces will be deleted, if trimnbsp is set to true)
trimnbsp
Type: Boolean
Obligatory: no
Default: false
trimnbspBooleannofalseWhen trimming, delete non-breaking spaces too? (If set to true, the answer is “yes”.) This setting also affects trimLines setting above.
removeEmptyLines
Type: Boolean
Obligatory: no
Default: false
removeEmptyLinesBooleannofalseif any line can be trimmed to empty string, it will be removed.
limitConsecutiveEmptyLinesTo
Type: Natural number or zero
Obligatory: no
Default: 0
limitConsecutiveEmptyLinesToNatural number or zerono0Set to 1 or more to allow that many blank lines between content
enforceSpacesOnly
Type: Boolean
Obligatory: no
Default: false
enforceSpacesOnlyBooleannofalseIf enabled, not only consecutive space character chunks will be collapsed but any whitespace character chunks (except line breaks).
cb
Type: Function
Obligatory: no
Default: see below
cbFunctionnosee belowAll output and every whitespace chunk (including single spaces) is fed to it. Whatever you return, gets written to resulting ranges.

Here are all defaults in one place for copying:

Function will return a plain object (Res type above):

It has the following keys:

Key’s nameTypeDescription
result
Type: String
resultStringThe string output where all ranges were applied to it.
ranges
Type: ranges: an array of one or more arrays containing from-to string index ranges OR null
rangesranges: an array of one or more arrays containing from-to string index ranges OR nullFor example, if characters from index 0 to 5 and 30 to 35 were deleted, that would be [[0, 5], [30, 35]]. Another example, if nothing was found, it would put here null.

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.cb

This program implements a callback interface — every reported range is fed to the callback. The default callback is ({ suggested }) => suggested but you can tweak it.

See examples.

When nothing is to be removed, callback will ping suggested key value as null. You can still return any string index range and it will be deleted (array of two elements) or replaced (array of three elements). Learn more about ranges notation.

The callback receives one plain object:

KeyTypeDescription
suggested
Type: Range or null
suggestedRange or nullThe range this program would apply if you left it alone.
whiteSpaceStartsAt
Type: Natural number or null
whiteSpaceStartsAtNatural number or nullIndex at which the reported whitespace chunk starts.
whiteSpaceEndsAt
Type: Natural number or null
whiteSpaceEndsAtNatural number or nullIndex at which the reported whitespace chunk ends.
str
Type: String
strStringThe input string, so the callback doesn’t have to close over it.

API — cbSchema

The list of opts.cb callback object’s key names, as an array of strings:

import { cbSchema } from "string-collapse-white-space";

console.log(cbSchema);
// => ["suggested", "whiteSpaceStartsAt", "whiteSpaceEndsAt", "str"]

It’s exported so that programs wrapping this one — and the unit tests — can assert that a callback object carries exactly these keys, without hardcoding the list in two places.

API — types

This package is written in TypeScript and exports the following types:

TypeDescription
Opts
Type: Opts
OptsThe Optional Options Object of collapse(), documented above.
Res
Type: Res
ResWhat collapse() returns — result and ranges.
Callback
Type: Callback
CallbackThe signature of opts.cb.
CbObj
Type: CbObj
CbObjThe plain object opts.cb is called with, shown above.
Extras
Type: Extras
ExtrasThe three positional keys of CbObj — whiteSpaceStartsAt, whiteSpaceEndsAt and str — without suggested.
Range
Type: Range
RangeA single range: a two- or three-element array.
RangesType
Type: RangesType
RangesTypeZero or more Ranges, or null. It’s named RangesType rather than Ranges to avoid a clash with the ranges-push class of that name.
import type { Callback, CbObj, Extras, Opts, Range, RangesType, Res } from "string-collapse-white-space";

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