github.com/martinohmann/rfoutlet@v1.2.1-0.20220707195255-8a66aa411105/web/src/convert.js (about)

     1  import { DateTime } from 'luxon';
     2  
     3  function intervalToDateTimes({ from, to }) {
     4    return {
     5      from: dayTimeToDateTime(from),
     6      to: dayTimeToDateTime(to),
     7    };
     8  }
     9  
    10  function dayTimeToDateTime(dayTime) {
    11    const { hour, minute } = dayTime;
    12  
    13    return DateTime.local().set({ hour, minute });
    14  }
    15  
    16  function dateTimesToInterval({ from, to }) {
    17    return {
    18      from: dateTimeToDayTime(from),
    19      to: dateTimeToDayTime(to),
    20    };
    21  }
    22  
    23  function dateTimeToDayTime(dateTime) {
    24    const { hour, minute } = dateTime;
    25  
    26    return { hour, minute };
    27  }
    28  
    29  function intervalToApp(interval) {
    30    const { from, to } = intervalToDateTimes(interval);
    31  
    32    return { ...interval, from, to };
    33  }
    34  
    35  function scheduleToApp(schedule) {
    36    const intervals = schedule || [];
    37  
    38    return intervals.map(interval => intervalToApp(interval));
    39  }
    40  
    41  export function convertToApp(groups) {
    42    return groups.map(group => {
    43      const outlets = group.outlets || [];
    44  
    45      group.outlets = outlets.map(outlet => {
    46        outlet.schedule = scheduleToApp(outlet.schedule);
    47  
    48        return outlet;
    49      });
    50  
    51      return group;
    52    });
    53  }
    54  
    55  export function intervalToApi(interval) {
    56    const { from, to } = dateTimesToInterval(interval);
    57  
    58    return { ...interval, from, to };
    59  }