Installation
Quick Take
API — rProcessOutside()
The main function rProcessOutside()
is imported like this:
It’s a function which takes four input arguments:
Input argument | Type | Obligatory | Description |
---|---|---|---|
originalStr Type: string Obligatory: yes | |||
originalStr | string | yes | Source string |
originalRanges Type: null or ranges — array of arraysObligatory: yes | |||
originalRanges | null or ranges — array of arrays | yes | String indexes outside these ranges will be processed (fed to callback function) |
cb Type: function Obligatory: yes | |||
cb | function | yes | Callback function you provide (like in Array.forEach ) |
skipChecks Type: boolean Obligatory: no | |||
skipChecks | boolean | no | By default checks are performed upon inputs but you can turn that off to boost the performance |
The function will return undefined
because it has a callback-style API.
API — version
You can import version
:
Callback cb
This program operates in a callback fashion, just like Array.prototype.forEach()
, for example:
import { rProcessOutside } from "ranges-process-outside";
const gather = [];
rProcessOutside("abcdefghij", [[0, 5]], (idx) => {
gather.push(idx);
});
console.log(gather);
// => [5, 6, 7, 8, 9]
This (idx) => { gather.push(idx); }
above is the callback function (as arrow-function).
It’s API is the following:
Input argument | Type | Description |
---|---|---|
fromIdx Type: String index: natural number or zero | ||
fromIdx | String index: natural number or zero | Starting index of the chunk programs pings you |
toIdx Type: String index: natural number or zero | ||
toIdx | String index: natural number or zero | Ending index of the chunk programs pings you |
offsetValueCb Type: Function or something falsy | ||
offsetValueCb | Function or something falsy | Callback function to bump the indexes in the loop that pings you all this. See below for more. |
offsetValueCb
API
A callback inside a callback!
offsetValueCb
argument the cb()
gives you is a function. Call it with a single argument, a natural number — that will instruct the outer loop function to skip that many characters.
For example, skip one character:
import { rProcessOutside } from "ranges-process-outside";
const gather = [];
rProcessOutside("abcdefghij", [[0, 5]], (fromIdx, toIdx, offsetValueCb) => {
gather.push(fromIdx);
if (fromIdx === 6) {
// at index 6, skip the next index:
offsetValueCb(1);
}
});
console.log(gather);
// => [5, 6, 8, 9]
// notice 7 is missing because we skipped by 1 while being at index 6
The third argument in the callback cb
(the arrow function (fromIdx, toIdx, offsetValueCb) => {...}
above) is a function which lets you bump the looping index. This way, you can skip the characters being pinged.
For example, Detergent processes HTML code. A string with bunch of invisible characters are detected and their from-to ranges are pushed to ranges being gathered. While processing the input further, Detergent avoids processing the characters booked for deletion (marked by ranges). It uses this program to work on everything in-between the ranges (invisible character locations). Now, Detergent detects a named HTML entity:
. Once it stumbles on its first character, the ampersand &
, it does what it has to do and it needs to skip 6 characters further.
This skipping is performed by this callback.