No 3rd party dependencies. All dependencies and devDependencies, checked recursively, are Codsen packages.
Permalink to InstallationInstallation
Permalink to Quick TakeQuick Take
Permalink to ExamplesExamples
- 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:
Collapse consecutive ASCII spaces and optionally trim or normalize other whitespace. Line breaks remain unless the trimming or empty-line options remove them.
| Input argument | Type | Obligatory | Description |
|---|---|---|---|
strType: String Obligatory: yes | |||
str | String | yes | Source string to work upon |
optsType: Object Obligatory: no | |||
opts | Object | no | Options to override the defaults below. |
The Optional Options Object has the following shape:
| Key | Type | Default | Description |
|---|---|---|---|
trimStartType: Boolean Default: true | |||
trimStart | Boolean | true | Remove leading whitespace, stopping at a protected non-breaking space. |
trimEndType: Boolean Default: true | |||
trimEnd | Boolean | true | Remove trailing whitespace, stopping at a protected non-breaking space. |
trimLinesType: Boolean Default: false | |||
trimLines | Boolean | false | Trim each line without removing its CR, LF, or CRLF line break. Protected non-breaking spaces stop trimming within that line. |
trimnbspType: Boolean Default: false | |||
trimnbsp | Boolean | false | Allow trimming to remove non-breaking spaces (U+00A0) too. |
removeEmptyLinesType: Boolean Default: false | |||
removeEmptyLines | Boolean | false | Remove blank lines between content, retaining one line break plus the number of blank lines set below. A line containing only whitespace, including non-breaking spaces, is blank. |
limitConsecutiveEmptyLinesToType: Non-negative integer Default: 0 | |||
limitConsecutiveEmptyLinesTo | Non-negative integer | 0 | Number of blank lines to retain when removeEmptyLines is true. |
enforceSpacesOnlyType: Boolean Default: false | |||
enforceSpacesOnly | Boolean | false | Replace each run of whitespace other than CR/LF with one ASCII space. This includes non-breaking spaces even when trimnbsp is false. |
cbType: Function Default: ({ suggested }) => suggested | |||
cb | Function | ({ suggested }) => suggested | Accept, replace, or ignore each proposed range. Unchanged whitespace receives a null suggestion. |
Trimming and empty-line removal take precedence over enforceSpacesOnly: a whitespace segment that is removed does not leave a replacement space. With trimnbsp: false, trimming still removes ordinary whitespace outside a protected non-breaking space. A non-breaking space on one line does not prevent trimming another line.
trimStart: false and trimEnd: false disable whole-string trimming. trimLines: true still trims the first and last lines. By default, ordinary space runs collapse, while other whitespace is preserved unless an enabled option changes it.
removeEmptyLines treats non-breaking-space-only lines as blank even when trimnbsp is false. Keep removeEmptyLines: false when those lines must remain.
collapse() uses the same options and callback contract in ESM and the direct-browser script.
\u00a0 below represents a non-breaking space:
collapse(" \u00a0a").result;
// => "\u00a0a"
collapse("a\t\nb", {
trimLines: true,
enforceSpacesOnly: true,
}).result;
// => "a\nb"
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 name | Type | Description |
|---|---|---|
resultType: String | ||
result | String | The string output where all ranges were applied to it. |
rangesType: ranges: an array of one or more arrays containing from-to string index ranges OR null | ||
ranges | ranges: an array of one or more arrays containing from-to string index ranges OR null | For 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
The callback receives each proposed edit before it is applied. Return suggested to accept it, another range to customize it, or null to preserve that part of the input. The default callback is ({ suggested }) => suggested.
See examples.
When a whitespace run needs no edit, the callback receives suggested: null. It can still return a range to delete or replace that whitespace. Handle this nullable value before indexing suggested. A run that already produced an edit suggestion does not receive an additional null notification, even when the callback rejects that edit.
A run spanning several lines can produce multiple subrange suggestions. Each reports the boundaries of the complete whitespace run, while suggested identifies the particular edit. All indices refer to the original input, and end indices are exclusive. At the end of the input, whiteSpaceEndsAt equals str.length, including any trailing line breaks, tabs, and non-breaking spaces.
The callback receives one plain object:
| Key | Type | Description |
|---|---|---|
suggestedType: Range or null | ||
suggested | Range or null | The range this program would apply if you left it alone. |
whiteSpaceStartsAtType: Natural number or null | ||
whiteSpaceStartsAt | Natural number or null | Start index of the complete whitespace run in the original input. |
whiteSpaceEndsAtType: Natural number or null | ||
whiteSpaceEndsAt | Natural number or null | Exclusive end index of the complete whitespace run in the original input. |
strType: String | ||
str | String | The 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:
| Type | Description |
|---|---|
OptsType: Opts | |
Opts | The Optional Options Object of collapse(), documented above. |
ResType: Res | |
Res | What collapse() returns — result and ranges. |
CallbackType: Callback | |
Callback | The signature of opts.cb. |
CbObjType: CbObj | |
CbObj | The plain object opts.cb is called with, shown above. |
ExtrasType: Extras | |
Extras | The three positional keys of CbObj — whiteSpaceStartsAt, whiteSpaceEndsAt and str — without suggested. |
RangeType: Range | |
Range | A single range: a two- or three-element array. |
RangesTypeType: RangesType | |
RangesType | Zero 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";