Well, sort of.
Read article ESLint Uses Similar Thing to Ranges
import { strict as assert } from "assert";
import { rApply } from "ranges-apply";
const oldString = `The quick brown fox jumps over the lazy dog.`;
const ranges = [
[4, 19, "bad grey wolf"],
[35, 43, "little Red Riding Hood"],
];
assert.equal(
rApply(oldString, ranges),
"The bad grey wolf jumps over the little Red Riding Hood."
);
It takes the source string and the amendments described by Ranges and produces a new string.
rApply( inputString, rangesArray, [progressFn] )
Input argument | Type | Obligatory? | Description |
---|---|---|---|
inputString | String | yes | Provide an array of ranges to invert. Ranges do not have to be sorted or merged. |
rangesArray | Array of zero or more arrays - OR - null | yes | Ranges to apply onto the string |
progressFn | Function or something falsy | no | Provide a callback function to report the progress - numbers 0 to 100 will be fed into it as the program advances. |
Function returns an amended string.
Check out ranges-push which helps to manage the rangesArray
. It has methods to add and retrieve the ranges. Also, it helps in cases where ranges overlap and helps to maintain the sorting order.
We array.reduce()
your given ranges array, slicing the input string accordingly. If given ranges is not array but null
(meaning absence of ranges), same string is returned.
Originally this library was part of email-comb, where we traversed HTML as a string and compiled an array of things to delete or replace later, in one go. The performance was important, so it was not a good idea to delete/replace things on the spot because each deletion slowed down the process. Instead, we traversed the string, compiled this to-do array, then did the deletion/replacement on the whole thing, once. This appears to be the fastest way.
We're going to use this library in all our HTML processing libraries who work on HTML as on string, without parsing it.
See it in the monorepo , on GitHub.
To report bugs or request features or assistance, raise an issue on GitHub .
Any code contributions welcome! All Pull Requests will be dealt promptly.
Copyright © 2010–2021 Roy Revelt and other contributors
Well, sort of.
Read article ESLint Uses Similar Thing to Ranges