-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
utils.ts
78 lines (71 loc) · 2.43 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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { DataType } from '../types';
import { IndexPatternPrivateState, IndexPattern } from './types';
import { DraggedField } from './indexpattern';
import {
BaseIndexPatternColumn,
FieldBasedIndexPatternColumn,
} from './operations/definitions/column_types';
import { operationDefinitionMap, OperationType } from './operations';
/**
* Normalizes the specified operation type. (e.g. document operations
* produce 'number')
*/
export function normalizeOperationDataType(type: DataType) {
return type === 'document' ? 'number' : type;
}
export function hasField(column: BaseIndexPatternColumn): column is FieldBasedIndexPatternColumn {
return 'sourceField' in column;
}
export function sortByField<C extends BaseIndexPatternColumn>(columns: C[]) {
return [...columns].sort((column1, column2) => {
if (hasField(column1) && hasField(column2)) {
return column1.sourceField.localeCompare(column2.sourceField);
}
return column1.operationType.localeCompare(column2.operationType);
});
}
export function isDraggedField(fieldCandidate: unknown): fieldCandidate is DraggedField {
return (
typeof fieldCandidate === 'object' &&
fieldCandidate !== null &&
'field' in fieldCandidate &&
'indexPatternId' in fieldCandidate
);
}
export function hasInvalidReference(state: IndexPatternPrivateState) {
return Object.values(state.layers).some((layer) => {
return layer.columnOrder.some((columnId) => {
const column = layer.columns[columnId];
return (
hasField(column) &&
fieldIsInvalid(
column.sourceField,
column.operationType,
state.indexPatterns[layer.indexPatternId]
)
);
});
});
}
export function fieldIsInvalid(
sourceField: string | undefined,
operationType: OperationType | undefined,
indexPattern: IndexPattern
) {
const operationDefinition = operationType && operationDefinitionMap[operationType];
return Boolean(
sourceField &&
operationDefinition &&
!indexPattern.fields.some(
(field) =>
field.name === sourceField &&
operationDefinition.input === 'field' &&
operationDefinition.getPossibleOperationForField(field) !== undefined
)
);
}