forked from SigNoz/signoz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
94 lines (77 loc) · 2.64 KB
/
utils.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
import { HAVING_FILTER_REGEXP } from 'constants/regExp';
import { IOption } from 'hooks/useResourceAttribute/types';
import uniqWith from 'lodash-es/unionWith';
import { parse } from 'papaparse';
import { HavingForm } from 'types/api/queryBuilder/queryBuilderData';
import { ORDERBY_FILTERS } from './OrderByFilter/config';
import {
orderByValueDelimiter,
splitOrderByFromString,
} from './OrderByFilter/utils';
import { getRemoveOrderFromValue } from './QueryBuilderSearch/utils';
export const handleKeyDownLimitFilter: React.KeyboardEventHandler<HTMLInputElement> = (
event,
): void => {
const keyCode = event.keyCode || event.which;
const isBackspace = keyCode === 8;
const isNumeric =
(keyCode >= 48 && keyCode <= 57) || (keyCode >= 96 && keyCode <= 105);
if (!isNumeric && !isBackspace) {
event.preventDefault();
}
};
export const getHavingObject = (currentSearch: string): HavingForm => {
const textArr = currentSearch.split(' ');
const [columnName = '', op = '', ...value] = textArr;
return { columnName, op, value };
};
export const isValidHavingValue = (search: string): boolean => {
const values = getHavingObject(search).value.join(' ');
if (values) {
return HAVING_FILTER_REGEXP.test(values);
}
return true;
};
export const getUniqueOrderByValues = (values: IOption[]): IOption[] => {
const modifiedValues = values.map((item) => {
const match = parse(item.value, { delimiter: orderByValueDelimiter });
if (!match) return { label: item.label, value: item.value };
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-unused-vars
const [_, order] = match.data.flat() as string[];
if (order)
return {
label: item.label,
value: item.value,
};
return {
label: `${item.value} ${ORDERBY_FILTERS.ASC}`,
value: `${item.value}${orderByValueDelimiter}${ORDERBY_FILTERS.ASC}`,
};
});
return uniqWith(
modifiedValues,
(current, next) =>
getRemoveOrderFromValue(current.value) ===
getRemoveOrderFromValue(next.value),
);
};
export const getValidOrderByResult = (result: IOption[]): IOption[] =>
result.reduce<IOption[]>((acc, item) => {
if (item.value === ORDERBY_FILTERS.ASC || item.value === ORDERBY_FILTERS.DESC)
return acc;
if (
item.value.includes(ORDERBY_FILTERS.ASC) ||
item.value.includes(ORDERBY_FILTERS.DESC)
) {
const splittedOrderBy = splitOrderByFromString(item.value);
if (splittedOrderBy) {
acc.push({
label: `${splittedOrderBy.columnName} ${splittedOrderBy.order}`,
value: `${splittedOrderBy.columnName}${orderByValueDelimiter}${splittedOrderBy.order}`,
});
return acc;
}
}
acc.push(item);
return acc;
}, []);