Détail du package

string-collapse-white-space

codsen4.3kMIT11.0.22

Replace chunks of whitespace with a single spaces

chunk, collapse, empty, replace

readme

string-collapse-white-space

Replace chunks of whitespace with a single spaces

page on codsen.com page on npm page on github Downloads per month changelog MIT Licence

Install

This package is pure ESM. If you're not ready yet, install an older version of this program, 9.1.0 (npm i string-collapse-white-space@9.1.0).

npm i string-collapse-white-space

Quick Take

import { strict as assert } from "assert";

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

assert.equal(
  collapse("  aaa     bbb    ccc   dddd  ").result,
  "aaa bbb ccc dddd",
);

assert.equal(collapse("   \t\t\t   aaa   \t\t\t   ").result, "aaa");

assert.equal(
  collapse("   aaa   bbb  \n    ccc   ddd   ", { trimLines: false }).result,
  "aaa bbb \n ccc ddd",
);

assert.equal(
  collapse("   aaa   bbb  \n    ccc   ddd   ", { trimLines: true }).result,
  "aaa bbb\nccc ddd",
);

// \xa0 is an unencoded non-breaking space:
assert.equal(
  collapse(
    "     \xa0    aaa   bbb    \xa0    \n     \xa0     ccc   ddd   \xa0   ",
    { trimLines: true, trimnbsp: true },
  ).result,
  "aaa bbb\nccc ddd",
);

Documentation

Please visit codsen.com for a full description of the API.

Contributing

To report bugs or request features or assistance, raise an issue on GitHub.

Licence

MIT License.

Copyright © 2010-2025 Roy Revelt and other contributors.

ok codsen star

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

11.0.0 (2022-12-01)

BREAKING CHANGES

  • Minimum supported Node version is v14.18; we're dropping v12 support

10.2.0 (2022-08-12)

Features

10.1.0 (2022-04-11)

Features

  • export defaults and version (1107244)

10.0.0 (2021-09-09)

Features

BREAKING CHANGES

  • programs now are in ES Modules and won't work with Common JS require()

9.1.0 (2021-05-24)

Features

  • config file based major bump blacklisting (e15f9bb)

9.0.15 (2021-04-11)

Reverts

  • Revert "chore: setup refresh" (23cf206)

9.0.1 (2021-01-28)

Fixed

  • add testStats to npmignore (f3c84e9)

9.0.0 (2021-01-23)

Features

  • rewrite in TS, start using named exports (70f7388)

BREAKING CHANGES

  • previously you'd consume like: import collapse from ... - now: import { collapse } from ...

7.0.0 (2020-11-06)

A major rewrite.

Each whitespace chunk is needed is passed through a callback (even when there is no action needed). This allows you to granularly control the collapsing. For example, you can delete the single spaces between two characters under certain circumstances (a thing not available by existing options' settings) - for example, minify css selectors:

div > span {
  color: red;
}

Imagine you are minifying the code above and parser extracted the div > span part. Now you want to collapse excessive whitespace like div \t > \t span (spaces with tabs), but also remove even single spaces, to turn it into div>span.

Easy!

import { strict as assert } from "assert";
import collapse from "../dist/string-collapse-white-space.esm.js";
assert.equal(
  collapse(`div > span`, {
    cb: ({ suggested, whiteSpaceStartsAt, whiteSpaceEndsAt, str }) => {
      if (str[whiteSpaceStartsAt - 1] === ">") {
        return [whiteSpaceStartsAt, whiteSpaceEndsAt];
      }
      if (str[whiteSpaceEndsAt] === ">") {
        return [whiteSpaceStartsAt, whiteSpaceEndsAt];
      }
      return suggested;
    },
  }).result,
  "div>span",
);

Features

  • opts.cb

BREAKING CHANGES

  • removed opts.recogniseHTML
  • removed opts.rangesOffset (use ranges-offset)

Also, properly tested and fixed opts.enforceSpacesOnly. It now works as intended, considering all edge cases and interaction with other options.

6.1.0 (2020-10-26)

Features

6.0.0 (2020-10-12)

Features

  • returns a plain object with both string and ranges (275b940)

BREAKING CHANGES

  • returns a plain object with both string and ranges

5.2.0 (2019-10-26)

Features

  • proper support for all possible kinds of line end symbols (5a49049)

5.2.0

  • full support for all kinds of linebreaks: LF, CR or CRLF
  • basic unit tests for UMD and CJS builds, to check their integrity (rather than functionality, which is covered properly by ES Modules build tests)

5.1.2 (2019-10-02)

Fixed

  • string boundary won't throw now, sorry about that (94639dd)

5.1.0 (2019-09-11)

Features

  • opts.limitConsecutiveEmptyLinesTo (ece9334)

4.5.0 (2019-01-20)

  • Various documentation and setup tweaks after we migrated to monorepo
  • Setup refresh: updated dependencies and all config files using automated tools

4.3.0 (2018-12-26)

  • Add dynamically generated tests - 10000 random strings are checked to catch any false-positives (43d7dc4)
  • Add opts.removeEmptyLines (75e0671)

4.2.0 (2018-10-25)

  • Updated all dependencies
  • Restored coveralls.io reporting
  • Restored unit test linting

4.1.0 (2018-07-24)

  • Fixed a strange false-positive bug which was happening around the encountered first bracket when there was no whitespace to remove, but with only a single line break. Sorry about that.
  • To iron-out all false-positive cases, I created an 8th group unit test where 10,000 randomly generated strings that don't contain anything to collapse are checked. Various pieces are assembled in random order and length: <br>, <br/>, <zzz class="yyy">, zzz, 1, _, a, &, #, .. Plus, there are single spaces in-between, added with 25% probability.

Here are ten examples of randomly-generated text which should not be changed by string-collapse function (notice there are maximum one space everywhere):

& 1 _<br/>#<br/><br><br> <br>_a1

a&1_.zzz<br/>.#zzz<br>&.<zzz class="yyy"> <zzz class="yyy">a zzz<br/>_<br><br/> <zzz class="yyy"><br/><zzz class="yyy">zzz1<br>_ <br><br><br>_# <zzz class="yyy">_.<br/>a1zzz 1.zzz<br/><zzz class="yyy">&<zzz class="yyy">.<br>.

aaa1<br>1a &a1<br/>1<br/> <br>..#_1zzz _<br><br/> <zzz class="yyy">1 <br/>zzz azzz&<br>a& &<br/> _a &<zzz class="yyy">#

<zzz class="yyy">__aa..#

_<zzz class="yyy"> <br>. _<br/>zzz... <br/>zzz<br/> 1.<br/> & zzz& 1zzza <br> <br/> <br><br><br>&<zzz class="yyy">zzz1.zzz._& _ ##<br>a#<br>.<br/>

.a<br>zzz _& 1_ 1zzz<zzz class="yyy"> <br/>#<br/>zzz & #<br/><br/> &<br>&<zzz class="yyy">_<br/><zzz class="yyy">.&a<br> &_#_

_azzz 11##

a.<br/>.<br/><br><br/><br/><br/><br/> # _1 1<zzz class="yyy"> .<br/>1<br>&# <br/><br/> <br>_aa <br/>

## zzz& .# .a&1&_aa 1 zzz<zzz class="yyy">zzz.zzz_zzz_##_## <br><br/><br/>1

<zzz class="yyy"> zzz 1<zzz class="yyy">_1<br>zzz.&<zzz class="yyy"> #___zzz<zzz class="yyy">.a a_<br/>1_. <br/> <br/><br> . _1 <br/>

Hopefully bugs like the one we just fixed should not happen any more 🤞

4.0.0 (2018-07-23)

  • When opts.returnRangesOnly is on and there are no results, instead of null (a falsey thing) now we return empty array (a truthy thing). It's better now because while you can't easily check for its existence (a la if result), actually you don't have to any more. Just map over its result. It will always be array, albeit sometimes empty.

3.7.0 (2018-06-20)

  • Set up Rollup to remove comments from the code

3.6.0 (2018-06-20)

3.5.0 (2018-06-11)

GitHub sold us out. In the meantime, we:

  • Migrated to BitBucket (to host repo + perform CI) and Codacy (for code quality audit)
  • Dropped BitHound (RIP) and Travis
  • Removed package-lock
  • Added new feature — opts.returnRangesOnly

3.4.0 (2018-05-26)

  • Set up Prettier on a custom ESLint rule set.
  • Removed package.lock and .editorconfig
  • Wired Rollup to remove comments from non-dev builds. This means we can now leave the console.logs in the source code — there's no need to comment-out console.log statements or care about them not spilling into production. Now it's done automatically.
  • Unit tests are pointing at ES modules build, which means that code coverage is correct now, without Babel functions being missed. This is important because now code coverage is real again and now there are no excuses not to perfect it.

3.3.0 (2018-04-30)

Features

  • opts.removeEmptyLines
  • Set up Prettier to run automatically on all relevant files. Dropping airbnb-base ESLint preset.
  • Stopping to commit (and generate at all) package-lock files
  • Beefed up unit tests, filled all missing rows. Obviously, unit tests will be ran against the transpiled code (as well as nyc/coveralls reporting) and I can't unit-test some of the functions that Babel adds. Hence not perfect coverage score.

3.2.0 (2017-10-29)

Features

  • Removed the look left-right matching function into a separate library, string-match-left-right and tapped it. Also did some tiny code rebasing.

3.1.0 (2017-10-27)

Features

  • AVA unit test linting via ESLint plugin

3.0.0 (2017-09-30)

Features

  • The main source now is in ES2015 modules with import/export.
  • Implemented Rollup to generate 3 flavours of this package: CommonJS, UMD and ESM module with import/export.
  • opts.recogniseHTML (default true) - if string contains HTML, whitespace around brackets will be collapsed completely, for example: < img => <img, not < img => < img as before. The "before" result now would yield only with this new setting turned off. Total 118 opening HTML tags are recognised (with or without attributes).

2.2.0 (2017-09-16)

Features

  • opts.trimLines - activates trim per-line basis
  • opts.trimnbsp - non-breaking spaces are trimmed too
  • switched to ESLint on airbnb-base config, with 3 exceptions: 1. no semicolons; 2. plus-plus allowed in loops;

2.1.0 (2017-09-03)

Features

  • Correctly treats non-breaking spaces - they are not considered collapsable or trimmable now.

2.0.0 (2017-09-03)

Changes

  • This morning I didn't like yesterday's v.1 API at all, personally I think it was stupid. So, I simplified it and basically, recoded the whole thing.

1.0.0 (2017-09-02)

  • First public release