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 argument | Type | Obligatory | Description |
|---|---|---|---|
strType: String Obligatory: yes | |||
str | String | yes | Source string to work upon |
optsType: Something falsy or a Plain object Obligatory: no | |||
opts | Something falsy or a Plain object | no | The Optional Options Object, see below for its API |
The Optional Options Object has the following shape:
| Key | Type | Obligatory | Default | Description |
|---|---|---|---|---|
trimStartType: Boolean Obligatory: no Default: true | ||||
trimStart | Boolean | no | true | if false, leading whitespace will be just collapsed. |
trimEndType: Boolean Obligatory: no Default: true | ||||
trimEnd | Boolean | no | true | if false, trailing whitespace will be just collapsed. |
trimLinesType: Boolean Obligatory: no Default: false | ||||
trimLines | Boolean | no | false | if 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) |
trimnbspType: Boolean Obligatory: no Default: false | ||||
trimnbsp | Boolean | no | false | When trimming, delete non-breaking spaces too? (If set to true, the answer is “yes”.) This setting also affects trimLines setting above. |
removeEmptyLinesType: Boolean Obligatory: no Default: false | ||||
removeEmptyLines | Boolean | no | false | if any line can be trimmed to empty string, it will be removed. |
limitConsecutiveEmptyLinesToType: Natural number or zero Obligatory: no Default: 0 | ||||
limitConsecutiveEmptyLinesTo | Natural number or zero | no | 0 | Set to 1 or more to allow that many blank lines between content |
enforceSpacesOnlyType: Boolean Obligatory: no Default: false | ||||
enforceSpacesOnly | Boolean | no | false | If enabled, not only consecutive space character chunks will be collapsed but any whitespace character chunks (except line breaks). |
cbType: Function Obligatory: no Default: see below | ||||
cb | Function | no | see below | All 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 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
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:
| 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 | Index at which the reported whitespace chunk starts. |
whiteSpaceEndsAtType: Natural number or null | ||
whiteSpaceEndsAt | Natural number or null | Index at which the reported whitespace chunk ends. |
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";