Welcome to mirror list, hosted at ThFree Co, Russian Federation.

datetime_range.js « utils « lib « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 391b685f74026c658c08233c483d2819b0a2de69 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import dateformat from 'dateformat';
import { pick, omit, isEqual, isEmpty } from 'lodash';
import { DATETIME_RANGE_TYPES } from './constants';
import { secondsToMilliseconds } from './datetime_utility';

const MINIMUM_DATE = new Date(0);

const DEFAULT_DIRECTION = 'before';

const durationToMillis = (duration) => {
  if (Object.entries(duration).length === 1 && Number.isFinite(duration.seconds)) {
    return secondsToMilliseconds(duration.seconds);
  }
  // eslint-disable-next-line @gitlab/require-i18n-strings
  throw new Error('Invalid duration: only `seconds` is supported');
};

const dateMinusDuration = (date, duration) => new Date(date.getTime() - durationToMillis(duration));

const datePlusDuration = (date, duration) => new Date(date.getTime() + durationToMillis(duration));

const isValidDuration = (duration) => Boolean(duration && Number.isFinite(duration.seconds));

const isValidDateString = (dateString) => {
  if (typeof dateString !== 'string' || !dateString.trim()) {
    return false;
  }

  try {
    // dateformat throws error that can be caught.
    // This is better than using `new Date()`
    dateformat(dateString, 'isoUtcDateTime');
    return true;
  } catch (e) {
    return false;
  }
};

const handleRangeDirection = ({ direction = DEFAULT_DIRECTION, anchorDate, minDate, maxDate }) => {
  let startDate;
  let endDate;

  if (direction === DEFAULT_DIRECTION) {
    startDate = minDate;
    endDate = anchorDate;
  } else {
    startDate = anchorDate;
    endDate = maxDate;
  }

  return {
    startDate,
    endDate,
  };
};

/**
 * Converts a fixed range to a fixed range
 * @param {Object} fixedRange - A range with fixed start and
 * end (e.g. "midnight January 1st 2020 to midday January31st 2020")
 */
const convertFixedToFixed = ({ start, end }) => ({
  start,
  end,
});

/**
 * Converts an anchored range to a fixed range
 * @param {Object} anchoredRange - A duration of time
 * relative to a fixed point in time (e.g., "the 30 minutes
 * before midnight January 1st 2020", or "the 2 days
 * after midday on the 11th of May 2019")
 */
const convertAnchoredToFixed = ({ anchor, duration, direction }) => {
  const anchorDate = new Date(anchor);

  const { startDate, endDate } = handleRangeDirection({
    minDate: dateMinusDuration(anchorDate, duration),
    maxDate: datePlusDuration(anchorDate, duration),
    direction,
    anchorDate,
  });

  return {
    start: startDate.toISOString(),
    end: endDate.toISOString(),
  };
};

/**
 * Converts a rolling change to a fixed range
 *
 * @param {Object} rollingRange - A time range relative to
 * now (e.g., "last 2 minutes", or "next 2 days")
 */
const convertRollingToFixed = ({ duration, direction }) => {
  // Use Date.now internally for easier mocking in tests
  const now = new Date(Date.now());

  return convertAnchoredToFixed({
    duration,
    direction,
    anchor: now.toISOString(),
  });
};

/**
 * Converts an open range to a fixed range
 *
 * @param {Object} openRange - A time range relative
 * to an anchor (e.g., "before midnight on the 1st of
 * January 2020", or "after midday on the 11th of May 2019")
 */
const convertOpenToFixed = ({ anchor, direction }) => {
  // Use Date.now internally for easier mocking in tests
  const now = new Date(Date.now());

  const { startDate, endDate } = handleRangeDirection({
    minDate: MINIMUM_DATE,
    maxDate: now,
    direction,
    anchorDate: new Date(anchor),
  });

  return {
    start: startDate.toISOString(),
    end: endDate.toISOString(),
  };
};

/**
 * Handles invalid date ranges
 */
const handleInvalidRange = () => {
  // eslint-disable-next-line @gitlab/require-i18n-strings
  throw new Error('The input range does not have the right format.');
};

const handlers = {
  invalid: handleInvalidRange,
  fixed: convertFixedToFixed,
  anchored: convertAnchoredToFixed,
  rolling: convertRollingToFixed,
  open: convertOpenToFixed,
};

/**
 * Validates and returns the type of range
 *
 * @param {Object} Date time range
 * @returns {String} `key` value for one of the handlers
 */
export function getRangeType(range) {
  const { start, end, anchor, duration } = range;

  if ((start || end) && !anchor && !duration) {
    return isValidDateString(start) && isValidDateString(end)
      ? DATETIME_RANGE_TYPES.fixed
      : DATETIME_RANGE_TYPES.invalid;
  }
  if (anchor && duration) {
    return isValidDateString(anchor) && isValidDuration(duration)
      ? DATETIME_RANGE_TYPES.anchored
      : DATETIME_RANGE_TYPES.invalid;
  }
  if (duration && !anchor) {
    return isValidDuration(duration) ? DATETIME_RANGE_TYPES.rolling : DATETIME_RANGE_TYPES.invalid;
  }
  if (anchor && !duration) {
    return isValidDateString(anchor) ? DATETIME_RANGE_TYPES.open : DATETIME_RANGE_TYPES.invalid;
  }
  return DATETIME_RANGE_TYPES.invalid;
}

/**
 * convertToFixedRange Transforms a `range of time` into a `fixed range of time`.
 *
 * The following types of a `ranges of time` can be represented:
 *
 * Fixed Range: A range with fixed start and end (e.g. "midnight January 1st 2020 to midday January 31st 2020")
 * Anchored Range: A duration of time relative to a fixed point in time (e.g., "the 30 minutes before midnight January 1st 2020", or "the 2 days after midday on the 11th of May 2019")
 * Rolling Range: A time range relative to now (e.g., "last 2 minutes", or "next 2 days")
 * Open Range: A time range relative to an anchor (e.g., "before midnight on the 1st of January 2020", or "after midday on the 11th of May 2019")
 *
 * @param {Object} dateTimeRange - A Time Range representation
 * It contains the data needed to create a fixed time range plus
 * a label (recommended) to indicate the range that is covered.
 *
 * A definition via a TypeScript notation is presented below:
 *
 *
 * type Duration = { // A duration of time, always in seconds
 *   seconds: number;
 * }
 *
 * type Direction = 'before' | 'after'; // Direction of time relative to an anchor
 *
 * type FixedRange = {
 *   start: ISO8601;
 *   end: ISO8601;
 *   label: string;
 * }
 *
 * type AnchoredRange = {
 *   anchor: ISO8601;
 *   duration: Duration;
 *   direction: Direction; // defaults to 'before'
 *   label: string;
 * }
 *
 * type RollingRange = {
 *   duration: Duration;
 *   direction: Direction; // defaults to 'before'
 *   label: string;
 * }
 *
 * type OpenRange = {
 *   anchor: ISO8601;
 *   direction: Direction; // defaults to 'before'
 *   label: string;
 * }
 *
 * type DateTimeRange = FixedRange | AnchoredRange | RollingRange | OpenRange;
 *
 *
 * @returns {FixedRange} An object with a start and end in ISO8601 format.
 */
export const convertToFixedRange = (dateTimeRange) =>
  handlers[getRangeType(dateTimeRange)](dateTimeRange);

/**
 * Returns a copy of the object only with time range
 * properties relevant to time range calculation.
 *
 * Filtered properties are:
 * - 'start'
 * - 'end'
 * - 'anchor'
 * - 'duration'
 * - 'direction': if direction is already the default, its removed.
 *
 * @param {Object} timeRange - A time range object
 * @returns Copy of time range
 */
const pruneTimeRange = (timeRange) => {
  const res = pick(timeRange, ['start', 'end', 'anchor', 'duration', 'direction']);
  if (res.direction === DEFAULT_DIRECTION) {
    return omit(res, 'direction');
  }
  return res;
};

/**
 * Returns true if the time ranges are equal according to
 * the time range calculation properties
 *
 * @param {Object} timeRange - A time range object
 * @param {Object} other - Time range object to compare with.
 * @returns true if the time ranges are equal, false otherwise
 */
export const isEqualTimeRanges = (timeRange, other) => {
  const tr1 = pruneTimeRange(timeRange);
  const tr2 = pruneTimeRange(other);
  return isEqual(tr1, tr2);
};

/**
 * Searches for a time range in a array of time ranges using
 * only the properies relevant to time ranges calculation.
 *
 * @param {Object} timeRange - Time range to search (needle)
 * @param {Array} timeRanges - Array of time tanges (haystack)
 */
export const findTimeRange = (timeRange, timeRanges) =>
  timeRanges.find((element) => isEqualTimeRanges(element, timeRange));

// Time Ranges as URL Parameters Utils

/**
 * List of possible time ranges parameters
 */
export const timeRangeParamNames = ['start', 'end', 'anchor', 'duration_seconds', 'direction'];

/**
 * Converts a valid time range to a flat key-value pairs object.
 *
 * Duration is flatted to avoid having nested objects.
 *
 * @param {Object} A time range
 * @returns key-value pairs object that can be used as parameters in a URL.
 */
export const timeRangeToParams = (timeRange) => {
  let params = pruneTimeRange(timeRange);
  if (timeRange.duration) {
    const durationParms = {};
    Object.keys(timeRange.duration).forEach((key) => {
      durationParms[`duration_${key}`] = timeRange.duration[key].toString();
    });
    params = { ...durationParms, ...params };
    params = omit(params, 'duration');
  }
  return params;
};

/**
 * Converts a valid set of flat params to a time range object
 *
 * Parameters that are not part of time range object are ignored.
 *
 * @param {params} params - key-value pairs object.
 */
export const timeRangeFromParams = (params) => {
  const timeRangeParams = pick(params, timeRangeParamNames);
  let range = Object.entries(timeRangeParams).reduce((acc, [key, val]) => {
    // unflatten duration
    if (key.startsWith('duration_')) {
      acc.duration = acc.duration || {};
      acc.duration[key.slice('duration_'.length)] = parseInt(val, 10);
      return acc;
    }
    return { [key]: val, ...acc };
  }, {});
  range = pruneTimeRange(range);
  return !isEmpty(range) ? range : null;
};