Coder Social home page Coder Social logo

gaps-islands's Introduction

Gaps and Islands

Reduces ranges into gaps and islands.

ranges       ├─────┤       ├─┤     ├───────┤   ├──┤
                ├──────┤             ├─┤   ├────┤  
islands      ◼◼◼◼◼◼◼◼◼◼◼   ◼◼◼     ◼◼◼◼◼◼◼◼◼◼◼◼◼◼◼◼
gaps                    └─┘   └───┘       

Identifying gaps and islands across overlapping ranges is a classic SQL problem. This module answers the same question if your data is stored in an array instead of a database.

Basic Usage

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.

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 }
]*/

gaps-islands's People

Contributors

mablay avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.