Installation
Quick Take
Examples
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, do we delete non-breaking spaces (if set to true, answer would be “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.