string-range-expander1.12.0
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 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 throw
n.
§ 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 , on Sourcehut.
§ Licence
Copyright © 2010–2020 Roy Revelt and other contributors
Related packages:
📦 ranges-push 4.0.2
Gather string index ranges
📦 ranges-apply 4.0.2
Take an array of string index ranges, delete/replace the string according to them
📦 string-left-right 3.0.1
Looks up the first non-whitespace character to the left/right of a given index
📦 ranges-merge 6.2.0
Merge and sort string index ranges
📦 ranges-sort 3.14.0
Sort string index ranges
📦 ranges-offset 1.0.3
Increment or decrement each index in every range
📦 string-uglify 1.3.4
Shorten sets of strings deterministically, to be git-friendly