Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mablay/gaps-islands
finds gaps and islands in an array of ranges
https://github.com/mablay/gaps-islands
Last synced: 8 days ago
JSON representation
finds gaps and islands in an array of ranges
- Host: GitHub
- URL: https://github.com/mablay/gaps-islands
- Owner: mablay
- Created: 2022-09-08T22:18:17.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2022-09-12T17:14:12.000Z (about 2 years ago)
- Last Synced: 2023-11-20T11:03:40.908Z (12 months ago)
- Language: JavaScript
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Gaps and Islands
Reduces ranges into gaps and islands.
```
ranges ├─────┤ ├─┤ ├───────┤ ├──┤
├──────┤ ├─┤ ├────┤
islands ◼◼◼◼◼◼◼◼◼◼◼ ◼◼◼ ◼◼◼◼◼◼◼◼◼◼◼◼◼◼◼◼
gaps └─┘ └───┘
```Identifying gaps and islands across overlapping ranges is a [classic SQL problem](https://medium.com/analytics-vidhya/sql-classic-problem-identifying-gaps-and-islands-across-overlapping-date-ranges-5681b5fcdb8).
This module answers the same question if your data is stored in an array instead of a database.## Basic Usage
```js
import { findIslands, findGaps } from '@occami/gaps-islands'
const shifts = [
{ name: 'Alice', start: 2, end: 6 },
{ name: 'Bob', start: 4, end: 8 },
{ name: 'Clair', start: 12, end: 16 }
]// find overlapping ranges
console.log(findIslands(shifts))
/*[
{ start: 2, end: 8, value: [ { 'Alice', ... }, { 'Bob', ... } ] },
{ start: 12, end: 16, value: [ { 'Clair', ... } ] }
]*/
// see how start: 2 and end: 8 spans the first two
// records, because they overlap, causing an island.
// .value aggregates all objects from that island.// find gaps between ranges
console.log(findGaps(shifts))
/* [{ start: 8, end: 12 }] */
```## Advanced Usage
If you want fine control over the island reducer, you might want to use `mergeIslands`.
```js
import { mergeIslands } from '@occami/gaps-islands'
const shifts = [
{ name: 'Alice', start: 2, end: 6 },
{ name: 'Bob', start: 4, end: 8 },
{ name: 'Clair', start: 12, end: 16 }
]
// reduce information into islands
// 1. copy data of interest in the range.value property.
const ranges = shifts.map(r => ({ ...r, value: r.end - r.start }))
// 2. define a reducer that merges overlapping ranges into the islands 'value' property.
const reducer = (a, b) => a.value + b.value
// 3. pass the reducer as optional argument.
console.log(mergeIslands(ranges, reducer))
/*[
{ start: 2, end: 8, value: 8 },
{ start: 12, end: 16, value: 4 }
]*/
```