No 3rd party dependencies. All dependencies and devDependencies, checked recursively, are Codsen packages.
Permalink to InstallationInstallation
Permalink to Quick TakeQuick Take
Permalink to ExamplesExamples
- Include carriage returns in the configured trim set
- Trim every JavaScript whitespace character
- Trim tabs but preserve ordinary spaces at the edges
- Include line feeds in the configured trim set
- Preserve non-space whitespace at the trimmed boundaries
- Include raw non-breaking spaces in the configured trim set
- Return an empty ranges array when the value needs no trimming
- Include tabs in the configured trim set
Purpose
trimSpaces() removes configurable whitespace from the start and end of a string. It returns both the trimmed string and the ranges removed from the original input. By default, it trims only ordinary spaces (U+0020).
import { trimSpaces } from "string-trim-spaces-only";
console.log(trimSpaces(" value "));
// {
// res: "value",
// ranges: [[0, 2], [7, 10]]
// }
API — trimSpaces()
The main function trimSpaces() is imported like this:
The function accepts a required string and an optional, partial options object:
| Argument | Type | Required | Description |
|---|---|---|---|
strType: String | |||
str | String | yes | String to trim. |
optsType: Partial options object | |||
opts | Partial options object | no | Whitespace characters to trim. |
Passing a non-string first argument throws an error.
Options
| Key | Type | Default | Description |
|---|---|---|---|
classicTrimType: Boolean Default: false | |||
classicTrim | Boolean | false | Use the same whitespace set as String.trim() and ignore the granular flags. |
crType: Boolean Default: false | |||
cr | Boolean | false | Trim carriage returns (U+000D). |
lfType: Boolean Default: false | |||
lf | Boolean | false | Trim line feeds (U+000A). |
tabType: Boolean Default: false | |||
tab | Boolean | false | Trim horizontal tabs (U+0009). |
spaceType: Boolean Default: true | |||
space | Boolean | true | Trim ordinary spaces (U+0020). |
nbspType: Boolean Default: false | |||
nbsp | Boolean | false | Trim non-breaking spaces (U+00A0). |
Here are all defaults in one place for copying:
Result
The function returns a plain object:
| Key | Type | Description |
|---|---|---|
resType: String | ||
res | String | String remaining after the configured whitespace is removed. |
rangesType: Range[] | ||
ranges | Range[] | Zero, one, or two end-exclusive [start, end] ranges identifying the removed input slices. |
Range offsets use JavaScript UTF-16 string indexes. If no characters are removed, ranges is an empty array. If both ends are trimmed, the leading range comes first.
The ranges can be combined with other Ranges ecosystem operations before the edits are applied:
import { rApply } from "ranges-apply";
import { trimSpaces } from "string-trim-spaces-only";
const input = " value ";
const { ranges } = trimSpaces(input);
console.log(rApply(input, ranges));
// => "value"
opts.classicTrim
Set classicTrim to true to use JavaScript’s complete String.trim() whitespace set while still receiving the removed ranges. In this mode, cr, lf, tab, space, and nbsp are ignored.
trimSpaces(" \t\n value \r\n ", { classicTrim: true });
// {
// res: "value",
// ranges: [[0, 4], [9, 13]]
// }
If you need only the resulting string, use String.trim() directly. Use this package when you need a custom trim set or the corresponding ranges.
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: