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

prevOpen Source→string-collapse-leading-whitespacenext

string-collapse-leading-whitespace7.2.0

Collapse the leading and trailing whitespace of a string

Downloads per monthChangelogMIT Licenselibera manifesto
  • the top
  • Installation
  • Quick Take
  • Examples
  • PURPOSE
  • API — COLLWHITESPA…
  • API — VERSION
  • Changelog

Installation

Quick Take

Examples

  • Collapse boundary whitespace containing line breaks
  • Preserve CRLF line endings
  • Keep a chosen number of leading and trailing line breaks
  • Preserve non-breaking-space boundaries
  • Pass through non-string input
  • Leave whitespace inside a multiline string unchanged
  • Preserve a string that has no boundary whitespace
  • Collapse strings containing only whitespace

Purpose

This program is aimed to process strings before concatenating them. It collapses the leading and trailing whitespace, if any, so that later the result looks reasonable.

For example, ranges-push uses it to merge to-be-inserted chunks of string.

It does not touch the “inner” whitespace, gaps between chunks of text present between the first and last non-whitespace characters. See string-collapse-white-space which processes those gaps.

API — collWhitespace()

The main function collWhitespace() is imported like this:

It’s a function which takes two input arguments:

Input argumentTypeObligatoryDefaultDescription
str
Type: String
Obligatory: yes
Default: undefined
strStringyesundefinedSource string to work on
lineBreakLimit
Type: Natural number or zero
Obligatory: no
Default: 1
lineBreakLimitNatural number or zerono1If whitespace contains linebreaks, it will be replaced by those linebreaks, count limited to this value

If first input argument is not a string, it will be just returned back, untouched. If second input argument is zero or falsy or not a number, it will be set to 1 and application will continue as normal.

The function will return a string of zero or more characters. If input was not a string, same input will be returned back, without an error.

API — version

You can import version:

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