Giter Site home page Giter Site logo

interval-operations's Introduction

Interval operations

A collection of dependency-free utility functions for performing mathematical set operations on intervals and arrays of intervals.

Installation

npm install interval-operations
// or, with yarn
yarn add interval-operations

Usage

Union

import { union, arrayUnion } from 'interval-operations';

union([1,3], [2,4], [5,7]); // [[1,4], [5,7]]
arrayUnion([[1,3], [5,7]], [[2,4], [6,8]]); // [[1,4], [5,8]]

Intersection

import { intersection, arrayIntersection } from 'interval-operations';

intersection([1,3], [2,4]); // [2,3]
intersection([1,2], [3,4]); // null
arrayIntersection([[1,3], [5,7]], [[2,4], [6,8]]); // [[2,3], [5,6]]

Difference

import { difference, arrayDifference } from 'interval-operations';

difference([1,3], [2,3]); // [1,2]
arrayDifference([[1,3], [4,6]], [[2,5]]); // [[1,2], [5,6]]

Contains

import { contains, arrayContains } from 'interval-operations';

contains([1,3], [2,3]); // true
contains([1,3], [3,4]); // false
arrayContains([[1,3], [2,5]], [[1,5]]); // true

Example with dates

const store1Hours = [
  [new Date('2019-03-14 08:00'), new Date('2019-03-14 16:00')],
  [new Date('2019-03-16 10:00'), new Date('2019-03-16 14:00')]
];

const store2Hours = [
  [new Date('2019-03-14 10:00'), new Date('2019-03-14 14:00')],
  [new Date('2019-03-15 08:00'), new Date('2019-03-15 16:00')],
];

// When is at least one store open?
arrayUnion(store1Hours, store2Hours);
/* [
  [new Date('2019-03-14 08:00'), new Date('2019-03-14 16:00')],
  [new Date('2019-03-15 08:00'), new Date('2019-03-15 16:00')],
  [new Date('2019-03-16 10:00'), new Date('2019-03-16 14:00')]
] */

// When are both stores open?
arrayIntersection(store1Hours, store2Hours);
// [[new Date('2019-03-14 10:00'), new Date('2019-03-14 14:00')]]

// When is only store 1 open?
arrayDifference(store1Hours, store2Hours);
/* [
  [new Date('2019-03-14 08:00'), new Date('2019-03-14 10:00')],
  [new Date('2019-03-14 14:00'), new Date('2019-03-14 16:00')],
  [new Date('2019-03-16 10:00'), new Date('2019-03-16 14:00')]
] */

interval-operations's People

Contributors

aanckar avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

interval-operations's Issues

arrayIntersection is not equal intersection?

    console.log(arrayIntersection([[1, 2]], [[2, 3]]))  // === []
    console.log(intersection([1, 2], [2, 3]))  // === [ 2, 2 ]

if Interval design is [x,y) I think arrayIntersection is right.
then I try union ,It is indeed [x,y) ,so intersection return error?

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.