Giter Site home page Giter Site logo

slot-calculator's Introduction

Slot Calculator

GitHub Workflow Status npm

Calculate time slots for your users to choose from.

  • Intersect availabilities of multiple users/resources.
  • Full timezone support.
  • Blazing fast (usage of ES6 Sets, Maps and efficient data structures).
  • Works great with Luxon and V-Calendar.

Table of contents

Documentation

View the JS/TS reference. TypeScript language support in your IDE is sufficient to make use of this package's types and annotations.

Demo

Play with a live demo on CodeSandbox

Usage

Setup

npm install slot-calculator
import { getSlots } from "slot-calculator"

const { allSlots } = getSlots({
  from: "2022-01-01",
  to: "2022-01-02",
  duration: 60,
})

Returned values

  • allSlots: An array of all generated slots. Only use this as a starting point for manipulating the output.
  • availableSlots: An array of available slots. Only use this as a starting point for manipulating the output.
  • allDates: An array of dates, including unavailable ones, between the from and to configuration variables. Use this to allow users to specify their own datetime, but within your chosen bounds.
  • availableDates: An array of available dates. Use this to mark on a calendar which dates can be selected.
  • allSlotsByDay: Once a user has selected a date, use this object to easily find all the slots for that day.
  • availableSlotsByDay: Once a user has selected a date, use this object to easily find the available slots for that day.
  • timeTaken: Worried that the number crunching is slowing down your app? Monitor this variable to see the time taken by slot calculator.

Examples

For these examples to work, use this setup code:

import { DateTime, Settings } from "luxon";

Settings.defaultZone = "UTC";
const dateTimeRef = DateTime.utc(2022, 1, 1);

Basic usage

getSlots({
  from: dateTimeRef.toISO(),
  to: dateTimeRef.plus({ hour: 2 }).toISO(),
  duration: 60,
});

With availabilities and unavailabilities

getSlots({
  from: dateTimeRef.toISO(),
  to: dateTimeRef.plus({ hour: 3 }).toISO(),
  availability: [
    {
      from: dateTimeRef.plus({ hour: 1 }).toISO(),
      to: dateTimeRef.plus({ hour: 2 }).toISO(),
    },
  ],
  unavailability: [
    {
      from: dateTimeRef.plus({ hour: 2 }).toISO(),
      to: dateTimeRef.plus({ hour: 3 }).toISO(),
    },
  ],
  duration: 60,
});

With multiple users

getSlots({
  from: dateTimeRef.toISO(),
  to: dateTimeRef.plus({ hour: 2 }).toISO(),
  availability: [
    {
      day: "Saturday",
      from: "00:00",
      to: "01:00",
      metadata: {
        user: "Alice",
      },
    },
    {
      from: dateTimeRef.plus({ hour: 1 }).toISO(),
      to: dateTimeRef.plus({ hour: 2 }).toISO(),
      metadata: {
        user: "Bob",
      },
    },
  ],
  duration: 60,
});

With timezones

getSlots({
  from: dateTimeRef.toISO(),
  to: dateTimeRef.plus({ hour: 2 }).toISO(),
  outputTimezone: "Europe/Paris",
  availability: [
    {
      day: "Saturday",
      from: "01:00",
      to: "02:00",
      timezone: "Europe/Paris",
    },
  ],
  duration: 60,
});

slot-calculator's People

Contributors

huzaifahj avatar

Stargazers

 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.