What is discontinuous-range?
The 'discontinuous-range' npm package allows you to create and manipulate ranges of numbers that can have gaps. This is useful for scenarios where you need to handle ranges that are not continuous, such as time slots, IP ranges, or any other numerical ranges with exclusions.
What are discontinuous-range's main functionalities?
Create a Range
This feature allows you to create a continuous range of numbers. In this example, a range from 1 to 10 is created.
const Range = require('discontinuous-range');
const range = new Range(1, 10);
console.log(range.toString()); // '1-10'
Add a Sub-Range
This feature allows you to add a sub-range to an existing range. In this example, a sub-range from 20 to 30 is added to the initial range of 1 to 10.
const Range = require('discontinuous-range');
const range = new Range(1, 10);
range.add(20, 30);
console.log(range.toString()); // '1-10,20-30'
Remove a Sub-Range
This feature allows you to remove a sub-range from an existing range. In this example, the sub-range from 5 to 7 is removed from the range '1-10,20-30'.
const Range = require('discontinuous-range');
const range = new Range(1, 10);
range.add(20, 30);
range.remove(5, 7);
console.log(range.toString()); // '1-4,8-10,20-30'
Check if a Number is in Range
This feature allows you to check if a specific number is within the defined range. In this example, 5 is within the range '1-10,20-30' but 15 is not.
const Range = require('discontinuous-range');
const range = new Range(1, 10);
range.add(20, 30);
console.log(range.contains(5)); // true
console.log(range.contains(15)); // false
Other packages similar to discontinuous-range
range-parser
The 'range-parser' package is used to parse HTTP Range headers. It is more specialized for handling byte ranges in HTTP requests, whereas 'discontinuous-range' is more general-purpose for any numerical ranges with gaps.
interval-tree
The 'interval-tree' package provides a data structure for storing intervals and efficiently querying them. It is more focused on interval queries and overlaps, while 'discontinuous-range' is focused on creating and manipulating ranges with gaps.
buckets-js
The 'buckets-js' package offers various data structures, including sets and maps, which can be used to manage ranges. However, it does not provide the same level of specialized functionality for discontinuous ranges as 'discontinuous-range'.
discontinuous-range
DiscontinuousRange(1, 10).subtract(4, 6); // [ 1-3, 7-10 ]
this is a pretty simple module, but it exists to service another project
so this'll be pretty lacking documentation.
reading the test to see how this works may help. otherwise, here's an example
that I think pretty much sums it up
###Example
var all_numbers = new DiscontinuousRange(1, 100); //[ 1-100 ]
var bad_numbers = DiscontinuousRange(13).add(8).add(60,80); //[8, 13, 60-80]
var good_numbers = all_numbers.clone().subtract(bad_numbers);
console.log(good_numbers.toString()); //[ 1-7, 9-12, 14-59, 81-100 ]
var random_good_number = good_numbers.index(Math.floor(Math.random() * good_numbers.length));