string-range-expander1.11.11

Expands string index ranges within whitespace boundaries until letters are met

§ Quick Take

import { strict as assert } from "assert";
import expander from "string-range-expander";

// let's say we have picked the "zzzz" index range - [16, 20]
// "something>\n\t    zzzz <here"
//                    |   |
//                  from  to
//
// PS. "\n" and "\t" take up a single character's length

assert.deepEqual(
  expander({
    str: "something>\n\t    zzzz <here",
    from: 16,
    to: 20,
    ifRightSideIncludesThisThenCropTightly: "<",
  }),
  [10, 21]
);

§ The Purpose

This program is used to manage the whitespace in the string index selections. Let's say we want to delete certain index range in a string but maybe there is whitespace around? This program extends the index ranges as needed.

§ API

expand(opts)

In other words, it's a function which takes a single input argument.

§ API - Input

Input argumentKey value's typeObligatory?Description
optsPlain objectyesAn Options Object. See below for its API.

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

§ Optional Options Object

Options Object's keyType of its valueObligatory?DefaultDescription
strstringyes"" (empty)String to reference
fromnumber (natural number)yes0Index from which we should expand backwards
tonumber (natural number)yes0Index from which we should expand backwards
ifLeftSideIncludesThisThenCropTightlystringno"" (empty)All characters to the left side of given range you want to tigger a tight crop. All concatenated into one chunk.
ifLeftSideIncludesThisCropItToostringno"" (empty)All characters to the left side of given range you want to skip as if they were whitespace
ifRightSideIncludesThisThenCropTightlystringno"" (empty)All characters to the right side of given range you want to tigger a tight crop. All concatenated into one chunk.
ifRightSideIncludesThisCropItToostringno"" (empty)All characters to the right side of given range you want to skip as if they were whitespace
extendToOneSideBoolean false or strings "left" or "right"nofalseYou can expand the range only to one side if you want using this.
wipeAllWhitespaceOnLeftBooleannofalseIf on, range will be extended to the left until it reaches the first non-whitespace character (or EOL)
wipeAllWhitespaceOnRightBooleannofalseIf on, range will be extended to the right until it reaches the first non-whitespace character (or EOL)
addSingleSpaceToPreventAccidentalConcatenationBooleannofalseIf on, it will prevent accidental concatenation of strings by inserting a single space in tight crop situations

Here it is in one place if you want to copy-paste it somewhere:

{
str: "",
from: 0,
to: 0,
ifLeftSideIncludesThisThenCropTightly: "",
ifLeftSideIncludesThisCropItToo: "",
ifRightSideIncludesThisThenCropTightly: "",
ifRightSideIncludesThisCropItToo: "",
extendToOneSide: false,
wipeAllWhitespaceOnLeft: false,
wipeAllWhitespaceOnRight: false,
addSingleSpaceToPreventAccidentalConcatenation: false
}

§ API - Output

The output is an array of two indexes, the new "from" and new "to". For example, [12, 14].

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 ranges-push 3.7.22
Gather string index ranges
📦 ranges-apply 3.2.3
Take an array of string index ranges, delete/replace the string according to them
📦 string-left-right 2.3.31
Looks up the first non-whitespace character to the left/right of a given index
📦 ranges-merge 5.0.3
Merge and sort string index ranges
📦 ranges-sort 3.13.3
Sort string index ranges
📦 string-collapse-white-space 6.0.0
Efficient collapsing of white space with optional outer- and/or line-trimming and HTML tag recognition
📦 string-fix-broken-named-entities 3.0.11
Finds and fixes common and not so common broken named HTML entities, returns ranges array of fixes