/
maxValues.ts
114 lines (109 loc) · 3.49 KB
/
maxValues.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
105
106
107
108
109
110
111
112
113
114
import { isBigInt, isNumber, toFloatOrThrow } from '@terascope/utils';
import { FieldType } from '@terascope/types';
import {
FieldTransformConfig, FunctionDefinitionType,
ProcessMode, DataTypeFieldAndChildren, FunctionDefinitionCategory
} from '../interfaces';
function maxValuesReducer(
acc: number|null,
curr: (number|bigint|null)[]|(number|bigint|null)
): number|null {
const currValue = (Array.isArray(curr) ? maxValuesFn(curr) : curr);
if (currValue == null) return acc;
if (acc == null) return toFloatOrThrow(currValue);
if (acc >= currValue) return acc;
return toFloatOrThrow(currValue);
}
function maxValuesFn(value: unknown): bigint|number|null {
if (isNumber(value) || isBigInt(value)) return value;
if (!Array.isArray(value)) return null;
return value.reduce(maxValuesReducer, null);
}
export const maxValuesConfig: FieldTransformConfig = {
name: 'maxValues',
type: FunctionDefinitionType.FIELD_TRANSFORM,
process_mode: ProcessMode.FULL_VALUES,
category: FunctionDefinitionCategory.NUMERIC,
description: 'Returns the maximum value in an array, this requires an array to function correctly',
examples: [
{
args: {},
config: {
version: 1,
fields: { testField: { type: FieldType.Byte, array: true } }
},
field: 'testField',
input: [100, 10],
output: 100
},
{
args: {},
config: {
version: 1,
fields: { testField: { type: FieldType.Byte, array: true } }
},
field: 'testField',
input: [10],
output: 10
},
{
args: {},
config: {
version: 1,
fields: {
testField: { type: FieldType.Tuple },
'testField.0': { type: FieldType.Byte, array: true },
'testField.1': { type: FieldType.Integer },
'testField.2': { type: FieldType.Long }
}
},
field: 'testField',
input: [10, 100000, 2],
output: 100000
},
{
args: {},
config: {
version: 1,
fields: {
testField: { type: FieldType.Tuple },
'testField.0': { type: FieldType.Byte, array: true },
'testField.1': { type: FieldType.Integer },
'testField.2': { type: FieldType.Long, array: true },
'testField.3': { type: FieldType.Short }
}
},
field: 'testField',
input: [[10, null], 100000, [2], null],
output: 100000
},
{
args: {},
config: {
version: 1,
fields: {
testField: { type: FieldType.Byte },
}
},
field: 'testField',
input: 2,
output: 2
}
],
create() {
return maxValuesFn;
},
argument_schema: {},
accepts: [FieldType.Number],
output_type(inputConfig: DataTypeFieldAndChildren): DataTypeFieldAndChildren {
const { field_config } = inputConfig;
return {
field_config: {
...field_config,
array: false,
type: FieldType.Number
},
child_config: undefined
};
}
};