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 , on GitHub.
Contributing
To report bugs or request features or assistance, raise an issue on GitHub .
Any code contributions welcome! All Pull Requests will be dealt promptly.
Licence
Copyright © 2010–2021 Roy Revelt and other contributors