Skip to Content
  • Website
Codsen
  • Home
  • Open Source
  • Articles
  • About

prevOpen Source→ranges-process-outsidenext

ranges-process-outside6.2.1

Iterate string considering ranges, as if they were already applied

Downloads per monthChangelogMIT Licenselibera manifesto
  • the top
  • Installation
  • Quick Take
  • Examples
  • API — RPROCESSOUTS…
  • API — VERSION
  • API — TYPES
  • CALLBACK CB
  • OFFSETVALUEC…
  • Changelog

Installation

Quick Take

Examples

  • Process the whole string when no ranges are supplied
  • Skip range validation in a prevalidated pipeline
  • Skip ahead from inside the callback
  • Process Unicode code points without splitting surrogate pairs

API — rProcessOutside()

The main function rProcessOutside() is imported like this:

It’s a function which takes four input arguments:

Input argumentTypeObligatoryDescription
originalStr
Type: string
Obligatory: yes
originalStrstringyesSource string
originalRanges
Type: null or ranges — array of arrays
Obligatory: yes
originalRangesnull or ranges — array of arraysyesString indexes outside these ranges will be processed (fed to callback function)
cb
Type: function
Obligatory: yes
cbfunctionyesCallback function you provide (like in Array.forEach)
skipChecks
Type: boolean
Obligatory: no
skipChecksbooleannoBy 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:

API — types

This package is written in TypeScript and exports the following types:

TypeDescription
Callback
Type: Callback
CallbackThe signature of the third input argument — called with fromIdx, toIdx and an OffsetValueCb.
OffsetValueCb
Type: OffsetValueCb
OffsetValueCbThe third callback argument — call it with a number to skip that many indexes.
Ranges
Type: Ranges
RangesZero or more ranges, or null, re-exported from ranges-crop.
import type { Callback, OffsetValueCb, Ranges } from "ranges-process-outside";

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 arrowopens in a new tab-function).

It’s API is the following:

Input argumentTypeDescription
fromIdx
Type: String index: natural number or zero
fromIdxString index: natural number or zeroStarting index of the chunk programs pings you
toIdx
Type: String index: natural number or zero
toIdxString index: natural number or zeroEnding index of the chunk programs pings you
offsetValueCb
Type: Function or something falsy
offsetValueCbFunction or something falsyCallback 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 the callback 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.

Changelog

Open Changelog
↑ back to top
prev next

Copyright

All rights reserved © Roy Revelt 2026
All our open source packages are under MIT licenceopens in a new tab

Activities

🐛 See a bug? Raise an issueopens in a new tab
💘 Check out the Indiewebopens in a new tab and Libera manifestoopens in a new tab