1.0.0 • Published 5 months ago
@torvalds/range_list v1.0.0
RangeList
The RangeList
class represents a list of ranges in JavaScript. A range is defined by a pair of integers, for example, [1, 5), which includes integers 1, 2, 3, and 4.
Usage
// Import the RangeList class
import RangeList from 'range-list';
// Create a new RangeList instance
const rl = new RangeList();
// Output the initial state (empty range list)
console.log(rl.toString()); // Should be ""
// Add ranges to the list
rl.add([1, 5]);
console.log(rl.toString()); // Should be: "[1, 5)"
rl.add([10, 20]);
console.log(rl.toString()); // Should be: "[1, 5) [10, 20)"
rl.add([20, 20]);
console.log(rl.toString()); // Should be: "[1, 5) [10, 20)"
rl.add([20, 21]);
console.log(rl.toString()); // Should be: "[1, 5) [10, 21)"
rl.add([2, 4]);
console.log(rl.toString()); // Should be: "[1, 5) [10, 21)"
rl.add([3, 8]);
console.log(rl.toString()); // Should be: "[1, 8) [10, 21)"
// Remove ranges from the list
rl.remove([10, 10]);
console.log(rl.toString()); // Should be: "[1, 8) [10, 21)"
rl.remove([10, 11]);
console.log(rl.toString()); // Should be: "[1, 8) [11, 21)"
rl.remove([15, 17]);
console.log(rl.toString()); // Should be: "[1, 8) [11, 15) [17, 21)"
rl.remove([3, 19]);
console.log(rl.toString()); // Should be: "[1, 3) [19, 21)"
API
add(range: Array<number>): void
Adds a range to the list.
- `range
: An array of two integers specifying the beginning and end of the range.
remove(range: Array<number>): void
Removes a range from the list.
range
: An array of two integers specifying the beginning and end of the range.
toString(): string
Converts the list of ranges to a string representation.
TODO
- Add TypeScript Support
- publish it
npm publish
1.0.0
5 months ago