all files / src/modules/ time.js

90.91% Statements 20/22
66.67% Branches 4/6
100% Functions 2/2
90.48% Lines 19/21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39                                        
import parseIsoDuration from 'parse-iso-duration';
 
 
export const getPlaySliderParams = function (timestamps, timeGrain) {
  let start = Math.min(...timestamps);
  let end = Math.max(...timestamps);
  let step;
 
  if (timeGrain.indexOf('/') > 0) {
    // Here, time grain is a time interval instead of a simple duration, either
    // `reference/duration` or `duration/reference`. We need to parse the
    // duration and make sure that start and end are in the right places. For
    // example, if `reference` is a Saturday and `duration` is 1 week (P1W)
    // then both start and end should be Saturdays.
    const parts = timeGrain.split('/', 2);
    let reference;
    if (Iparts[0].endsWith('Z')) {  // ISO string
      reference = new Date(parts[0]).getTime();
      step = parseIsoDuration(parts[1]);
    } else {
      reference = new Date(parts[1]).getTime();
      step = parseIsoDuration(parts[0]);
    }
    start = reference + step * Math.floor((start - reference) / step);
    end = reference + step * (Math.floor((end - reference) / step) + 1);
  } else {
    // lock start and end to the closest steps
    step = parseIsoDuration(timeGrain);
    start -= start % step;
    end += step - end % step;
  }
 
  const values = timeGrain != null ? [start, start + step] : [start, end];
  const disabled = timestamps.every(timestamp => timestamp === null);
 
  return { start, end, step, values, disabled };
};