Code Monkey home page Code Monkey 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

farazahmadkhan15 avatar huzaifahj avatar iamnafets avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

slot-calculator's Issues

slots are shifted

Hello, I'm working on my side project for booking, and there is one issue,
the configuration you have business hours 9-5, event duration is 60 minutes, but for some reason, you need to move one person by 15 minutes, but that will affect all future slots...
image

is it possible to provide an option if "slots" should remain respected?

performance is not defined

iam using this module in express.js but it show me the error performance is not defined, can i use this module in express.js

this is my code

const { getSlots } = require('slot-calculator')

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

Slot timezone is ignored if day is not passed

When day is not passed to availability slot, timezone is ignored. Please below is the code with expected & received output:

const { allSlots } = getSlots({
    from: '2023-11-24T09:00:00.000Z',
    to: '2023-11-24T11:00:00.000Z',
    outputTimezone: 'UTC',
    availability: [
        {
            // day: 'Friday',
            from: '09:00',
            to: '11:00',
            timezone: 'UTC',
            metadata: {
                user: 'user1'
            }
        }
    ],
    duration: duration,
});

Output:

[
    {
        "from": "2023-11-24T09:00:00.000Z",
        "to": "2023-11-24T10:00:00.000Z",
        "available": false
    },
    {
        "from": "2023-11-24T10:00:00.000Z",
        "to": "2023-11-24T11:00:00.000Z",
        "available": false
    }
]

Expected output:

[
    {
        "from": "2023-11-24T09:00:00.000Z",
        "to": "2023-11-24T10:00:00.000Z",
        "available": true,
        "metadataAvailable": [
            {
                "user": "user1"
            }
        ]
    },
    {
        "from": "2023-11-24T10:00:00.000Z",
        "to": "2023-11-24T11:00:00.000Z",
        "available": true,
        "metadataAvailable": [
            {
                "user": "user1"
            }
        ]
    }
]

Getting the expected output if I pass the day as Friday

Is anyone else facing this issue? If yes, how can I resolve this?

BUG - Does not work with ts file

simulate :

Create a project with npx tsc --init
npm install slot-calculator

create a file temp.ts and put the code exemple from doc
import { getSlots } from "slot-calculator"
const { allSlots } = getSlots({
from: "2022-01-01",
to: "2022-01-02",
duration: 60,
})

run : ts-node temp.ts

Error [ERR_REQUIRE_ESM]: require() of ES Module \node_modules\slot-calculator\dist\index.js from \Documents\Dates\temp.ts not supported.
Instead change the require of index.js in Documents\Dates\temp.ts to a dynamic import() which is available in all CommonJS modules

Daylight Savings Time Bug

These lines fail on daylight savings time.

slot-calculator/index.ts

Lines 215 to 237 in 7d0b064

const dayText = typeof slot.day === "string" ? slot.day : slot.day.text;
for (const date of getDatesByDay(slot.timezone ?? "UTC")[dayText] ??
[]) {
const obj: InputSlot = {
from: DateTime.fromISO(date, {
zone: slot.timezone,
})
.plus({
hours: Number(slot.from.split(":")[0]),
minutes: Number(slot.from.split(":")[1]),
})
.toISO(),
to: DateTime.fromISO(date, {
zone: slot.timezone,
})
.plus({
hours: Number(slot.to.split(":")[0]),
minutes: Number(slot.to.split(":")[1]),
})
.toISO(),
metadata: slot.metadata,
};
generatedSlots.push(obj);

The DST shift (e.g. on May 10th) moves forward at 2am, pushing slots +- 1 hour.

Timezone America/Sao_Paulo is not consistent

This example wont work, it should return available true. :(

import { getSlots } from "slot-calculator";
import { DateTime, Settings } from "luxon";
//Settings.defaultZone = "UTC";
const dateTimeRef = DateTime.utc(2023, 1, 24);
const weekday = dateTimeRef.weekdayLong;
console.log(weekday)

const allSlots = getSlots({
  from: "2023-01-24T12:00:00-03:00",
  to: "2023-01-24T13:00:00-03:00", 
  outputTimezone: "America/Sao_Paulo",
  duration: 60,
  unavailability:[],
  availability: [
    {
        day: "segunda",
        from: "09:00",
        to: "19:00",
        timezone: "America/Sao_Paulo"
    },{
        day: "terca-feira",
        from: "01:00",
        to: "23:00",
        timezone: "America/Sao_Paulo"
    },
    {
        day: "quarta-feira",
        from: "09:00",
        to: "19:00",
        timezone: "America/Sao_Paulo"
    },
    {
        day: "quinta-feira",
        from: "09:00",
        to: "19:00",
        timezone: "America/Sao_Paulo"
    },
    {
        day: "sexta-feira",
        from: "09:00",
        to: "19:00",
        timezone: "America/Sao_Paulo"
    }
  ]
});

console.log(allSlots);

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.