• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

110
Vistas
Decreasing the complexity of the JS algorithm for creating slots based on days of the week

Trying to generate slots in future based on availableTimes, slots are nothing but they have start time and end time based on slotSize and startTime

Example data:

availableTimes: 
{
  TUESDAY: [{ dayOfTheWeek: "TUESDAY", start: 8, end: 10 }],
},
daysInFuture: 1,
timeZone: "Australia/Melbourne",
slotSizeInMinutes: 60,
timeStamp: 1640041590364

Based on this availableTime Data and considering current date is

2021-12-21T08:00:00.000+11:00

it should return

[
    {
      ref: "2021-12-21T08:00:00.000+11:00",
      startTime: "2021-12-21T08:00:00.000+11:00",
      endTime: "2021-12-21T09:00:00.000+11:00",
      available: true,
    },
    {
      ref: "2021-12-21T09:00:00.000+11:00",
      startTime: "2021-12-21T09:00:00.000+11:00",
      endTime: "2021-12-21T10:00:00.000+11:00",
      available: true,
    },
  ]

which is working as expected, I just want to decrease the complexity of the function.

function generateTimeSlots(
  availableTimes: {
    [x: string]: MenuAvailability[];
  },
  daysInFuture: number,
  timeZone: string,
  slotSizeInMinutes: number,
  timeStamp: number
): orders.TimeSlot[] {
  const timeSlots = [];

  for (let j = 0; j <= daysInFuture; j++) {
    let currentDate = DateTime.fromMillis(timeStamp)
      .setZone(timeZone)
      .plus({ days: j });

    const currentDay = currentDate.weekdayLong.toUpperCase() as DayOfTheWeek;
    const slotsForTheDay = availableTimes[currentDay] ?? [];

    for (let i = 0; i < slotsForTheDay.length; i++) {
      const startHour = slotsForTheDay[i].start;
      const endHour = slotsForTheDay[i].end;

      const numOfSlots = ((endHour - startHour) * 60) / slotSizeInMinutes;

      const addMinutes = (date: DateTime, minutes: number) => {
        return DateTime.fromISO(date.toISO()).plus({ minutes: minutes });
      };

      currentDate = currentDate.set({
        hour: startHour,
        minute: 0,
        second: 0,
        millisecond: 0,
      });

      for (let i = 0; i < numOfSlots; i++) {
        timeSlots.push({
          ref: currentDate.toISO(),
          startTime: currentDate.toISO(),
          endTime: addMinutes(currentDate, slotSizeInMinutes).toISO(),
          available: true,
        });
        currentDate = addMinutes(currentDate, slotSizeInMinutes);
      }
    }
  }

  return timeSlots;
}
almost 3 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda