ranges-iterate2.1.0

Iterate a string and any changes within given string index ranges

Quick Take

import { strict as assert } from "assert";
import { rIterate } from "ranges-iterate";

// Ranges in the following example "punches out" a "hole" from `a` to `g`
// (included), replacing it with `xyz`. That's what gets iterated.

const gathered = [];

// a callback-based interface:
rIterate(
  "abcdefghij",
  [[0, 7, "xyz"]],
  ({ i, val }) => {
    gathered.push(`i = ${i}; val = ${val}`);
  }
);

assert.deepEqual(gathered, [
  "i = 0; val = x",
  "i = 1; val = y",
  "i = 2; val = z",
  "i = 3; val = h",
  "i = 4; val = i",
  "i = 5; val = j",
]);

Quick Take

It iterates all characters in a string, as if given ranges were already applied.

Sometimes certain operations on a string aren't really composable — sometimes we want to traverse the string as if ranges were applied, as if we already had the final result.

API

rIterate(str, ranges, cb, [offset])

In other words, this library gives you a synchronous function (exported as a default) and you must feed three obligatory arguments and fourth, optional (marked with square brackets).

Input argument Type Obligatory? Description
str string yes The input string we are operating on
ranges null or array of zero or more arrays (ranges) yes The ranges gathered so far
cb Something falsy or a function yes Callback function to be able to consume the indexes and character values
offset String index, a natural number no You can cut corners and start operations later in the string

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-sort 4.1.0
Sort string index ranges
📦 ranges-process-outside 4.1.0
Iterate string considering ranges, as if they were already applied
📦 ranges-apply 5.1.0
Take an array of string index ranges, delete/replace the string according to them
📦 ranges-ent-decode 4.1.0
Recursive HTML entity decoding for Ranges workflow
📦 ranges-is-index-within 2.1.0
Checks if index is within any of the given string index ranges
📦 ranges-push 5.1.0
Gather string index ranges
📦 ranges-regex 4.1.0
Integrate regex operations into Ranges workflow