string-range-expander3.0.1

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

expander(opts)

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

API - Input

Input argument Key value's type Obligatory? Description
opts Plain object yes An 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 key Type of its value Obligatory? Default Description
str string yes "" (empty) String to reference
from number (natural number) yes 0 Index from which we should expand backwards
to number (natural number) yes 0 Index from which we should expand backwards
ifLeftSideIncludesThisThenCropTightly string no "" (empty) All characters to the left side of given range you want to tigger a tight crop. All concatenated into one chunk.
ifLeftSideIncludesThisCropItToo string no "" (empty) All characters to the left side of given range you want to skip as if they were whitespace
ifRightSideIncludesThisThenCropTightly string no "" (empty) All characters to the right side of given range you want to tigger a tight crop. All concatenated into one chunk.
ifRightSideIncludesThisCropItToo string no "" (empty) All characters to the right side of given range you want to skip as if they were whitespace
extendToOneSide Boolean false or strings "left" or "right" no false You can expand the range only to one side if you want using this.
wipeAllWhitespaceOnLeft Boolean no false If on, range will be extended to the left until it reaches the first non-whitespace character (or EOL)
wipeAllWhitespaceOnRight Boolean no false If on, range will be extended to the right until it reaches the first non-whitespace character (or EOL)
addSingleSpaceToPreventAccidentalConcatenation Boolean no false If 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].

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-push 6.0.1
Gather string index ranges
📦 ranges-apply 6.0.1
Take an array of string index ranges, delete/replace the string according to them
📦 string-left-right 5.0.1
Looks up the first non-whitespace character to the left/right of a given index
📦 ranges-merge 8.0.1
Merge and sort string index ranges
📦 ranges-sort 5.0.1
Sort string index ranges
📦 ranges-offset 3.0.1
Increment or decrement each index in every range
📦 string-match-left-right 8.0.1
Match substrings on the left or right of a given index, ignoring whitespace