1.2.0 • Published 6 years ago

intersecting-ranges v1.2.0

Weekly downloads
471
License
MIT
Repository
github
Last release
6 years ago

Intersecting Ranges

Find the intersection of N intervals using a variant of Marzullo's algorithm.

diagram

Installation

yarn add intersecting-ranges

Usage

API

intersectingRanges(ranges [, options]);

Options

optiontypedefaultdescription
omitEmptybooleantrueDon't return the original ranges if there are no overlaps
withDatabooleanfalseOptionally store data for each range to be merged into the intersections

Example using ranges in the picture

const intersectingRanges = require("intersecting-ranges");

const ranges = [
  [1, 31], // pink
  [3, 10, { foo: 1 }], // orange
  [13, 20], // orange
  [23, 29], // orange
  [4, 15], // green
  [16, 30], // green
  [1, 7, { bar: 2 }], // blue
  [9, 24] // blue
];

intersectingRanges(ranges);
/* =>
[ [ 4, 7 ],
  [ 9, 10 ],
  [ 13, 15 ],
  [ 16, 20 ],
  [ 23, 24 ] ]
*/

intersectingRanges(ranges, { withData: true });
/* =>
[ [ 4, 7, { foo: 1, bar: 2 } ],
  [ 9, 10, { foo: 1 } ],
  [ 13, 15 ],
  [ 16, 20 ],
  [ 23, 24 ] ]
*/

With/without omitEmpty option

const ranges = [[1, 31], [34, 36]];

intersectingRanges(ranges);
// []
intersectingRanges(ranges, { omitEmpty: false });
// [[1, 31], [34, 36]];