string-trim-spaces-only3.0.14

Like String.trim() but you can choose granularly what to trim

§ Quick Take

import { strict as assert } from "assert";
import { trimSpaces } from "string-trim-spaces-only";

assert.deepEqual(trimSpaces("  aaa   "), {
  res: "aaa",
  ranges: [
    [0, 2],
    [5, 8],
  ],
});

assert.deepEqual(trimSpaces("   \t  zz   \n    "), {
  res: "\t  zz   \n",
  ranges: [
    [0, 3],
    [12, 16],
  ],
});

§ API

trimSpaces(str, [opts])

In other words, it's a function which takes two input arguments, second-one being optional (marked by square brackets).

§ API - Function's Input

Input argumentKey value's typeObligatory?Description
inputStringyesInput string you want to trim some way
optsPlain objectnoAn Optional Options Object. See below for its API.

If input arguments are supplied have any other types, an error will be thrown.

§ Optional Options Object

An Optional Options Object's keyType of its valueDefaultDescription
classicTrimBooleanfalseIf set to true, trimming becomes the same as String.trim().
crBooleanfalseShould we trim the carriage returns (CR)
lfBooleanfalseShould we trim the line breaks (LF)
tabBooleanfalseShould we trim tabs
spaceBooleantrueShould we trim spaces
nbspBooleanfalseShould we trim raw non-breaking spaces

Here is the default options object in one place:

{
classicTrim: false,
cr: false,
lf: false,
tab: false,
space: true,
nbsp: false
}

§ API - Function's Output

Since v.2, the output is a plain object:

Key nameKey value's typeDescription
resString or zero or more charactersResult string after trimming.
rangesArray of zero or more arraysIf we trimmed anything, each slice range will be added into this array.

§ opts.classicTrim

String.trim() returns string but sometimes you need just ranges of what would be trimmed, to merge them into compiled ranges array and to process later, along everything else. In those cases, use opts.classicTrim. If you need just string value, it's not worth to use this function as a substitute for String.trim() for performance reasons.

§ Changelog

See it in the monorepo opens in a new tab, on GitHub.

§ Contributing

To report bugs or request features or assistance, raise an issue on GitHub opens in a new tab.

Any code contributions welcome! All Pull Requests will be dealt promptly.

§ Licence

MIT opens in a new tab

Copyright © 2010–2021 Roy Revelt and other contributors

Related packages:

📦 ranges-apply 5.0.14
Take an array of string index ranges, delete/replace the string according to them
📦 ranges-push 5.0.14
Gather string index ranges
📦 string-collapse-leading-whitespace 5.0.14
Collapse the leading and trailing whitespace of a string
📦 string-strip-html 8.2.11
Strips HTML tags from strings. No parser, accepts mixed sources
📦 string-remove-widows 2.0.14
Helps to prevent widow words in a text
📦 string-extract-class-names 6.0.14
Extracts CSS class/id names from a string
📦 string-split-by-whitespace 2.0.14
Split string into array by chunks of whitespace