What is ranges-apply?
The 'ranges-apply' npm package is used to apply multiple string editing operations, defined as ranges, to a given input string. This is particularly useful for tasks that involve multiple, non-overlapping edits to a string, such as text processing, code formatting, or data cleaning.
What are ranges-apply's main functionalities?
Apply Ranges
This feature allows you to apply multiple ranges of edits to a string. Each range is defined by a start index, an end index, and a replacement string. The function processes these ranges and applies the edits to the input string.
const { rApply } = require('ranges-apply');
const input = 'Hello, world!';
const ranges = [
[7, 12, 'universe'],
[0, 5, 'Hi']
];
const result = rApply(input, ranges);
console.log(result); // 'Hi, universe!'
Handling Overlapping Ranges
This feature demonstrates how the package handles overlapping ranges. The edits are applied in the order they appear in the array, which can result in overlapping changes being concatenated.
const { rApply } = require('ranges-apply');
const input = 'Hello, world!';
const ranges = [
[7, 12, 'universe'],
[10, 15, 'planet']
];
const result = rApply(input, ranges);
console.log(result); // 'Hello, universeplanet!'
Empty Ranges
This feature shows how to insert text at a specific position without removing any existing characters. An empty range (where the start and end indices are the same) allows you to insert text at that position.
const { rApply } = require('ranges-apply');
const input = 'Hello, world!';
const ranges = [
[7, 7, 'beautiful ']
];
const result = rApply(input, ranges);
console.log(result); // 'Hello, beautiful world!'
Other packages similar to ranges-apply
diff-match-patch
The 'diff-match-patch' package provides robust algorithms for synchronizing plain text. It can be used to find differences between texts, patch texts, and apply patches. Compared to 'ranges-apply', it offers more advanced diffing and patching capabilities but may be more complex to use for simple range-based edits.
string-replace-async
The 'string-replace-async' package allows for asynchronous string replacements using regular expressions. While it focuses on regex-based replacements rather than range-based edits, it can be useful for more complex text processing tasks that require asynchronous operations.
replacestream
The 'replacestream' package is used for streaming text replacements. It is particularly useful for processing large files or streams of data. Unlike 'ranges-apply', which works on static strings, 'replacestream' is designed for use cases involving streams and large-scale text processing.
ranges-apply
Take an array of string index ranges, delete/replace the string according to them
Install
This package is pure ESM. If you're not ready yet, install an older version of this program, 5.1.0 (npm i ranges-apply@5.1.0
).
npm i ranges-apply
Quick Take
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."
);
Documentation
Please visit codsen.com for a full description of the API.
Contributing
To report bugs or request features or assistance, raise an issue on GitHub.
Licence
MIT License.
Copyright © 2010-2022 Roy Revelt and other contributors.