vasc / rangelist

Implements list of ranges data sctructure to represent a non contiguous integer range

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RangeList

Implements list of ranges data structure to represent a non contiguous range of numbers. This can be helpful to represent things like multiple time slots with a break in between or continuous spaces that get progressively occupied leaving openings in the middle.

This library allows you to manipulate ranges iteratively gettings consistent results, which can be tricky to do by hand.

Example

const { 
  addRange, 
  removeRange,  
  format
} = require('./lib/RangeList')

const emptyRange = [];
const oneToFive = addRange(emptyRange, [1, 5]);
// [[1, 5]]

const threeToFive = removeRange(oneToFive, [1, 3]);
// [[3, 5]]

const multipleSegments = addRange(threeToFive, [1, 2]);
// [[1, 2], [3, 5]]

format(multipleSegments);
// [1, 2) [3, 5)

Check the implementation for method documentation and the test file for more usage examples.

About

Implements list of ranges data sctructure to represent a non contiguous integer range


Languages

Language:JavaScript 100.0%