-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
helpers.ts
104 lines (91 loc) · 2.8 KB
/
helpers.ts
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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import datemath from '@elastic/datemath';
import { compact, pickBy } from 'lodash';
import moment from 'moment';
import { UrlParams } from './types';
function getParsedDate(rawDate?: string, options = {}) {
if (rawDate) {
const parsed = datemath.parse(rawDate, options);
if (parsed && parsed.isValid()) {
return parsed.toDate();
}
}
}
export function getExactDate(rawDate: string) {
const isRelativeDate = rawDate.startsWith('now');
if (isRelativeDate) {
// remove rounding from relative dates "Today" (now/d) and "This week" (now/w)
const rawDateWithouRounding = rawDate.replace(/\/([smhdw])$/, '');
return getParsedDate(rawDateWithouRounding);
}
return getParsedDate(rawDate);
}
export function getDateRange({
state = {},
rangeFrom,
rangeTo,
}: {
state?: Pick<
UrlParams,
'rangeFrom' | 'rangeTo' | 'start' | 'end' | 'exactStart' | 'exactEnd'
>;
rangeFrom?: string;
rangeTo?: string;
}) {
// If the previous state had the same range, just return that instead of calculating a new range.
if (state.rangeFrom === rangeFrom && state.rangeTo === rangeTo) {
return {
start: state.start,
end: state.end,
exactStart: state.exactStart,
exactEnd: state.exactEnd,
};
}
const start = getParsedDate(rangeFrom);
const end = getParsedDate(rangeTo, { roundUp: true });
const exactStart = rangeFrom ? getExactDate(rangeFrom) : undefined;
const exactEnd = rangeTo ? getExactDate(rangeTo) : undefined;
// `getParsedDate` will return undefined for invalid or empty dates. We return
// the previous state if either date is undefined.
if (!start || !end) {
return {
start: state.start,
end: state.end,
exactStart: state.exactStart,
exactEnd: state.exactEnd,
};
}
// rounds down start to minute
const roundedStart = moment(start).startOf('minute');
return {
start: roundedStart.toISOString(),
end: end.toISOString(),
exactStart: exactStart?.toISOString(),
exactEnd: exactEnd?.toISOString(),
};
}
export function toNumber(value?: string) {
if (value !== undefined) {
return parseInt(value, 10);
}
}
export function toString(value?: string) {
if (value === '' || value === 'null' || value === 'undefined') {
return;
}
return value;
}
export function toBoolean(value?: string) {
return value === 'true';
}
export function getPathAsArray(pathname: string = '') {
return compact(pathname.split('/'));
}
export function removeUndefinedProps<T extends object>(obj: T): Partial<T> {
return pickBy(obj, (value) => value !== undefined);
}